Class: MotionPrime::AssociationCollection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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, 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)

  super all(*args)
end

Instance Attribute Details

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

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

#add(record) ⇒ Object

Add model record to association collection.

@example:

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

Returns:

  • MotionPrime::Model model



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

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

#all(find_options = nil, sort_options = nil) ⇒ Object

Return all association records.

@example:

project.users.all
project.users.all(age: 10)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'motion-prime/models/association_collection.rb', line 58

def all(find_options = nil, sort_options = nil)
  find_options = build_find_options(find_options)
  sort_options = build_sort_options(sort_options)

  data = if bag.store.present?
    bag.find(find_options, sort_options)
  else
    bag.to_a.select do |entity| 
      find_options.all? { |field, value| entity.info[field] == value }
    end
  end
  set_inverse_relation_for(data)
  data
end

#delete_allObject

Remove all association records.

@example:

project.users.delete_all


83
84
85
# File 'motion-prime/models/association_collection.rb', line 83

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

#model_classObject



73
74
75
# File 'motion-prime/models/association_collection.rb', line 73

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



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

def new(attributes = {})
  record = model_class.new(attributes).tap do |model|
    set_inverse_relation_for(model)
  end
  add(record)
end