module Migrant::ModelExtensions

Attributes

schema[RW]

Public Instance Methods

belongs_to(*args) click to toggle source
Calls superclass method
# File lib/migrant/model_extensions.rb, line 5
def belongs_to(*args)
  super
  create_migrant_schema
  @schema.add_association(self.reflect_on_association(args.first))
end
create_migrant_schema() click to toggle source
# File lib/migrant/model_extensions.rb, line 11
def create_migrant_schema
  if self.superclass == ActiveRecord::Base
   @schema ||= Schema.new
  else
    @schema ||= InheritedSchema.new(self.superclass.schema)
  end
end
mock(attributes={}, recursive=true) click to toggle source
# File lib/migrant/model_extensions.rb, line 60
def mock(attributes={}, recursive=true)
  raise NoStructureDefined.new("In order to mock() #{self.to_s}, you need to define a Migrant structure block") unless @schema
 
  attribs = {}
  attribs.merge!(self.superclass.mock_attributes(attributes, recursive)) unless self.superclass == ActiveRecord::Base
  new attribs.merge(mock_attributes(attributes, recursive))
end
mock!(attributes={}, recursive=true) click to toggle source
# File lib/migrant/model_extensions.rb, line 82
def mock!(attributes={}, recursive=true)
  mock(attributes, recursive).tap do |mock|
    mock.save!
  end
end
mock_attributes(attributes={}, recursive=true) click to toggle source
# File lib/migrant/model_extensions.rb, line 68
def mock_attributes(attributes={}, recursive=true)
  attribs = @schema.columns.collect { |name, data_type| [name, data_type.mock] }.flatten(1)

  # Only recurse to one level, otherwise things get way too complicated
  if recursive
    attribs += self.reflect_on_all_associations(:belongs_to).collect do |association|
                begin
                  (association.klass.respond_to?(:mock))? [association.name, association.klass.mock({}, false)] : nil
                rescue NameError; nil; end # User hasn't defined association, just skip it
               end.compact.flatten
  end
  Hash[*attribs].merge(attributes)
end
no_structure() click to toggle source

Same as defining a structure block, but with no attributes besides relationships (such as in a many-to-many)

# File lib/migrant/model_extensions.rb, line 52
def no_structure
  structure {}
end
reset_structure!() click to toggle source
# File lib/migrant/model_extensions.rb, line 56
def reset_structure!
  @schema = nil
end
structure(type=nil, &block) click to toggle source
# File lib/migrant/model_extensions.rb, line 19
def structure(type=nil, &block)
  # Using instance_*evil* to get the neater DSL on the models.
  # So, my_field in the structure block actually calls Migrant::Schema.my_field

  create_migrant_schema
  @structure_defined = true

  if self.superclass == ActiveRecord::Base
    @schema.define_structure(type, &block)
    @schema.validations.each do |field, validation_options|
      validations = (validation_options.class == Array)? validation_options : [validation_options]
      validations.each do |validation|
        validation = (validation.class == Hash)? validation : { validation => true }
        self.validates(field, validation)
      end
    end
    # Set up serialized columns as required
    @schema.columns.select do |name, column|
      if column.serialized?
        serialize(name, column.serialized_class_name)
      end
    end
  else
    self.superclass.structure(&block) # For STI, cascade all fields onto the parent model
  end
end
structure_defined?() click to toggle source
# File lib/migrant/model_extensions.rb, line 46
def structure_defined?
  @schema && @structure_defined || false
end