Skip to content

Instantly share code, notes, and snippets.

@8parth
Created August 9, 2018 11:34
Show Gist options
  • Save 8parth/37336816b887072d9459b84e8e4401c8 to your computer and use it in GitHub Desktop.
Save 8parth/37336816b887072d9459b84e8e4401c8 to your computer and use it in GitHub Desktop.
Service Class Generator
# frozen_string_literal: true
# lib/generators/service/service_generator.rb
require 'rails/generators'
class ServiceGenerator < Rails::Generators::NamedBase
def create_service_file
directories = name.split('/')
directories.pop
directory_name = directories.join('/')
create_file "app/services/#{directory_name}/#{file_name}.rb", <<-FILE
class #{class_name}
def initialize
end
def call
end
end
FILE
end
end
# lib/generators/service/USAGE
Description:
Generates Service Class
Example:
rails generate service Thing
This will create:
app/services/thing.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment