class ScaffoldPlus::Generators::HasOneGenerator
Public Instance Methods
add_migration()
click to toggle source
# File lib/generators/scaffold_plus/has_one/has_one_generator.rb, line 33 def add_migration return unless options.migration? migration_template 'child_migration.rb', "db/migrate/#{migration_name}.rb" end
add_to_models()
click to toggle source
# File lib/generators/scaffold_plus/has_one/has_one_generator.rb, line 48 def add_to_models inject_into_class "app/models/#{name}.rb", class_name do text = before_array.include?(name) ? "\n" : "" text << " has_one :#{child}" text << ", inverse_of: :#{name}" if options.inverse? text << ", dependent: :#{dependent}" if options.dependent.present? text << "\n" if options.nested.present? text << " accepts_nested_attributes_for :#{child}, allow_destroy: true\n" end text << "\n" if after_array.include?(name) text end inject_into_class "app/models/#{child}.rb", child.camelize do text = before_array.include?(child) ? "\n" : "" text << " belongs_to :#{name}" if options.foreign_key text << ", foreign_key: \"#{options.foreign_key}\"" end text << ", inverse_of: :#{child}" if options.inverse? text << "\n" text << "\n" if after_array.include?(child) text end end
add_to_route()
click to toggle source
# File lib/generators/scaffold_plus/has_one/has_one_generator.rb, line 38 def add_to_route return unless options.route? gsub_file "config/routes.rb", /^ resources :#{table_name} do$/ do |match| match << "\n resources :#{child.pluralize}" end gsub_file "config/routes.rb", /^ resources :#{table_name}$/ do |match| match << " do\n resources :#{child.pluralize}\n end" end end
update_child_controller()
click to toggle source
# File lib/generators/scaffold_plus/has_one/has_one_generator.rb, line 86 def update_child_controller return unless options.permit? text = ":#{name}_id" file = "app/controllers/#{child.pluralize}_controller.rb" return unless File.exist?(file) gsub_file file, /(permit\(.*)\)/, "\\1, #{text})" # Special case: no previous permit gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})" end
update_nested_resource()
click to toggle source
# File lib/generators/scaffold_plus/has_one/has_one_generator.rb, line 96 def update_nested_resource return unless options.route? children = child.pluralize file = "app/controllers/#{children}_controller.rb" if File.exist?(file) 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 end file = "app/views/#{children}/_form.html.erb" if File.exist?(file) gsub_file file, /form_for\(@#{child}/ do |match| match = "form_for([@#{name}, @#{child}]" end end end
update_parent_controller()
click to toggle source
# File lib/generators/scaffold_plus/has_one/has_one_generator.rb, line 75 def update_parent_controller return if options.nested.blank? list = options.nested.map{|n| ":#{n}"}.join(', ') text = "#{child}_attributes: [ #{list} ]" file = "app/controllers/#{table_name}_controller.rb" return unless File.exist?(file) gsub_file file, /(permit\(.*)\)/, "\\1, #{text})" # Special case: no previous permit gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})" end
Protected Instance Methods
after_array()
click to toggle source
# File lib/generators/scaffold_plus/has_one/has_one_generator.rb, line 123 def after_array options.after || [] end
before_array()
click to toggle source
# File lib/generators/scaffold_plus/has_one/has_one_generator.rb, line 119 def before_array options.before || [] end
dependent()
click to toggle source
# File lib/generators/scaffold_plus/has_one/has_one_generator.rb, line 127 def dependent if options.dependent.present? && options.dependent == "restrict" "restrict_with_exception" else options.dependent end end
migration_name()
click to toggle source
# File lib/generators/scaffold_plus/has_one/has_one_generator.rb, line 135 def migration_name "add_#{name}_id_to_#{child}" end