class FactoryGirl::Strategy::Stub

Public Instance Methods

association(runner) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 6
def association(runner)
  runner.run(:build_stubbed)
end
result(evaluation) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 10
def result(evaluation)
  evaluation.object.tap do |instance|
    stub_database_interaction_on_result(instance)
    clear_changed_attributes_on_result(instance)
    evaluation.notify(:after_stub, instance)
  end
end

Private Instance Methods

clear_changed_attributes_on_result(result_instance) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 81
def clear_changed_attributes_on_result(result_instance)
  unless result_instance.respond_to?(:clear_changes_information)
    result_instance.extend ActiveModelDirtyBackport
  end

  result_instance.clear_changes_information
end
connection() click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 44
def connection
  raise "stubbed models are not allowed to access the database - #{self.class.to_s}#connection()"
end
created_at() click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 74
def created_at
  @created_at ||= Time.now.in_time_zone
end
decrement!(*args) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 64
def decrement!(*args)
  raise "stubbed models are not allowed to access the database - #{self.class}#decrement!(#{args.join(',')})"
end
destroy(*args) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 40
def destroy(*args)
  raise "stubbed models are not allowed to access the database - #{self.class.to_s}#destroy(#{args.join(",")})"
end
increment!(*args) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 60
def increment!(*args)
  raise "stubbed models are not allowed to access the database - #{self.class}#increment!(#{args.join(',')})"
end
new_record?() click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 32
def new_record?
  id.nil?
end
next_id() click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 20
def next_id
  @@next_id += 1
end
persisted?() click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 28
def persisted?
  !new_record?
end
reload(*args) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 48
def reload(*args)
  raise "stubbed models are not allowed to access the database - #{self.class.to_s}#reload()"
end
save(*args) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 36
def save(*args)
  raise "stubbed models are not allowed to access the database - #{self.class.to_s}#save(#{args.join(",")})"
end
stub_database_interaction_on_result(result_instance) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 24
def stub_database_interaction_on_result(result_instance)
  result_instance.id ||= next_id

  result_instance.instance_eval do
    def persisted?
      !new_record?
    end

    def new_record?
      id.nil?
    end

    def save(*args)
      raise "stubbed models are not allowed to access the database - #{self.class.to_s}#save(#{args.join(",")})"
    end

    def destroy(*args)
      raise "stubbed models are not allowed to access the database - #{self.class.to_s}#destroy(#{args.join(",")})"
    end

    def connection
      raise "stubbed models are not allowed to access the database - #{self.class.to_s}#connection()"
    end

    def reload(*args)
      raise "stubbed models are not allowed to access the database - #{self.class.to_s}#reload()"
    end

    def update_attribute(*args)
      raise "stubbed models are not allowed to access the database - #{self.class.to_s}#update_attribute(#{args.join(",")})"
    end

    def update_column(*args)
      raise "stubbed models are not allowed to access the database - #{self.class.to_s}#update_column(#{args.join(",")})"
    end

    def increment!(*args)
      raise "stubbed models are not allowed to access the database - #{self.class}#increment!(#{args.join(',')})"
    end

    def decrement!(*args)
      raise "stubbed models are not allowed to access the database - #{self.class}#decrement!(#{args.join(',')})"
    end
  end

  created_at_missing_default = result_instance.respond_to?(:created_at) && !result_instance.created_at
  result_instance_missing_created_at = !result_instance.respond_to?(:created_at)

  if created_at_missing_default || result_instance_missing_created_at
    result_instance.instance_eval do
      def created_at
        @created_at ||= Time.now.in_time_zone
      end
    end
  end
end
update_attribute(*args) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 52
def update_attribute(*args)
  raise "stubbed models are not allowed to access the database - #{self.class.to_s}#update_attribute(#{args.join(",")})"
end
update_column(*args) click to toggle source
# File lib/factory_girl/strategy/stub.rb, line 56
def update_column(*args)
  raise "stubbed models are not allowed to access the database - #{self.class.to_s}#update_column(#{args.join(",")})"
end