Class: MDWA::Generators::ModelAssociation
- Inherits:
-
Object
- Object
- MDWA::Generators::ModelAssociation
- Defined in:
- lib/mdwa/generators/model_association.rb
Constant Summary collapse
- ACCEPTED_RELATIONS =
[:has_many, :belongs_to, :has_and_belongs_to_many, :nested_many, :nested_one, :has_one]
Instance Attribute Summary collapse
-
#composition ⇒ Object
Returns the value of attribute composition.
-
#model1 ⇒ Object
Returns the value of attribute model1.
-
#model2 ⇒ Object
Returns the value of attribute model2.
-
#reference_field ⇒ Object
Returns the value of attribute reference_field.
-
#relation ⇒ Object
Returns the value of attribute relation.
-
#skip_views ⇒ Object
Returns the value of attribute skip_views.
Instance Method Summary collapse
- #belongs_to? ⇒ Boolean
- #composition? ⇒ Boolean
- #filter_input(destination_field) ⇒ Object
- #has_and_belongs_to_many? ⇒ Boolean
- #has_many? ⇒ Boolean
- #has_one? ⇒ Boolean
-
#initialize(model1_name, model2_name, relation_name, reference_field = nil) ⇒ ModelAssociation
constructor
A new instance of ModelAssociation.
- #nested? ⇒ Boolean
- #nested_many? ⇒ Boolean
- #nested_one? ⇒ Boolean
- #ordered ⇒ Object
- #relation_valid? ⇒ Boolean
- #skip_views? ⇒ Boolean
Constructor Details
#initialize(model1_name, model2_name, relation_name, reference_field = nil) ⇒ ModelAssociation
Returns a new instance of ModelAssociation.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/mdwa/generators/model_association.rb', line 13 def initialize(model1_name, model2_name, relation_name, reference_field = nil) self.model1 = model1_name self.model2 = model2_name self.relation = relation_name self.reference_field = reference_field || 'id' self.composition = false self.skip_views = false # validation raise "Invalid model name: #{@model1.name}" unless self.model1.valid? raise "Invalid model name: #{@model2.name}" unless self.model2.valid? raise "Invalid relation type: #{@relation}" unless self.relation_valid? end |
Instance Attribute Details
#composition ⇒ Object
Returns the value of attribute composition.
9 10 11 |
# File 'lib/mdwa/generators/model_association.rb', line 9 def composition @composition end |
#model1 ⇒ Object
Returns the value of attribute model1.
9 10 11 |
# File 'lib/mdwa/generators/model_association.rb', line 9 def model1 @model1 end |
#model2 ⇒ Object
Returns the value of attribute model2.
9 10 11 |
# File 'lib/mdwa/generators/model_association.rb', line 9 def model2 @model2 end |
#reference_field ⇒ Object
Returns the value of attribute reference_field.
9 10 11 |
# File 'lib/mdwa/generators/model_association.rb', line 9 def reference_field @reference_field end |
#relation ⇒ Object
Returns the value of attribute relation.
9 10 11 |
# File 'lib/mdwa/generators/model_association.rb', line 9 def relation @relation end |
#skip_views ⇒ Object
Returns the value of attribute skip_views.
9 10 11 |
# File 'lib/mdwa/generators/model_association.rb', line 9 def skip_views @skip_views end |
Instance Method Details
#belongs_to? ⇒ Boolean
60 61 62 |
# File 'lib/mdwa/generators/model_association.rb', line 60 def belongs_to? return (relation == 'belongs_to') end |
#composition? ⇒ Boolean
44 45 46 |
# File 'lib/mdwa/generators/model_association.rb', line 44 def composition? self.composition end |
#filter_input(destination_field) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/mdwa/generators/model_association.rb', line 89 def filter_input(destination_field) input = [] input << "render '/template/mdwa/autocomplete_tag', :dom_element => '##{self.model2.name.underscore}', :dom_element_id => '##{self.model2.name.underscore}_id', :data => #{model2.klass}.all, :field_name => '#{destination_field}'" input << "text_field_tag :#{self.model2.name.underscore}" input << "hidden_field_tag :#{self.model2.name.underscore}_id" return input end |
#has_and_belongs_to_many? ⇒ Boolean
84 85 86 |
# File 'lib/mdwa/generators/model_association.rb', line 84 def has_and_belongs_to_many? return (relation == 'has_and_belongs_to_many') end |
#has_many? ⇒ Boolean
64 65 66 |
# File 'lib/mdwa/generators/model_association.rb', line 64 def has_many? return (relation == 'has_many') end |
#has_one? ⇒ Boolean
80 81 82 |
# File 'lib/mdwa/generators/model_association.rb', line 80 def has_one? return (relation == 'has_one') end |
#nested? ⇒ Boolean
72 73 74 |
# File 'lib/mdwa/generators/model_association.rb', line 72 def nested? nested_many? || nested_one? end |
#nested_many? ⇒ Boolean
68 69 70 |
# File 'lib/mdwa/generators/model_association.rb', line 68 def nested_many? return (relation == 'nested_many') end |
#nested_one? ⇒ Boolean
76 77 78 |
# File 'lib/mdwa/generators/model_association.rb', line 76 def nested_one? return (relation == 'nested_one') end |
#ordered ⇒ Object
56 57 58 |
# File 'lib/mdwa/generators/model_association.rb', line 56 def ordered [@model1, @model2].sort! {|a,b| a.plural_name <=> b.plural_name} end |
#relation_valid? ⇒ Boolean
52 53 54 |
# File 'lib/mdwa/generators/model_association.rb', line 52 def relation_valid? ACCEPTED_RELATIONS.include? relation.to_sym end |
#skip_views? ⇒ Boolean
48 49 50 |
# File 'lib/mdwa/generators/model_association.rb', line 48 def skip_views? self.skip_views end |