Class: MotionPrime::AssociationCollection
- Inherits:
-
Array
- Object
- Array
- MotionPrime::AssociationCollection
- Includes:
- FilterMixin
- 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_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#model_inverse_relation_name ⇒ Object
readonly
Returns the value of attribute model_inverse_relation_name.
Instance Method Summary collapse
-
#add(record) ⇒ Object
(also: #<<)
Add model record to association collection.
-
#all ⇒ Object
(also: #to_a)
Return all association records.
-
#delete_all ⇒ Object
Remove all association records.
- #filter(find_options = {}, sort_options = nil) ⇒ Object
-
#find(find_options = {}, sort_options = nil) ⇒ Object
Find association records.
-
#initialize(bag, options, *args) ⇒ AssociationCollection
constructor
A new instance of AssociationCollection.
-
#new(attributes = {}) ⇒ Object
Initialize a new object and add to collection.
- #stored? ⇒ Boolean
Methods included from FilterMixin
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 22 23 24 25 |
# File 'motion-prime/models/association_collection.rb', line 8 def initialize(bag, , *args) @bag = bag @association_name = [:association_name] @model_class = [:class_name].constantize 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) # when use #find() nested children will be reallocated and their bags will be empty # data = stored? ? find(*args) : filter(*args) data = filter(*args) super data end |
Instance Attribute Details
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
5 6 7 |
# File 'motion-prime/models/association_collection.rb', line 5 def association_name @association_name end |
#bag ⇒ Object (readonly)
Returns the value of attribute bag.
5 6 7 |
# File 'motion-prime/models/association_collection.rb', line 5 def bag @bag end |
#inverse_relation_key ⇒ Object (readonly)
Returns the value of attribute inverse_relation_key.
6 7 8 |
# File 'motion-prime/models/association_collection.rb', line 6 def inverse_relation_key @inverse_relation_key end |
#inverse_relation_name ⇒ Object (readonly)
Returns the value of attribute inverse_relation_name.
6 7 8 |
# File 'motion-prime/models/association_collection.rb', line 6 def inverse_relation_name @inverse_relation_name end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
5 6 7 |
# File 'motion-prime/models/association_collection.rb', line 5 def model_class @model_class end |
#model_inverse_relation_name ⇒ Object (readonly)
Returns the value of attribute model_inverse_relation_name.
6 7 8 |
# File 'motion-prime/models/association_collection.rb', line 6 def model_inverse_relation_name @model_inverse_relation_name end |
Instance Method Details
#add(record) ⇒ Object Also known as: <<
Add model record to association collection.
@example:
project.users.new(name: "Bob", age: 10)
46 47 48 49 50 |
# File 'motion-prime/models/association_collection.rb', line 46 def add(record) set_inverse_relation_for(record) self.bag << record record end |
#all ⇒ Object Also known as: to_a
Return all association records.
@example:
project.users.all
59 60 61 62 63 |
# File 'motion-prime/models/association_collection.rb', line 59 def all data = bag.to_a set_inverse_relation_for(data) data end |
#delete_all ⇒ Object
Remove all association records.
@example:
project.users.delete_all
101 102 103 |
# File 'motion-prime/models/association_collection.rb', line 101 def delete_all all.each { |obj| obj.delete } end |
#filter(find_options = {}, sort_options = nil) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'motion-prime/models/association_collection.rb', line 85 def filter( = {}, = nil) = () = () data = filter_array(bag.to_a, , ) set_inverse_relation_for(data) data end |
#find(find_options = {}, sort_options = nil) ⇒ Object
Find association records.
@example:
project.users.find(age: 10)
74 75 76 77 78 79 80 81 82 83 |
# File 'motion-prime/models/association_collection.rb', line 74 def find( = {}, = nil) raise "Use `filter` method when bag has not been saved yet" unless stored? = () = () data = bag.find(, ) set_inverse_relation_for(data) data end |
#new(attributes = {}) ⇒ Object
Initialize a new object and add to collection.
@example:
project.users.new(name: "Bob", age: 10)
34 35 36 37 |
# File 'motion-prime/models/association_collection.rb', line 34 def new(attributes = {}) record = model_class.new(attributes) add(record) end |
#stored? ⇒ Boolean
105 106 107 |
# File 'motion-prime/models/association_collection.rb', line 105 def stored? bag.store.present? end |