Class: Generators::DeclareSchema::Migration::HabtmModelShim
- Inherits:
-
Struct
- Object
- Struct
- Generators::DeclareSchema::Migration::HabtmModelShim
- Defined in:
- lib/generators/declare_schema/migration/migrator.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#foreign_key_classes ⇒ Object
Returns the value of attribute foreign_key_classes.
-
#foreign_keys ⇒ Object
Returns the value of attribute foreign_keys.
-
#join_table ⇒ Object
Returns the value of attribute join_table.
Class Method Summary collapse
Instance Method Summary collapse
- #constraint_specs ⇒ Object
- #field_specs ⇒ Object
- #ignore_indexes ⇒ Object
- #index_definitions_with_primary_key ⇒ Object (also: #index_definitions)
- #primary_key ⇒ Object
- #table_exists? ⇒ Boolean
- #table_name ⇒ Object
- #table_options ⇒ Object
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection
8 9 10 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 8 def connection @connection end |
#foreign_key_classes ⇒ Object
Returns the value of attribute 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_keys ⇒ Object
Returns the value of attribute foreign_keys
8 9 10 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 8 def foreign_keys @foreign_keys end |
#join_table ⇒ Object
Returns the value of attribute 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_specs ⇒ Object
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_specs ⇒ Object
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_indexes ⇒ Object
59 60 61 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 59 def ignore_indexes [] end |
#index_definitions_with_primary_key ⇒ Object 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_key ⇒ Object
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
34 35 36 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 34 def table_exists? ActiveRecord::Migration.table_exists? table_name end |
#table_name ⇒ Object
30 31 32 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 30 def table_name join_table end |
#table_options ⇒ Object
26 27 28 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 26 def {} end |