Class: DeclareSchema::Model::HabtmModelShim

Inherits:
Object
  • Object
show all
Defined in:
lib/declare_schema/model/habtm_model_shim.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(join_table, foreign_keys, foreign_key_classes, connection) ⇒ HabtmModelShim

Returns a new instance of HabtmModelShim.



25
26
27
28
29
30
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 25

def initialize(join_table, foreign_keys, foreign_key_classes, connection)
  @join_table = join_table
  @foreign_keys = foreign_keys
  @foreign_key_classes = foreign_key_classes
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



23
24
25
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 23

def connection
  @connection
end

#foreign_key_classesObject (readonly)

Returns the value of attribute foreign_key_classes.



23
24
25
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 23

def foreign_key_classes
  @foreign_key_classes
end

#foreign_keysObject (readonly)

Returns the value of attribute foreign_keys.



23
24
25
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 23

def foreign_keys
  @foreign_keys
end

#join_tableObject (readonly)

Returns the value of attribute join_table.



23
24
25
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 23

def join_table
  @join_table
end

Class Method Details

.from_reflection(refl) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 7

def from_reflection(refl)
  join_table = refl.join_table
  foreign_keys_and_classes = [
    [refl.foreign_key.to_s, refl.active_record],
    [refl.association_foreign_key.to_s, refl.class_name.constantize]
  ].sort { |a, b| a.first <=> b.first }
  foreign_keys = foreign_keys_and_classes.map(&:first)
  foreign_key_classes = foreign_keys_and_classes.map(&:last)
  # this may fail in weird ways if HABTM is running across two DB connections (assuming that's even supported)
  # figure that anybody who sets THAT up can deal with their own migrations...
  connection = refl.active_record.connection

  new(join_table, foreign_keys, foreign_key_classes, connection)
end

Instance Method Details

#_declared_primary_keyObject



50
51
52
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 50

def _declared_primary_key
  false # no single-column primary key declared
end

#_table_optionsObject



32
33
34
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 32

def _table_options
  {}
end

#constraint_specsObject



67
68
69
70
71
72
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 67

def constraint_specs
  [
    ForeignKeyDefinition.new(self, foreign_keys.first, parent_table: foreign_key_classes.first.table_name, constraint_name: "#{join_table}_FK1", dependent: :delete),
    ForeignKeyDefinition.new(self, foreign_keys.last, parent_table: foreign_key_classes.last.table_name, constraint_name: "#{join_table}_FK2", dependent: :delete)
  ]
end

#field_specsObject



40
41
42
43
44
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 40

def field_specs
  foreign_keys.each_with_index.each_with_object({}) do |(v, position), result|
    result[v] = ::DeclareSchema::Model::FieldSpec.new(self, v, :integer, position: position, null: false)
  end
end

#ignore_indexesObject



63
64
65
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 63

def ignore_indexes
  []
end

#index_definitions_with_primary_keyObject Also known as: index_definitions



54
55
56
57
58
59
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 54

def index_definitions_with_primary_key
  [
    IndexDefinition.new(self, foreign_keys, unique: true, name: ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME),
    IndexDefinition.new(self, foreign_keys.last) # not unique by itself; combines with primary key to be unique
  ]
end

#primary_keyObject



46
47
48
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 46

def primary_key
  false # no single-column primary key in database
end

#table_nameObject



36
37
38
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 36

def table_name
  join_table
end