Class: ScaffoldPlus::Generators::HasManyGenerator
- Inherits:
-
ActiveRecord::Generators::Base
- Object
- ActiveRecord::Generators::Base
- ScaffoldPlus::Generators::HasManyGenerator
- Defined in:
- lib/generators/scaffold_plus/has_many/has_many_generator.rb
Instance Method Summary collapse
- #add_counter ⇒ Object
- #add_migration ⇒ Object
- #add_to_models ⇒ Object
- #add_to_permit ⇒ Object
- #add_to_route ⇒ Object
- #update_child_controller_and_view ⇒ Object
Instance Method Details
#add_counter ⇒ Object
38 39 40 41 |
# File 'lib/generators/scaffold_plus/has_many/has_many_generator.rb', line 38 def add_counter return unless .counter? migration_template 'counter_migration.rb', "db/migrate/#{counter_migration}.rb" end |
#add_migration ⇒ Object
33 34 35 36 |
# File 'lib/generators/scaffold_plus/has_many/has_many_generator.rb', line 33 def add_migration return unless .migration? migration_template 'child_migration.rb', "db/migrate/#{migration_name}.rb" end |
#add_to_models ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/generators/scaffold_plus/has_many/has_many_generator.rb', line 53 def add_to_models inject_into_class "app/models/#{name}.rb", class_name do text = before_array.include?(name) ? "\n" : "" text << " has_many :#{children}" text << ", inverse_of: :#{name}" if .inverse? text << ", dependent: :#{dependent}" if .dependent.present? text << "\n" if .nested.present? text << " accepts_nested_attributes_for :#{children}\n" end text << "\n" if after_array.include?(name) text end child = children.singularize inject_into_class "app/models/#{child}.rb", child.camelize do text = before_array.include?(child) ? "\n" : "" text << " belongs_to :#{name}" if .foreign_key.present? text << ", foreign_key: \"#{.foreign_key}\"" end text << ", inverse_of: :#{children}" if .inverse? text << ", counter_cache: true" if .counter? text << "\n" text << "\n" if after_array.include?(child) text end end |
#add_to_permit ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/generators/scaffold_plus/has_many/has_many_generator.rb', line 82 def add_to_permit return if .nested.blank? list = .nested.map{|n| ":#{n}"}.join(', ') text = "#{children}_attributes: [ #{list} ]" file = "app/controllers/#{table_name}_controller.rb" gsub_file file, /(permit\(.*)\)/, "\\1, #{text})" # Special case: no previous permit gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})" end |
#add_to_route ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/generators/scaffold_plus/has_many/has_many_generator.rb', line 43 def add_to_route return unless .route? gsub_file "config/routes.rb", /^ resources :#{table_name} do$/ do |match| match << "\n resources :#{children}" end gsub_file "config/routes.rb", /^ resources :#{table_name}$/ do |match| match << " do\n resources :#{children}\n end" end end |
#update_child_controller_and_view ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/generators/scaffold_plus/has_many/has_many_generator.rb', line 92 def update_child_controller_and_view return unless .route? child = children.singularize file = "app/controllers/#{children}_controller.rb" gsub_file file, /GET .#{children}.new$/ do |match| match = "GET /#{table_name}/:id/#{children}/new" end gsub_file file, /^ @#{child} = #{child.camelize}.new$/ do |match| match = " @#{name} = #{class_name}.find(params[:#{name}_id])\n" + " @#{child} = @#{name}.#{children}.build" end file = "app/views/#{children}/_form.html.erb" gsub_file file, /form_for\(@#{child}/ do |match| match = "form_for([@#{name}, @#{child}]" end end |