class ActiveMongoid::Associations::Many
Public Class Methods
new(base, target, metadata)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 11 def initialize(base, target, metadata) init(base, ActiveMongoid::Associations::Targets::Enumerable.new(target), metadata) do end end
Public Instance Methods
<<(*args)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 16 def <<(*args) objs = args.flatten return concat(objs) if objs.size > 1 if objs = objs.first append(objs) objs.save if base.persisted? && !_binding? end self end
Also aliased as: push
blank?()
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 106 def blank? size == 0 end
build(attributes = {}, type = nil) { |obj| ... }
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 27 def build(attributes = {}, type = nil) obj = (type || klass).new(attributes) append(obj) yield(obj) if block_given? obj end
Also aliased as: new
concat(objects)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 35 def concat(objects) objs, inserts = [], [] objects.each do |obj| next unless obj append(obj) save_or_delay(obj, objs, inserts) if base.persisted? end persist_delayed(objs, inserts) self end
create(attributes = nil, type = nil, &block)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 110 def create(attributes = nil, type = nil, &block) if attributes.is_a?(::Array) attributes.map { |attrs| create(attrs, type, &block) } else obj = build(attributes, type, &block) base.persisted? ? obj.save : raise_unsaved(obj) obj end end
create!(attributes = nil, type = nil, &block)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 120 def create!(attributes = nil, type = nil, &block) if attributes.is_a?(::Array) attributes.map { |attrs| create!(attrs, type, &block) } else obj = build(attributes, type, &block) base.persisted? ? obj.save! : raise_unsaved(obj) obj end end
delete(object)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 61 def delete(object) target.delete(object) do |obj| unbind_one(obj) if obj cascade!(obj) if obj end end
delete_all(conditions = nil)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 68 def delete_all(conditions = nil) remove_all(conditions, :delete_all) end
destroy_all(conditions = nil)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 72 def destroy_all(conditions = nil) remove_all(conditions, :destroy_all) end
each() { |obj| ... }
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 76 def each if block_given? target.each { |obj| yield(obj) } else to_enum end end
exists?()
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 84 def exists? criteria.exists? end
find(*args)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 88 def find(*args) begin matching = criteria.find(*args) rescue Exception => e end Array(matching).each { |obj| target.push(obj) } matching end
find_or_create_by(attrs = {}, type = nil, &block)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 130 def find_or_create_by(attrs = {}, type = nil, &block) find_or(:create, attrs, type, &block) end
find_or_initialize_by(attrs = {}, type = nil, &block)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 134 def find_or_initialize_by(attrs = {}, type = nil, &block) find_or(:build, attrs, type, &block) end
nil?()
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 138 def nil? false end
nullify()
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 97 def nullify criteria.update_all(__metadata__.foreign_key => nil) target.clear do |obj| unbind_one(obj) obj.changed_attributes.delete(__metadata__.foreign_key) end end
Also aliased as: nullify_all
purge()
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 46 def purge unless __metadata__.destructive? nullify else after_remove_error = nil criteria.delete_all many = target.clear do |obj| unbind_one(obj) obj.destroyed = true if obj.respond_to?(:destroyed=) end many end end
Also aliased as: clear
raise_unsaved(obj)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 155 def raise_unsaved(obj) # raise new exception end
respond_to?(name, include_private = false)
click to toggle source
Calls superclass method
# File lib/active_mongoid/associations/many.rb, line 142 def respond_to?(name, include_private = false) [].respond_to?(name, include_private) || klass.respond_to?(name, include_private) || super end
scoped()
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 147 def scoped criteria end
substitute(replacement)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 159 def substitute(replacement) if replacement new_objs, objs = replacement.compact, [] new_ids = new_objs.map { |obj| obj.id } remove_not_in(new_ids) new_objs.each do |obj| objs.push(obj) if obj.send(__metadata__.foreign_key) != base.id end concat(objs) else purge end self end
unscoped()
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 151 def unscoped criteria.unscoped end
Private Instance Methods
append(object)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 185 def append(object) target.push(object) # characterize_one(object) bind_one(object) end
cascade!(object)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 191 def cascade!(object) if base.persisted? if __metadata__.destructive? object.send(__metadata__.dependent) else object.save end end end
find_or(method, attrs = {}, type = nil, &block)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 180 def find_or(method, attrs = {}, type = nil, &block) attrs["_type"] = type.to_s if type where(attrs).first || send(method, attrs, type, &block) end
with_polymorphic_criterion(criteria, metadata, type = nil)
click to toggle source
# File lib/active_mongoid/associations/many.rb, line 201 def with_polymorphic_criterion(criteria, metadata, type = nil) if metadata.polymorphic? criteria.where(metadata.type => type.name) else criteria end end