class DirtySeed::Model

Represents an Active Record model

Constants

PROTECTED_COLUMNS

@!method initialize(active_record_model) @param active_record_model [Class] an active record model @return [DirtySeed::Model]

Public Instance Methods

associated_models() click to toggle source

Returns ActiveRecord models associated to the current ActiveRecord model

through a has_many or has_one associations

@return [Array<Class>] ActiveRecord models

# File lib/dirty_seed/model.rb, line 25
def associated_models
  associations.map(&:associated_models).flatten
end
associations() click to toggle source

Returns an dirty associations representing the ActiveRecord model belongs_to associations @return [Array<DirtySeed::Association>]

# File lib/dirty_seed/model.rb, line 31
def associations
  included_reflections.map do |reflection|
    DirtySeed::Association.new(reflection)
  end
end
attributes() click to toggle source

Returns ActiveRecord model attributes @return [Array<DirtySeed::Attribute>]

# File lib/dirty_seed/model.rb, line 39
def attributes
  included_columns.map do |column|
    attribute = DirtySeed::Attribute.new(column)
    attribute.model = self
    attribute
  end
end

Private Instance Methods

included_columns() click to toggle source

Returns columns that should be treated as regular attributes @return [Array<ActiveRecord::ConnectionAdapters::Column>]

# File lib/dirty_seed/model.rb, line 51
def included_columns
  excluded = PROTECTED_COLUMNS + reflection_related_attributes
  columns.reject do |column|
    column.name.in? excluded
  end
end
included_reflections() click to toggle source

Returns reflections of the ActiveRecord model @return [Array<ActiveRecord::Reflection::BelongsToReflection>]

# File lib/dirty_seed/model.rb, line 72
def included_reflections
  reflections.values.select do |reflection|
    reflection.is_a? ActiveRecord::Reflection::BelongsToReflection
  end
end