Module: DeclareSchema::Model

Defined in:
lib/declare_schema/model.rb,
lib/declare_schema/model/column.rb,
lib/declare_schema/model/field_spec.rb,
lib/declare_schema/model/habtm_model_shim.rb,
lib/declare_schema/model/index_definition.rb,
lib/declare_schema/model/foreign_key_definition.rb,
lib/declare_schema/model/table_options_definition.rb

Defined Under Namespace

Modules: ClassMethods Classes: Column, FieldSpec, ForeignKeyDefinition, HabtmModelShim, IndexDefinition, TableOptionsDefinition

Class Method Summary collapse

Class Method Details

.mix_in(base) ⇒ Object



10
11
12
13
14
15
16
17
18
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
45
46
47
48
49
50
51
52
53
# File 'lib/declare_schema/model.rb', line 10

def mix_in(base)
  base.singleton_class.prepend ClassMethods unless base.singleton_class < ClassMethods # don't mix in if a base class already did it

  base.class_eval do
    # ignore the model in the migration until somebody sets
    # @include_in_migration via the fields declaration
    inheriting_cattr_reader include_in_migration: false

    # attr_types holds the type class for any attribute reader (i.e. getter
    # method) that returns rich-types
    inheriting_cattr_reader attr_types: HashWithIndifferentAccess.new
    inheriting_cattr_reader attr_order: []

    # field_specs holds FieldSpec objects for every declared
    # field. Note that attribute readers are created (by ActiveRecord)
    # for all fields, so there is also an entry for the field in
    # attr_types. This is redundant but simplifies the implementation
    # and speeds things up a little.
    inheriting_cattr_reader field_specs: HashWithIndifferentAccess.new

    # index_definitions holds IndexDefinition objects for all the declared indexes.
    inheriting_cattr_reader index_definitions: []
    inheriting_cattr_reader ignore_indexes: []
    inheriting_cattr_reader constraint_specs: []

    # table_options holds optional configuration for the create_table statement
    # supported options include :charset and :collation
    inheriting_cattr_reader table_options: HashWithIndifferentAccess.new

    # eval avoids the ruby 1.9.2 "super from singleton method ..." error

    eval "      def self.inherited(klass)\n        unless klass.field_specs.has_key?(inheritance_column)\n          fields do |f|\n            f.field(inheritance_column, :string, limit: 255, null: true)\n          end\n          index(inheritance_column)\n        end\n        super\n      end\n    EOS\n  end\nend\n"