Module: Migrant::ModelExtensions

Defined in:
lib/migrant/model_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#schemaObject

Returns the value of attribute schema.



3
4
5
# File 'lib/migrant/model_extensions.rb', line 3

def schema
  @schema
end

Instance Method Details

#belongs_to(*args) ⇒ Object



5
6
7
8
9
# 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_schemaObject



11
12
13
14
15
16
17
# 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) ⇒ Object

Raises:



60
61
62
63
64
65
66
# 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) ⇒ Object



82
83
84
85
86
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# 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_structureObject

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



52
53
54
# File 'lib/migrant/model_extensions.rb', line 52

def no_structure
  structure {}
end

#reset_structure!Object



56
57
58
# File 'lib/migrant/model_extensions.rb', line 56

def reset_structure!
  @schema = nil
end

#structure(type = nil, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/migrant/model_extensions.rb', line 46

def structure_defined?
  @schema && @structure_defined || false
end