Sha256: d8aa1f3e5654b163c2dda3f446cae37abc33b1f98c3b7331101f2f4799f4881a
Contents?: true
Size: 1.46 KB
Versions: 3
Compression:
Stored size: 1.46 KB
Contents
module Migrant module ModelExtensions attr_accessor :schema def structure(&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 if self.superclass == ActiveRecord::Base @schema ||= Schema.new @schema.add_associations(self.reflect_on_all_associations) @schema.define_structure(&block) else self.superclass.structure(&block) # For STI, cascade all fields onto the parent model @schema = InheritedSchema.new(self.superclass.schema) end end # Same as defining a structure block, but with no attributes besides # relationships (such as in a many-to-many) def no_structure structure {} end def mock(recursive=true) attribs = @schema.columns.reject { |column| column.is_a?(DataType::ForeignKey)}.collect { |name, data_type| [name, data_type.mock] }.flatten # 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 new Hash[*attribs] end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
migrant-0.1.2 | lib/migrant/model_extensions.rb |
migrant-0.1.1 | lib/migrant/model_extensions.rb |
migrant-0.1.0 | lib/migrant/model_extensions.rb |