Class: DeclareSchema::Model::HabtmModelShim
- Inherits:
-
Object
- Object
- DeclareSchema::Model::HabtmModelShim
- Defined in:
- lib/declare_schema/model/habtm_model_shim.rb
Instance Attribute Summary collapse
-
#foreign_keys ⇒ Object
readonly
Returns the value of attribute foreign_keys.
-
#join_table ⇒ Object
readonly
Returns the value of attribute join_table.
-
#parent_table_names ⇒ Object
readonly
Returns the value of attribute parent_table_names.
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 ⇒ Object
- #index_definitions_with_primary_key ⇒ Object
-
#initialize(join_table, foreign_keys, parent_table_names) ⇒ HabtmModelShim
constructor
A new instance of HabtmModelShim.
- #primary_key ⇒ Object
- #table_name ⇒ Object
Constructor Details
#initialize(join_table, foreign_keys, parent_table_names) ⇒ HabtmModelShim
Returns a new instance of HabtmModelShim.
15 16 17 18 19 20 21 22 23 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 15 def initialize(join_table, foreign_keys, parent_table_names) 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 end |
Instance Attribute Details
#foreign_keys ⇒ Object (readonly)
Returns the value of attribute foreign_keys.
13 14 15 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 13 def foreign_keys @foreign_keys end |
#join_table ⇒ Object (readonly)
Returns the value of attribute join_table.
13 14 15 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 13 def join_table @join_table end |
#parent_table_names ⇒ Object (readonly)
Returns the value of attribute parent_table_names.
13 14 15 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 13 def parent_table_names @parent_table_names end |
Class Method Details
.from_reflection(refl) ⇒ Object
7 8 9 10 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 7 def from_reflection(refl) new(refl.join_table, [refl.foreign_key, refl.association_foreign_key], [refl.active_record.table_name, refl.class_name.constantize.table_name]) end |
Instance Method Details
#_declared_primary_key ⇒ Object
43 44 45 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 43 def _declared_primary_key foreign_keys end |
#_table_options ⇒ Object
25 26 27 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 25 def {} end |
#constraint_specs ⇒ Object
64 65 66 67 68 69 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 64 def constraint_specs [ ForeignKeyDefinition.new(foreign_keys.first, child_table: @join_table, parent_table: parent_table_names.first, constraint_name: "#{join_table}_FK1", dependent: :delete), ForeignKeyDefinition.new(foreign_keys.last, child_table: @join_table, parent_table: parent_table_names.last, constraint_name: "#{join_table}_FK2", dependent: :delete) ] end |
#field_specs ⇒ Object
33 34 35 36 37 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 33 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_indexes ⇒ Object
60 61 62 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 60 def ignore_indexes [] end |
#index_definitions ⇒ Object
47 48 49 50 51 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 47 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_key ⇒ Object
53 54 55 56 57 58 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 53 def index_definitions_with_primary_key [ *index_definitions, IndexDefinition.new(foreign_keys, name: Model::IndexDefinition::PRIMARY_KEY_NAME, unique: true) # creates a primary composite key on both foreign keys ] end |
#primary_key ⇒ Object
39 40 41 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 39 def primary_key false # no single-column primary key in database end |
#table_name ⇒ Object
29 30 31 |
# File 'lib/declare_schema/model/habtm_model_shim.rb', line 29 def table_name join_table end |