Class: Railsmith::Generators::ModelServiceGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
ModelServiceGeneratorSupport
Defined in:
lib/generators/railsmith/model_service/model_service_generator.rb

Overview

Scaffolds a service class for a given model constant.

Default mode (no flags):

  • Generates into ‘app/services/<model>_service.rb` with no module wrapper

Namespace mode (–namespace=Billing::Services):

  • Generates into ‘app/services/billing/services/<model>_service.rb`

  • Wraps class in the given modules; auto-adds ‘domain` from first segment

Domain mode (–domain=Billing):

  • Generates into ‘app/domains/<domain>/services/<model>_service.rb`

  • Wraps class in ‘<Domain>::Services::<Model>Service`

Input DSL (–inputs or –inputs=name:type …):

  • Without values: introspects model columns (requires model to be loaded)

  • With values: generates explicit input declarations from the given specs

Association DSL (–associations):

  • Introspects model associations via reflect_on_all_associations

  • Generates has_many, has_one, belongs_to DSL and includes declaration

Instance Method Summary collapse

Instance Method Details

#create_model_serviceObject



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/generators/railsmith/model_service/model_service_generator.rb', line 182

def create_model_service
  if File.exist?(File.join(destination_root, target_file)) && !options[:force]
    say_status(
      :skip,
      "#{target_file} already exists (use --force to overwrite)",
      :yellow
    )
    return
  end

  template "model_service.rb.tt", target_file
  register_with_pipeline
end

#register_with_pipelineObject



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/generators/railsmith/model_service/model_service_generator.rb', line 196

def register_with_pipeline
  return if options[:pipeline].to_s.strip.empty?

  pipeline_path = ensure_pipeline_file
  return unless pipeline_path

  insert_into_file(
    pipeline_path,
    build_pipeline_step_line,
    before: /\n#{Regexp.escape(class_indent)}end\n?\z/m
  )
end