Module: CaseForm::ElementExt::Associationable
- Included in:
- CaseForm::Element::CollectionInput, CaseForm::Element::GeneratorHandle, CaseForm::Element::NestedModel, CaseForm::Element::SimpleError
- Defined in:
- lib/case_form/element_ext/associationable.rb
Instance Method Summary collapse
- #association ⇒ Object
- #association_class ⇒ Object
- #association_human_model_name ⇒ Object
- #association_method ⇒ Object
- #association_type?(type) ⇒ Boolean
- #associationable? ⇒ Boolean
- #collection_association? ⇒ Boolean
- #one_to_one_association? ⇒ Boolean
- #specific_method ⇒ Object
- #validate_nested_attributes_association(method, object) ⇒ Object
Instance Method Details
#association ⇒ Object
8 9 10 |
# File 'lib/case_form/element_ext/associationable.rb', line 8 def association object.class.reflect_on_association(method) if associationable? end |
#association_class ⇒ Object
12 13 14 |
# File 'lib/case_form/element_ext/associationable.rb', line 12 def association_class association.klass if association end |
#association_human_model_name ⇒ Object
41 42 43 |
# File 'lib/case_form/element_ext/associationable.rb', line 41 def association_human_model_name association_class.human_name end |
#association_method ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/case_form/element_ext/associationable.rb', line 20 def association_method case association.macro when :belongs_to :"#{method.to_s}_id" when :has_one method when :has_many :"#{method.to_s.singularize}_ids" end end |
#association_type?(type) ⇒ Boolean
16 17 18 |
# File 'lib/case_form/element_ext/associationable.rb', line 16 def association_type?(type) association and association.macro == type end |
#associationable? ⇒ Boolean
4 5 6 |
# File 'lib/case_form/element_ext/associationable.rb', line 4 def associationable? object.class.respond_to?(:reflect_on_association) end |
#collection_association? ⇒ Boolean
49 50 51 |
# File 'lib/case_form/element_ext/associationable.rb', line 49 def collection_association? association_type?(:has_many) end |
#one_to_one_association? ⇒ Boolean
45 46 47 |
# File 'lib/case_form/element_ext/associationable.rb', line 45 def one_to_one_association? association_type?(:belongs_to) || association_type?(:has_one) end |
#specific_method ⇒ Object
31 32 33 |
# File 'lib/case_form/element_ext/associationable.rb', line 31 def specific_method association ? association_method : method end |
#validate_nested_attributes_association(method, object) ⇒ Object
35 36 37 38 39 |
# File 'lib/case_form/element_ext/associationable.rb', line 35 def validate_nested_attributes_association(method, object) raise(NoMethodError, "Not defined :accepts_nested_attributes_for method " + "for :#{method} association in #{object.class} model!") unless object.respond_to?(:"#{method}_attributes=") method end |