Module: ActiveRecordSchema::Dsl::ClassMethods

Defined in:
lib/active_record_schema/dsl.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_record_schema/dsl.rb', line 27

def belongs_to(name, options = {})
  options.symbolize_keys!
  skip_index = options.delete(:index) == false
      
  foreign_key  = options[:foreign_key] || "#{name}_id"
  field :"#{foreign_key}", :as => :integer
      
  if options[:polymorphic]
    foreign_type = options[:foreign_type] || "#{name}_type"
    field :"#{foreign_type}"
    add_index [:"#{foreign_key}", :"#{foreign_type}"] if !skip_index
  else
    add_index :"#{foreign_key}" if !skip_index
  end
      
  super(name, options)
end

#field(name, *args) ⇒ Object

field :name, :string, :default => “Joe”



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_record_schema/dsl.rb', line 14

def field(name, *args)
  options    = args.extract_options!
  type       = options.delete(:as) || options.delete(:type) || args.first || :string
  type       = type.name.underscore.to_sym if (type.class == Class) 
  index      = options.delete(:index)
  
  schema.add_field(name, type, options)

  if index
    schema.add_index(name)
  end       
end

#has_and_belongs_to_many(name, options = {}, &extension) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_record_schema/dsl.rb', line 45

def has_and_belongs_to_many(name, options = {}, &extension)
  options.symbolize_keys!

  self_class_name = self.name
  association_class_name = options[:class_name] || "#{name}".singularize.camelize

  table = options[:join_table] || [self_class_name, association_class_name].sort.map(&:tableize).join("_")
  key1  = options[:foreign_key] || "#{self_class_name.underscore}_id"
  key2  = options[:association_foreign_key] || "#{association_class_name.underscore}_id"
  skip_index = options.delete(:index) == false

  schema.add_join(table, key1, key2, !skip_index)
  super(name, options, &extension)
end

#index(column_name, options = {}) ⇒ Object Also known as: add_index



60
61
62
# File 'lib/active_record_schema/dsl.rb', line 60

def index(column_name, options = {})
  schema.add_index(column_name, options)
end

#inheritableObject



70
71
72
# File 'lib/active_record_schema/dsl.rb', line 70

def inheritable
  field :"#{inheritance_column}"
end

#schemaObject



8
9
10
11
# File 'lib/active_record_schema/dsl.rb', line 8

def schema
  @active_record_schema_schema ||= ActiveRecordSchema::Schema.new(self)
  
end

#timestampsObject



65
66
67
68
# File 'lib/active_record_schema/dsl.rb', line 65

def timestamps
  field :created_at, :datetime
  field :updated_at, :datetime
end