Class: Meek::CollectionProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/meek/collection_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, association) ⇒ CollectionProxy

Returns a new instance of CollectionProxy.



6
7
8
# File 'lib/meek/collection_proxy.rb', line 6

def initialize model, association
  @proxy_association = Meek::ProxyAssociation.new(model, association)
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



4
5
6
# File 'lib/meek/collection_proxy.rb', line 4

def model
  @model
end

#ownerObject

Returns the value of attribute owner.



4
5
6
# File 'lib/meek/collection_proxy.rb', line 4

def owner
  @owner
end

#proxy_associationObject

Returns the value of attribute proxy_association.



4
5
6
# File 'lib/meek/collection_proxy.rb', line 4

def proxy_association
  @proxy_association
end

Instance Method Details

#<<(*records) ⇒ Object Also known as: push, append



22
23
24
25
# File 'lib/meek/collection_proxy.rb', line 22

def <<(*records)
  records.map {|record| record.save }
  proxy_association.concat(records) && self
end

#clearObject

Equivalent to delete_all. The difference is that returns self, instead of an array with the deleted objects, so methods can be chained. See delete_all for more information. Note that because delete_all removes records by directly running an SQL query into the database, the updated_at column of the object is not changed.



39
40
41
42
# File 'lib/meek/collection_proxy.rb', line 39

def clear
  delete_all
  self
end

#concat(new) ⇒ Object

replace, delete_all, destroy_all, delete, destroy, distinct, uniq,



18
19
20
# File 'lib/meek/collection_proxy.rb', line 18

def concat new
  proxy_association.concat(new)
end

#prepend(*args) ⇒ Object

Raises:

  • (NoMethodError)


29
30
31
# File 'lib/meek/collection_proxy.rb', line 29

def prepend(*args)
  raise NoMethodError, "prepend on association is not defined. Please use << or append"
end

#reloadObject

Reloads the collection from the database. Returns self.



45
46
47
48
# File 'lib/meek/collection_proxy.rb', line 45

def reload
  proxy_association.reload
  self
end

#resetObject

Unloads the association. Returns self.



51
52
53
54
55
# File 'lib/meek/collection_proxy.rb', line 51

def reset
  proxy_association.reset
  proxy_association.reset_scope
  self
end