Class: Generators::DeclareSchema::Migration::HabtmModelShim

Inherits:
Struct
  • Object
show all
Defined in:
lib/generators/declare_schema/migration/migrator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject

Returns the value of attribute connection

Returns:

  • (Object)

    the current value of connection



8
9
10
# File 'lib/generators/declare_schema/migration/migrator.rb', line 8

def connection
  @connection
end

#foreign_key_classesObject

Returns the value of attribute foreign_key_classes

Returns:

  • (Object)

    the current value of foreign_key_classes



8
9
10
# File 'lib/generators/declare_schema/migration/migrator.rb', line 8

def foreign_key_classes
  @foreign_key_classes
end

#foreign_keysObject

Returns the value of attribute foreign_keys

Returns:

  • (Object)

    the current value of foreign_keys



8
9
10
# File 'lib/generators/declare_schema/migration/migrator.rb', line 8

def foreign_keys
  @foreign_keys
end

#join_tableObject

Returns the value of attribute join_table

Returns:

  • (Object)

    the current value of join_table



8
9
10
# File 'lib/generators/declare_schema/migration/migrator.rb', line 8

def join_table
  @join_table
end

Class Method Details

.from_reflection(refl) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/generators/declare_schema/migration/migrator.rb', line 10

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

#constraint_specsObject



63
64
65
66
67
68
# File 'lib/generators/declare_schema/migration/migrator.rb', line 63

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

#field_specsObject



38
39
40
41
42
43
44
# File 'lib/generators/declare_schema/migration/migrator.rb', line 38

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

#ignore_indexesObject



59
60
61
# File 'lib/generators/declare_schema/migration/migrator.rb', line 59

def ignore_indexes
  []
end

#index_definitions_with_primary_keyObject Also known as: index_definitions



50
51
52
53
54
55
# File 'lib/generators/declare_schema/migration/migrator.rb', line 50

def index_definitions_with_primary_key
  [
    ::DeclareSchema::Model::IndexDefinition.new(self, foreign_keys, unique: true, name: ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME),
    ::DeclareSchema::Model::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/generators/declare_schema/migration/migrator.rb', line 46

def primary_key
  false # no single-column primary key
end

#table_exists?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/generators/declare_schema/migration/migrator.rb', line 34

def table_exists?
  ActiveRecord::Migration.table_exists? table_name
end

#table_nameObject



30
31
32
# File 'lib/generators/declare_schema/migration/migrator.rb', line 30

def table_name
  join_table
end

#table_optionsObject



26
27
28
# File 'lib/generators/declare_schema/migration/migrator.rb', line 26

def table_options
  {}
end