Class: MassiveRecord::ORM::Relations::ProxyCollection

Inherits:
Proxy
  • Object
show all
Defined in:
lib/massive_record/orm/relations/proxy_collection.rb

Overview

Proxy class for relations representing a collection

Instance Attribute Summary

Attributes inherited from Proxy

#metadata, #proxy_owner, #proxy_target

Instance Method Summary collapse

Methods inherited from Proxy

#blank?, #initialize, #inspect, #is_a?, #loaded!, #loaded?, #method_missing, #reload, #respond_to?, #to_param

Constructor Details

This class inherits a constructor from MassiveRecord::ORM::Relations::Proxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MassiveRecord::ORM::Relations::Proxy

Instance Method Details

#delete(*records) ⇒ Object

Deletes record(s) from the collection



57
58
59
# File 'lib/massive_record/orm/relations/proxy_collection.rb', line 57

def delete(*records)
  delete_or_destroy *records, :delete
end

#delete_allObject

Deletes all records from the relationship. Does not destroy the records



75
76
77
78
79
80
# File 'lib/massive_record/orm/relations/proxy_collection.rb', line 75

def delete_all
  delete(load_proxy_target).tap do
    reset
    loaded!
  end
end

#destroy(*records) ⇒ Object

Destroy record(s) from the collection Each record will be asked to destroy itself as well



49
50
51
# File 'lib/massive_record/orm/relations/proxy_collection.rb', line 49

def destroy(*records)
  delete_or_destroy *records, :destroy
end

#destroy_allObject

Destroys all records



64
65
66
67
68
69
# File 'lib/massive_record/orm/relations/proxy_collection.rb', line 64

def destroy_all
  destroy(load_proxy_target).tap do
    reset
    loaded!
  end
end

#empty?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/massive_record/orm/relations/proxy_collection.rb', line 41

def empty?
  length == 0
end

#firstObject



37
38
39
# File 'lib/massive_record/orm/relations/proxy_collection.rb', line 37

def first
  limit(1).first
end

#load_proxy_target(options = {}) ⇒ Object

Loading proxy_targets will merge it with records found currently in proxy, to make sure we don’t remove any pushed proxy_targets only cause we load the proxy_targets.



14
15
16
17
18
19
# File 'lib/massive_record/orm/relations/proxy_collection.rb', line 14

def load_proxy_target(options = {})
  proxy_target_before_load = proxy_target
  proxy_target_after_load = super

  self.proxy_target = (proxy_target_before_load + proxy_target_after_load).uniq
end

#replace(*records) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/massive_record/orm/relations/proxy_collection.rb', line 26

def replace(*records)
  records.flatten!

  if records.length == 1 and records.first.nil?
    reset
  else
    delete_all
    concat(records)
  end
end

#reset(force = false) ⇒ Object



21
22
23
24
# File 'lib/massive_record/orm/relations/proxy_collection.rb', line 21

def reset(force = false)
  super
  @proxy_target = []
end