class ActiveMocker::HasMany

Attributes

foreign_id[R]

@api private

foreign_key[R]

@api private

relation_class[R]

@api private

source[R]

@api private

Public Class Methods

new(collection, options = {}) click to toggle source
Calls superclass method
# File lib/active_mocker/mock/has_many.rb, line 11
def initialize(collection, options = {})
  @relation_class = options[:relation_class]
  @foreign_key    = options[:foreign_key]
  @foreign_id     = options[:foreign_id]
  @source         = options[:source]
  self.class.include "#{@relation_class.name}::Scopes".constantize
  super(collection)
  set_foreign_key
end

Private Class Methods

new(collection, options = {}) click to toggle source
Calls superclass method
# File lib/active_mocker/mock/has_many.rb, line 6
def self.new(collection, options = {})
  return Relation.new(collection) if options[:relation_class].nil?
  super(collection, options)
end

Private Instance Methods

build(options = {}, &block) click to toggle source
# File lib/active_mocker/mock/has_many.rb, line 30
def build(options = {}, &block)
  new_record = relation_class.new(init_options.merge!(options), &block)

  # @private
  def new_record._belongs_to(collection)
    @belongs_to_collection = collection
  end

  new_record._belongs_to(self)

  # @private
  def new_record.save
    @belongs_to_collection << self
    super
  end

  new_record
end
create(options = {}, &block) click to toggle source
# File lib/active_mocker/mock/has_many.rb, line 49
def create(options = {}, &block)
  created_record = relation_class.create(init_options.merge!(options), &block)
  collection << created_record
  created_record
end
Also aliased as: create!
create!(options = {}, &block)
Alias for: create
init_options() click to toggle source

@api private

# File lib/active_mocker/mock/has_many.rb, line 58
def init_options
  { foreign_key => foreign_id }
end
set_foreign_key() click to toggle source
# File lib/active_mocker/mock/has_many.rb, line 21
def set_foreign_key
  collection.each do |item|
    item.send(:write_attribute, foreign_key, foreign_id) if item.respond_to?("#{foreign_key}=")
  end
end