Class: Superpowers::Generators::ScaffoldGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- Superpowers::Generators::ScaffoldGenerator
- Includes:
- Rails::Generators::ResourceHelpers
- Defined in:
- lib/generators/superpowers/scaffold/scaffold_generator.rb
Instance Method Summary collapse
-
#add_resource_route ⇒ Object
override.
- #create_controller ⇒ Object
- #generate_model_and_insert_association ⇒ Object
-
#initialize(args, *options) ⇒ ScaffoldGenerator
constructor
:nodoc:.
- #scaffold_views ⇒ Object
Constructor Details
#initialize(args, *options) ⇒ ScaffoldGenerator
:nodoc:
41 42 43 |
# File 'lib/generators/superpowers/scaffold/scaffold_generator.rb', line 41 def initialize(args, *) #:nodoc: super end |
Instance Method Details
#add_resource_route ⇒ Object
override
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/generators/superpowers/scaffold/scaffold_generator.rb', line 25 def add_resource_route return if [:actions].present? route_config = "resources :#{file_name.pluralize}\n" if @options[:nested_route] route_config = "resources :#{plural_nested_parent_name} do\n" \ " resources :#{file_name.pluralize}\n" \ "end\n" end route route_config gsub_file 'config/routes.rb', / *resources :#{plural_nested_parent_name}\n/, '' end |
#create_controller ⇒ Object
45 46 47 |
# File 'lib/generators/superpowers/scaffold/scaffold_generator.rb', line 45 def create_controller template "controller.rb", File.join('app/controllers', "#{controller_file_name}_controller.rb"), behavior: self.behavior end |
#generate_model_and_insert_association ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/generators/superpowers/scaffold/scaffold_generator.rb', line 53 def generate_model_and_insert_association case self.behavior when :invoke invoke :model, [singular_name, migration_attributes, scoped_by_reference].flatten, behavior: :invoke inject_into_file scoped_by_file_path, "\n has_many :#{plural_name}", after: "ApplicationRecord" when :revoke # gsub_file doesn't work for some reason, so this is a quick temp solution content = File.read(scoped_by_file_path).gsub(/\s*has_many :#{plural_name}/, '') File.open(scoped_by_file_path, 'wb') { |file| file.write(content) } invoke :model, [singular_name], behavior: :revoke end end |
#scaffold_views ⇒ Object
49 50 51 |
# File 'lib/generators/superpowers/scaffold/scaffold_generator.rb', line 49 def scaffold_views invoke "erb:scaffold", [singular_name, migration_attributes], behavior: self.behavior end |