class NinjaModel::Associations::CollectionAssociation
Attributes
proxy[R]
Public Class Methods
new(owner, reflection)
click to toggle source
Calls superclass method
NinjaModel::Associations::Association::new
# File lib/ninja_model/associations/collection_association.rb, line 8 def initialize(owner, reflection) super @proxy = CollectionProxy.new(self) end
Public Instance Methods
add_to_target(record) { |record| ... }
click to toggle source
def create!(attributes = {}, options = {}, &block)
create_record(attributes, options, true, &block)
end
# File lib/ninja_model/associations/collection_association.rb, line 59 def add_to_target(record) yield(record) if block_given? @target << record record end
build(attributes = {}, options = {}) { |record| ... }
click to toggle source
# File lib/ninja_model/associations/collection_association.rb, line 41 def build(attributes = {}, options = {}, &block) if attributes.is_a?(Array) attributes.collect { |attr| build(attr, options, &block) } else add_to_target(build_record(attributes, options)) do |record| yield(record) if block_given? end end end
first(*args)
click to toggle source
def find(*args)
if block_given? load_target.find(*args) { |*block_args| yield(*block_args) } else scoped.find(*args) end
end
# File lib/ninja_model/associations/collection_association.rb, line 33 def first(*args) first_or_last(:first, *args) end
last(*args)
click to toggle source
# File lib/ninja_model/associations/collection_association.rb, line 37 def last(*args) first_or_last(:last, *args) end
reader(force_reload = false)
click to toggle source
# File lib/ninja_model/associations/collection_association.rb, line 13 def reader(force_reload = false) if force_reload klass.uncached { reload } end proxy end
reset()
click to toggle source
# File lib/ninja_model/associations/collection_association.rb, line 20 def reset @loaded = false @target = [] end
Private Instance Methods
create_scope()
click to toggle source
def insert_record(record, validate = true, raise = false)
raise NotImplementedError
end
# File lib/ninja_model/associations/collection_association.rb, line 91 def create_scope scoped.scope_for_create.stringify_keys end
find_target()
click to toggle source
# File lib/ninja_model/associations/collection_association.rb, line 67 def find_target puts "find_target for #{self}" scoped.all end
first_or_last(type, *args)
click to toggle source
# File lib/ninja_model/associations/collection_association.rb, line 95 def first_or_last(type, *args) args.shift if args.first.is_a?(Hash) && args.first.empty? collection = scoped.all collection.send(type, *args) end