Module: ViewMapper::HasManyChildModels
- Included in:
- HasManyExistingView, HasManyView
- Defined in:
- lib/view_mapper/has_many_child_models.rb
Instance Attribute Summary collapse
-
#child_models ⇒ Object
readonly
Returns the value of attribute child_models.
Instance Method Summary collapse
- #add_child_model_actions(m) ⇒ Object
- #add_child_model_forms(m) ⇒ Object
- #add_child_models_manifest(m) ⇒ Object
- #find_child_models ⇒ Object
- #is_child_model_action?(action) ⇒ Boolean
- #is_model_dependency_action(action) ⇒ Object
- #is_view_show(action) ⇒ Object
- #validate_child_model(child_model, parent_model_name, parent_model) ⇒ Object
- #validate_child_model_associations(child_model) ⇒ Object
- #validate_child_models ⇒ Object
Instance Attribute Details
#child_models ⇒ Object (readonly)
Returns the value of attribute child_models.
4 5 6 |
# File 'lib/view_mapper/has_many_child_models.rb', line 4 def child_models @child_models end |
Instance Method Details
#add_child_model_actions(m) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/view_mapper/has_many_child_models.rb', line 46 def add_child_model_actions(m) m.directory(File.join('test/fixtures', class_path)) m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb") m.template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb") unless [:skip_fixture] m.template 'fixtures.yml', File.join('test/fixtures', "#{table_name}.yml") end unless [:skip_migration] m.migration_template 'migration.rb', 'db/migrate', :assigns => { :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}" }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}" end end |
#add_child_model_forms(m) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/view_mapper/has_many_child_models.rb', line 24 def add_child_model_forms(m) m.template( "view_show.html.erb", File.join('app/views', controller_class_path, controller_file_name, "show.html.erb"), { :assigns => { :child_models => child_models } } ) m.template( "view_form.html.erb", File.join('app/views', controller_class_path, controller_file_name, "_form.html.erb") ) child_models.each do |child_model| m.template( "view_child_form.html.erb", File.join('app/views', controller_class_path, controller_file_name, "_#{child_model.name.underscore}.html.erb"), { :assigns => { :child_model => child_model } } ) end m.file('nested_attributes.js', 'public/javascripts/nested_attributes.js') m end |
#add_child_models_manifest(m) ⇒ Object
18 19 20 21 22 |
# File 'lib/view_mapper/has_many_child_models.rb', line 18 def add_child_models_manifest(m) add_child_model_actions(m) if valid && !view_only? add_child_model_forms(m) if valid m end |
#find_child_models ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/view_mapper/has_many_child_models.rb', line 65 def find_child_models if view_param view_param.split(',').collect { |param| ModelInfo.new(param.singularize) } elsif view_only? model.child_models else [] end end |
#is_child_model_action?(action) ⇒ Boolean
6 7 8 |
# File 'lib/view_mapper/has_many_child_models.rb', line 6 def is_child_model_action?(action) is_model_dependency_action(action) || is_view_show(action) end |
#is_model_dependency_action(action) ⇒ Object
10 11 12 |
# File 'lib/view_mapper/has_many_child_models.rb', line 10 def is_model_dependency_action(action) action[0] == :dependency && action[1].include?('model') end |
#is_view_show(action) ⇒ Object
14 15 16 |
# File 'lib/view_mapper/has_many_child_models.rb', line 14 def is_view_show(action) action[0] == :template && action[1].include?('view_show.html.erb') end |
#validate_child_model(child_model, parent_model_name, parent_model) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/view_mapper/has_many_child_models.rb', line 90 def validate_child_model(child_model, parent_model_name, parent_model) if !child_model.valid? logger.error child_model.error return false elsif parent_model && !parent_model.accepts_nested_attributes_for?(child_model) logger.warning "Model #{parent_model.name} does not accept nested attributes for model #{child_model.name}." return false else if child_model.has_many?(parent_model_name.pluralize) || child_model.has_and_belongs_to_many?(parent_model_name.pluralize) true elsif !child_model.belongs_to?(parent_model_name) logger.warning "Model #{child_model.name} does not contain a belongs_to association for #{parent_model_name}." return false elsif !child_model.has_foreign_key_for?(parent_model_name) logger.warning "Model #{child_model.name} does not contain a foreign key for #{parent_model_name}." return false end end true end |
#validate_child_model_associations(child_model) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/view_mapper/has_many_child_models.rb', line 111 def validate_child_model_associations(child_model) if child_model.belongs_to?(class_name) true else child_model.has_many?(class_name.pluralize) || child_model.has_and_belongs_to_many?(class_name.pluralize) end end |
#validate_child_models ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/view_mapper/has_many_child_models.rb', line 75 def validate_child_models cms = child_models if cms.empty? if view_only? logger.error "No has_many associations exist in class #{model.name}." else logger.error "No has_many association specified." end return false end cms.reject! { |child_model| !validate_child_model(child_model, class_name, view_only? ? model : nil) } @child_models = cms !cms.empty? end |