class Fixtory::DSL::Row

Public Class Methods

new(name, table, &block) click to toggle source
# File lib/fixtory/dsl/row.rb, line 6
def initialize(name, table, &block)
  @name = name.to_s
  @table = table
  @inserted = false
  @attributes = {
    _primary_key => ::ActiveRecord::FixtureSet.identify(@name)
  }
  instance_eval &block
end

Public Instance Methods

_primary_key() click to toggle source
# File lib/fixtory/dsl/row.rb, line 16
def _primary_key
  @table._model_class.primary_key
end
_primary_key_value() click to toggle source
# File lib/fixtory/dsl/row.rb, line 20
def _primary_key_value
  @attributes[_primary_key]
end
method_missing(attribute, *args) click to toggle source
# File lib/fixtory/dsl/row.rb, line 24
def method_missing(attribute, *args)
  attribute = attribute.to_s
  value = args.first

  if value
    if reflection = @table._model_class._reflections[attribute.to_sym]
      if reflection.macro == :belongs_to
        attribute = reflection.association_foreign_key
        value = value._primary_key_value
      else
        (::Array === value ? value : [value]).each do |row|
          row.__send__(reflection.foreign_key, self._primary_key_value)
        end

        return
      end
    end

    @attributes[attribute] = value
  else
    if @attributes.key?(attribute)
      @attributes[attribute]
    else
      @table._builder.instance_eval(attribute)
    end
  end
end