Class: DeclareSchema::Model::HabtmModelShim
- Inherits:
-
Object
- Object
- DeclareSchema::Model::HabtmModelShim
- Defined in:
- lib/declare_schema/model/habtm_model_shim.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#foreign_key_classes ⇒ Object
readonly
Returns the value of attribute foreign_key_classes.
-
#foreign_keys ⇒ Object
readonly
Returns the value of attribute foreign_keys.
-
#join_table ⇒ Object
readonly
Returns the value of attribute join_table.
Class Method Summary collapse
Instance Method Summary collapse
- #_declared_primary_key ⇒ Object
- #_table_options ⇒ Object
- #constraint_specs ⇒ Object
- #field_specs ⇒ Object
- #ignore_indexes ⇒ Object
- #index_definitions_with_primary_key ⇒ Object (also: #index_definitions)
- #index_name ⇒ Object
-
#initialize(join_table, foreign_keys, foreign_key_classes, connection) ⇒ HabtmModelShim
constructor
A new instance of HabtmModelShim.
- #primary_key ⇒ Object
- #table_name ⇒ Object
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
#connection ⇒ Object (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_classes ⇒ Object (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_keys ⇒ Object (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_table ⇒ Object (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_key ⇒ Object
54 55 56 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 54 def _declared_primary_key false # no single-column primary key declared end |
#_table_options ⇒ Object
32 33 34 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 32 def {} end |
#constraint_specs ⇒ Object
71 72 73 74 75 76 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 71 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_specs ⇒ Object
44 45 46 47 48 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 44 def field_specs foreign_keys.each_with_index.each_with_object({}) do |(v, position), result| result[v] = ::DeclareSchema::Model::FieldSpec.new(self, v, :bigint, position: position, null: false) end end |
#ignore_indexes ⇒ Object
67 68 69 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 67 def ignore_indexes [] end |
#index_definitions_with_primary_key ⇒ Object Also known as: index_definitions
58 59 60 61 62 63 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 58 def index_definitions_with_primary_key [ IndexDefinition.new(self, foreign_keys, unique: true, name: index_name), # creates a primary composite key on both foriegn keys IndexDefinition.new(self, foreign_keys.last) # not unique by itself; combines with primary key to be unique ] end |
#index_name ⇒ Object
40 41 42 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 40 def index_name "index_#{table_name}_on_#{foreign_keys.first}_#{foreign_keys.last}" end |
#primary_key ⇒ Object
50 51 52 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 50 def primary_key false # no single-column primary key in database end |
#table_name ⇒ Object
36 37 38 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 36 def table_name join_table end |