Class: RailsBaseApiScaffold::Generators::ControllerGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::ResourceHelpers, GeneratorHelpers
Defined in:
lib/generators/rails_base_api_scaffold/controller_generator.rb

Overview

Custom scaffolding generator

Instance Attribute Summary

Attributes included from GeneratorHelpers

#attributes, #options

Instance Method Summary collapse

Instance Method Details

#add_routesObject



20
21
22
23
24
25
26
27
# File 'lib/generators/rails_base_api_scaffold/controller_generator.rb', line 20

def add_routes
  # Include tabs and line break for proper formatting
  routes_string = "      resources :#{plural_name}\n"
  # Inject into file following the api and v1 namespaces
  inject_into_file 'config/routes.rb', after: "  namespace :api do\n    namespace :v1 do\n" do
    routes_string
  end
end

#add_scoped_searchObject



29
30
31
32
33
34
# File 'lib/generators/rails_base_api_scaffold/controller_generator.rb', line 29

def add_scoped_search
  scoped_search_string = "  scoped_search on: [ :id, :created_at, :updated_at ], only_explicit: true\n"
  inject_into_file "app/models/#{file_name}.rb", after: "class #{class_name} < ActiveRecord::Base\n" do
    scoped_search_string
  end
end

#copy_templatesObject



14
15
16
17
18
# File 'lib/generators/rails_base_api_scaffold/controller_generator.rb', line 14

def copy_templates
  template "controller.rb", File.join("app/controllers/api/v1", "#{controller_file_name}_controller.rb")
  template "serializer.rb", File.join("app/serializers", "#{file_name}_serializer.rb")
  template "spec.rb", File.join("spec/requests/api/v1", "#{controller_file_name}_controller_spec.rb")
end