123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::FixtureSet::TableRow

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Exceptions:
Inherits: Object
Defined in: activerecord/lib/active_record/fixture_set/table_row.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(fixture, table_rows:, label:, now:) ⇒ TableRow

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 65

def initialize(fixture, table_rows:, label:, now:)
  @table_rows = table_rows
  @label = label
  @now = now
  @row = fixture.to_hash
  fill_row_model_attributes
end

Instance Method Details

#add_join_records(association) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 186

def add_join_records(association)
  # This is the case when the join table has no fixtures file
  if (targets = @row.delete(association.name.to_s))
    table_name  = association.join_table
    column_type = association.primary_key_type
    lhs_key     = association.lhs_key
    rhs_key     = association.rhs_key

    targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
    joins   = targets.map do |target|
      join = { lhs_key => @row[.primary_key_name],
               rhs_key => ActiveRecord::FixtureSet.identify(target, column_type) }
      association.timestamp_column_names.each do |col|
        join[col] = @now
      end
      join
    end
    @table_rows.tables[table_name].concat(joins)
  end
end

#column_defined?(col) ⇒ Boolean (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 136

def column_defined?(col)
  !.has_column?(col) || @row.include?(col)
end

#fill_row_model_attributes (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 86

def fill_row_model_attributes
  return unless model_class
  fill_timestamps
  interpolate_label
  model_class.composite_primary_key? ? generate_composite_primary_key : generate_primary_key
  resolve_enums
  resolve_sti_reflections
end

#fill_timestamps (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 103

def fill_timestamps
  # fill in timestamp columns if they aren't specified and the model is set to record_timestamps
  if model_class.record_timestamps
    .timestamp_column_names.each do |c_name|
      @row[c_name] = @now unless @row.key?(c_name)
    end
  end
end

#generate_composite_primary_key (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 127

def generate_composite_primary_key
  composite_key = ActiveRecord::FixtureSet.composite_identify(@label, .primary_key_name)
  composite_key.each do |column, value|
    next if column_defined?(column)

    @row[column] = value
  end
end

#generate_primary_key (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 119

def generate_primary_key
  pk = .primary_key_name

  unless column_defined?(pk)
    @row[pk] = ActiveRecord::FixtureSet.identify(@label, .column_type(pk))
  end
end

#interpolate_label (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 112

def interpolate_label
  # interpolate the fixture label
  @row.each do |key, value|
    @row[key] = value.gsub("$LABEL", @label.to_s) if value.is_a?(String)
  end
end

#model_class (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 82

def model_class
  @table_rows.model_class
end

#model_metadata (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 78

def 
  @table_rows.
end

#reflection_class (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 95

def reflection_class
  @reflection_class ||= if @row.include?(.inheritance_column_name)
    @row[.inheritance_column_name].constantize rescue model_class
  else
    model_class
  end
end

#resolve_enums (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 140

def resolve_enums
  reflection_class.defined_enums.each do |name, values|
    if @row.include?(name)
      @row[name] = values.fetch(@row[name], @row[name])
    end
  end
end

#resolve_sti_reflections (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 148

def resolve_sti_reflections
  # If STI is used, find the correct subclass for association reflection
  reflection_class._reflections.each_value do |association|
    case association.macro
    when :belongs_to
      # Do not replace association name with association foreign key if they are named the same
      fk_name = association.join_foreign_key

      if association.name.to_s != fk_name && value = @row.delete(association.name.to_s)
        if association.polymorphic?
          if value.sub!(/\s*\(([^)]*)\)\s*$/, "")
            # support polymorphic belongs_to as "label (Type)"
            @row[association.join_foreign_type] = $1
          end
        elsif association.join_primary_key != association.klass.primary_key
          raise PrimaryKeyError.new(@label, association, value)
        end

        if fk_name.is_a?(Array)
          composite_key = ActiveRecord::FixtureSet.composite_identify(value, fk_name)
          composite_key.each do |column, value|
            next if column_defined?(column)

            @row[column] = value
          end
        else
          fk_type = reflection_class.type_for_attribute(fk_name).type
          @row[fk_name] = ActiveRecord::FixtureSet.identify(value, fk_type)
        end
      end
    when :has_many
      if association.options[:through]
        add_join_records(HasManyThroughProxy.new(association))
      end
    end
  end
end

#to_hash

[ GitHub ]

  
# File 'activerecord/lib/active_record/fixture_set/table_row.rb', line 73

def to_hash
  @row
end