class MiniModel::HasManyAssociation
Public Class Methods
new(owner, association_name, options = {})
click to toggle source
# File lib/minimodel/associations.rb, line 5 def initialize(owner, association_name, options = {}) @owner, @association_name, @options = owner, association_name, options end
Public Instance Methods
each(&block)
click to toggle source
# File lib/minimodel/associations.rb, line 9 def each(&block) collection.each(&block) end
size()
click to toggle source
# File lib/minimodel/associations.rb, line 13 def size arel_model? ? collection.size : collection.length end
Private Instance Methods
arel_model?()
click to toggle source
# File lib/minimodel/associations.rb, line 33 def arel_model? target_model.respond_to?(:where) end
collection()
click to toggle source
# File lib/minimodel/associations.rb, line 21 def collection @collection ||= begin owner_id = @owner.send(@owner.class.primary_key) if arel_model? target_model.where(foreign_key => owner_id) else target_model.all.select { |object| object.send(foreign_key) == owner_id } end end end
foreign_key()
click to toggle source
# File lib/minimodel/associations.rb, line 37 def foreign_key @foreign_key ||= '%s_%s' % [@owner.class.name.demodulize.underscore, @owner.class.primary_key] end
target_model()
click to toggle source
# File lib/minimodel/associations.rb, line 41 def target_model @target_model ||= @association_name.to_s.classify.constantize end