docbrown/lib/generators/service/service_generator.rb
Michael Kohl 824e9a13cb
Add service generator (#11265)
* Add service generator

* Finalize service generator

* Add documentation

* Explicitly require rails/generators

* Update Zeitwerk initializer

* Remove explicit require

* Update docs/backend/service-objects.md

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>
2020-11-10 09:09:35 +07:00

24 lines
631 B
Ruby

class ServiceGenerator < Rails::Generators::NamedBase
source_root File.expand_path("templates", __dir__)
argument :arguments, type: :array, default: [], banner: "arguments for .call"
class_option :spec, type: :boolean, default: true
def create_service_file
template(
"service.erb",
File.join("app/services", class_path, "#{file_name}.rb"),
)
return unless options["spec"]
template(
"service_spec.erb",
File.join("spec/services", class_path, "#{file_name}_spec.rb"),
)
end
def signature
@signature ||= arguments.present? ? "(#{arguments.join(', ')})" : nil
end
end