Module: Para::FormBuilder::NestedForm
- Defined in:
- lib/para/form_builder/nested_form.rb
Instance Method Summary collapse
- #allow_destroy? ⇒ Boolean
- #inverse_of?(field_name) ⇒ Boolean
- #nested? ⇒ Boolean
- #nested_attribute_name ⇒ Object
-
#nested_fields ⇒ Object
FIXME : When we have a nested field that maps to an STI model, the _id field is passed instead of the relation, and the inverse_of guard doesn’t work.
- #nested_resource_dom_id ⇒ Object
- #nested_resource_name ⇒ Object
- #parent_object ⇒ Object
- #remove_association_button ⇒ Object
Instance Method Details
#allow_destroy? ⇒ Boolean
49 50 51 52 53 54 55 |
# File 'lib/para/form_builder/nested_form.rb', line 49 def allow_destroy? return false unless nested? = parent_object. relation = [nested_attribute_name] relation && relation[:allow_destroy] end |
#inverse_of?(field_name) ⇒ Boolean
57 58 59 60 61 62 |
# File 'lib/para/form_builder/nested_form.rb', line 57 def inverse_of?(field_name) return false unless nested? reflection = parent_object.class.reflect_on_association(nested_attribute_name) reflection && (reflection.[:inverse_of].to_s == field_name.to_s) end |
#nested? ⇒ Boolean
64 65 66 |
# File 'lib/para/form_builder/nested_form.rb', line 64 def nested? nested_attribute_name.present? && [:parent_builder] end |
#nested_attribute_name ⇒ Object
68 69 70 71 72 73 |
# File 'lib/para/form_builder/nested_form.rb', line 68 def nested_attribute_name [:nested_attribute_name] ||= if (match = object_name.match(/\[(\w+)_attributes\]/)) match[1] end end |
#nested_fields ⇒ Object
FIXME : When we have a nested field that maps to an STI model, the _id
field is passed instead of the relation, and the inverse_of
guard doesn't work
8 9 10 11 12 |
# File 'lib/para/form_builder/nested_form.rb', line 8 def nested_fields @nested_fields ||= fields.reject do |field| inverse_of?(field.name) end end |
#nested_resource_dom_id ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/para/form_builder/nested_form.rb', line 27 def nested_resource_dom_id return "" unless nested? @nested_resource_dom_id ||= [ object.class.model_name.singular, (Time.now.to_f * 1000).to_i, (object.id || "_new_#{ nested_attribute_name }_id") ].join('-') end |
#nested_resource_name ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/para/form_builder/nested_form.rb', line 14 def nested_resource_name @nested_resource_name ||= begin name_method = Para.config.resource_name_methods.find do |method_name| object.respond_to?(method_name) && object.try(method_name).present? end name = (name_method && object.send(name_method)) || default_resource_name name = name.to_s.gsub(/(<\/p>|<br\s*\/?>)/, " ") template.sanitize(name, tags: []) end end |
#parent_object ⇒ Object
75 76 77 |
# File 'lib/para/form_builder/nested_form.rb', line 75 def parent_object nested? && [:parent_builder].object end |
#remove_association_button ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/para/form_builder/nested_form.rb', line 37 def return "" unless allow_destroy? template.content_tag(:div, class: 'panel-btns') do template.link_to_remove_association( self, wrapper_class: 'form-fields', class: 'btn btn-danger btn-xs' ) do ::I18n.t('para.form.nested.remove') end end end |