Class: MotionPrime::AssociationCollection
- Inherits:
-
Array
- Object
- Array
- MotionPrime::AssociationCollection
- Defined in:
- motion-prime/models/association_collection.rb
Instance Attribute Summary collapse
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#bag ⇒ Object
readonly
Returns the value of attribute bag.
-
#inverse_relation_key ⇒ Object
readonly
Returns the value of attribute inverse_relation_key.
-
#inverse_relation_name ⇒ Object
readonly
Returns the value of attribute inverse_relation_name.
-
#model_inverse_relation_name ⇒ Object
readonly
Returns the value of attribute model_inverse_relation_name.
Instance Method Summary collapse
- #all(*args) ⇒ Object
- #define_inverse_relation(options) ⇒ Object
- #delete_all ⇒ Object
- #find_options(options) ⇒ Object
-
#initialize(bag, options, *args) ⇒ AssociationCollection
constructor
A new instance of AssociationCollection.
- #model_class ⇒ Object
- #new(attributes = {}) ⇒ Object
- #set_inverse_relation_for(models) ⇒ Object
- #sort_options(options) ⇒ Object
Constructor Details
#initialize(bag, options, *args) ⇒ AssociationCollection
Returns a new instance of AssociationCollection.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'motion-prime/models/association_collection.rb', line 8 def initialize(bag, , *args) @bag = bag @association_name = [:association_name] bag. = model_class = [:inverse_relation] define_inverse_relation() @model_inverse_relation_name = (model_class._associations || {}).find do |name, | [:class_name] == inverse_relation.class_name_without_kvo end.try(:first) super all(*args) end |
Instance Attribute Details
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
3 4 5 |
# File 'motion-prime/models/association_collection.rb', line 3 def association_name @association_name end |
#bag ⇒ Object (readonly)
Returns the value of attribute bag.
3 4 5 |
# File 'motion-prime/models/association_collection.rb', line 3 def bag @bag end |
#inverse_relation_key ⇒ Object (readonly)
Returns the value of attribute inverse_relation_key.
4 5 6 |
# File 'motion-prime/models/association_collection.rb', line 4 def inverse_relation_key @inverse_relation_key end |
#inverse_relation_name ⇒ Object (readonly)
Returns the value of attribute inverse_relation_name.
4 5 6 |
# File 'motion-prime/models/association_collection.rb', line 4 def inverse_relation_name @inverse_relation_name end |
#model_inverse_relation_name ⇒ Object (readonly)
Returns the value of attribute model_inverse_relation_name.
4 5 6 |
# File 'motion-prime/models/association_collection.rb', line 4 def model_inverse_relation_name @model_inverse_relation_name end |
Instance Method Details
#all(*args) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'motion-prime/models/association_collection.rb', line 42 def all(*args) return [] unless bag.store.present? = (args[0]) = (args[1]) data = if .present? # TODO: check bag items count bag.find(, ) else bag.to_a.select { |entity| .all? { |field, value| entity.send(field) == value } } end set_inverse_relation_for(data) data end |
#define_inverse_relation(options) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'motion-prime/models/association_collection.rb', line 29 def define_inverse_relation() # TODO: handle different relation types (habtm, has_one...) @inverse_relation_name = name = [:name].to_sym self.class_eval do define_method name do [:instance] end alias_method :inverse_relation, name end @inverse_relation_key = inverse_relation._associations[association_name][:foreign_key].try(:to_sym) end |
#delete_all ⇒ Object
79 80 81 |
# File 'motion-prime/models/association_collection.rb', line 79 def delete_all all.each { |obj| obj.delete } end |
#find_options(options) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'motion-prime/models/association_collection.rb', line 61 def () ||= {} .merge!(bag_key: bag.key) if inverse_relation_key.present? {inverse_relation_key => inverse_relation.id}.merge else end end |
#model_class ⇒ Object
75 76 77 |
# File 'motion-prime/models/association_collection.rb', line 75 def model_class @model_class ||= @association_name.classify.constantize end |
#new(attributes = {}) ⇒ Object
23 24 25 26 27 |
# File 'motion-prime/models/association_collection.rb', line 23 def new(attributes = {}) model_class.new(attributes).tap do |model| set_inverse_relation_for(model) end end |
#set_inverse_relation_for(models) ⇒ Object
55 56 57 58 59 |
# File 'motion-prime/models/association_collection.rb', line 55 def set_inverse_relation_for(models) [*models].each do |model| model.send("#{inverse_relation_name}=", inverse_relation) end if model_inverse_relation_name.present? end |
#sort_options(options) ⇒ Object
71 72 73 |
# File 'motion-prime/models/association_collection.rb', line 71 def () || {sort: model_class.} end |