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, parent_table_names, connection:) ⇒ HabtmModelShim

Returns a new instance of HabtmModelShim.



17
18
19
20
21
22
23
24
25
26
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 17

def initialize(join_table, foreign_keys, parent_table_names, connection:)
  foreign_keys.is_a?(Array) && foreign_keys.size == 2 or
    raise ArgumentError, "foreign_keys must be <Array[2]>; got #{foreign_keys.inspect}"
  parent_table_names.is_a?(Array) && parent_table_names.size == 2 or
    raise ArgumentError, "parent_table_names must be <Array[2]>; got #{parent_table_names.inspect}"
  @join_table = join_table
  @foreign_keys = foreign_keys.sort # Rails requires these be in alphabetical order
  @parent_table_names = @foreign_keys == foreign_keys ? parent_table_names : parent_table_names.reverse # match the above sort
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



15
16
17
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 15

def connection
  @connection
end

#foreign_keysObject (readonly)

Returns the value of attribute foreign_keys.



15
16
17
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 15

def foreign_keys
  @foreign_keys
end

#join_tableObject (readonly)

Returns the value of attribute join_table.



15
16
17
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 15

def join_table
  @join_table
end

#parent_table_namesObject (readonly)

Returns the value of attribute parent_table_names.



15
16
17
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 15

def parent_table_names
  @parent_table_names
end

Class Method Details

.from_reflection(reflection) ⇒ Object



7
8
9
10
11
12
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 7

def from_reflection(reflection)
  new(reflection.join_table,
      [reflection.foreign_key, reflection.association_foreign_key],
      [reflection.active_record.table_name, reflection.klass.table_name],
      connection: reflection.active_record.connection)
end

Instance Method Details

#_declared_primary_keyObject



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

def _declared_primary_key
  foreign_keys
end

#_table_optionsObject



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

def _table_options
  {}
end

#constraint_definitionsObject



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

def constraint_definitions
  @constraint_definitions ||= Set.new([
    ForeignKeyDefinition.new(foreign_keys.first, constraint_name: "#{join_table}_FK1", child_table_name: @join_table, parent_table_name: parent_table_names.first, dependent: :delete),
    ForeignKeyDefinition.new(foreign_keys.last, constraint_name: "#{join_table}_FK2", child_table_name: @join_table, parent_table_name: parent_table_names.last, dependent: :delete)
  ])
end

#field_specsObject



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

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

#ignore_indexesObject



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

def ignore_indexes
  @ignore_indexes ||= Set.new
end

#index_definitionsObject



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

def index_definitions
  [
    IndexDefinition.new(foreign_keys.last, table_name: table_name, unique: false) # index for queries where we only have the last foreign key
  ]
end

#index_definitions_with_primary_keyObject



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

def index_definitions_with_primary_key
  [
    *index_definitions,
    IndexDefinition.new(foreign_keys, table_name: table_name, name: Model::IndexDefinition::PRIMARY_KEY_NAME, unique: true) # creates a primary composite key on both foreign keys
  ]
end

#primary_keyObject



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

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

#table_nameObject



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

def table_name
  join_table
end