Class: ActiveRecord::Fixture

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
activerecord/lib/active_record/fixtures.rb

Overview

:nodoc:

Defined Under Namespace

Classes: FixtureError, FormatError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#as_json, #compact_blank, #exclude?, #excluding, #in_order_of, #including, #index_by, #index_with, #many?, #maximum, #minimum, #pick, #pluck, #sole

Constructor Details

#initialize(fixture, model_class) ⇒ Fixture

Returns a new instance of Fixture.



797
798
799
800
# File 'activerecord/lib/active_record/fixtures.rb', line 797

def initialize(fixture, model_class)
  @fixture     = fixture
  @model_class = model_class
end

Instance Attribute Details

#fixtureObject (readonly) Also known as: to_hash

Returns the value of attribute fixture.



795
796
797
# File 'activerecord/lib/active_record/fixtures.rb', line 795

def fixture
  @fixture
end

#model_classObject (readonly)

Returns the value of attribute model_class.



795
796
797
# File 'activerecord/lib/active_record/fixtures.rb', line 795

def model_class
  @model_class
end

Instance Method Details

#[](key) ⇒ Object



810
811
812
# File 'activerecord/lib/active_record/fixtures.rb', line 810

def [](key)
  fixture[key]
end

#class_nameObject



802
803
804
# File 'activerecord/lib/active_record/fixtures.rb', line 802

def class_name
  model_class.name if model_class
end

#each(&block) ⇒ Object



806
807
808
# File 'activerecord/lib/active_record/fixtures.rb', line 806

def each(&block)
  fixture.each(&block)
end

#findObject



816
817
818
819
820
821
822
823
824
825
# File 'activerecord/lib/active_record/fixtures.rb', line 816

def find
  raise FixtureClassNotFound, "No class attached to find." unless model_class
  object = model_class.unscoped do
    pk_clauses = fixture.slice(*Array(model_class.primary_key))
    model_class.find_by!(pk_clauses)
  end
  # Fixtures can't be eagerly loaded
  object.instance_variable_set(:@strict_loading, false)
  object
end