Class: MotionPrime::AssociationCollection

Inherits:
Array
  • Object
show all
Includes:
FilterMixin
Defined in:
motion-prime/models/association_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FilterMixin

#filter_array

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
# File 'motion-prime/models/association_collection.rb', line 8

def initialize(bag, options, *args)
  @bag = bag
  @association_name = options[:association_name]
  bag.bare_class = model_class

  inverse_relation_options = options[:inverse_relation]
  define_inverse_relation(inverse_relation_options)

  @model_inverse_relation_name = (model_class._associations || {}).find do |name, options|
    options[:class_name] == inverse_relation.class_name_without_kvo
  end.try(:first)

  data = bag.store.present? ? find(*args) : filter(*args)
  super data
end

Instance Attribute Details

#association_nameObject (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

#bagObject (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_keyObject (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_nameObject (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_inverse_relation_nameObject (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)

Returns:

  • MotionPrime::Model model



43
44
45
46
47
# File 'motion-prime/models/association_collection.rb', line 43

def add(record)
  set_inverse_relation_for(record)
  self.bag << record
  record
end

#allObject Also known as: to_a

Return all association records.

@example:

project.users.all


56
57
58
59
60
# File 'motion-prime/models/association_collection.rb', line 56

def all
  data = bag.to_a
  set_inverse_relation_for(data)
  data
end

#delete_allObject

Remove all association records.

@example:

project.users.delete_all


102
103
104
# File 'motion-prime/models/association_collection.rb', line 102

def delete_all
  all.each { |obj| obj.delete }
end

#filter(find_options = {}, sort_options = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'motion-prime/models/association_collection.rb', line 82

def filter(find_options = {}, sort_options = nil)
  find_options = build_find_options(find_options)
  sort_options = build_sort_options(sort_options)

  data = filter_array(bag.to_a, find_options, sort_options)

  set_inverse_relation_for(data)
  data
end

#find(find_options = {}, sort_options = nil) ⇒ Object

Find association records.

@example:

project.users.find(age: 10)


71
72
73
74
75
76
77
78
79
80
# File 'motion-prime/models/association_collection.rb', line 71

def find(find_options = {}, sort_options = nil)
  raise "Use `filter` method when bag has not been saved yet" unless bag.store.present?

  find_options = build_find_options(find_options)
  sort_options = build_sort_options(sort_options)

  data = bag.find(find_options, sort_options)
  set_inverse_relation_for(data)
  data
end

#model_classObject



92
93
94
# File 'motion-prime/models/association_collection.rb', line 92

def model_class
  @model_class ||= @association_name.classify.constantize
end

#new(attributes = {}) ⇒ Object

Initialize a new object and add to collection.

@example:

project.users.new(name: "Bob", age: 10)

Returns:

  • MotionPrime::Model unsaved model



31
32
33
34
# File 'motion-prime/models/association_collection.rb', line 31

def new(attributes = {})
  record = model_class.new(attributes)
  add(record)
end