Class: DeclareSchema::Model::ForeignKeyDefinition
- Inherits:
-
Object
- Object
- DeclareSchema::Model::ForeignKeyDefinition
- Includes:
- Comparable
- Defined in:
- lib/declare_schema/model/foreign_key_definition.rb
Instance Attribute Summary collapse
-
#constraint_name ⇒ Object
readonly
Returns the value of attribute constraint_name.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#foreign_key_name ⇒ Object
readonly
Returns the value of attribute foreign_key_name.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#on_delete_cascade ⇒ Object
readonly
Returns the value of attribute on_delete_cascade.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(rhs) ⇒ Object
-
#initialize(model, foreign_key, options = {}) ⇒ ForeignKeyDefinition
constructor
A new instance of ForeignKeyDefinition.
-
#parent_class ⇒ Object
returns the parent class as a Class object or nil if no :class_name option given.
- #parent_table_name ⇒ Object
- #to_add_statement ⇒ Object
Constructor Details
#initialize(model, foreign_key, options = {}) ⇒ ForeignKeyDefinition
Returns a new instance of ForeignKeyDefinition.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10 def initialize(model, foreign_key, = {}) @model = model @foreign_key = foreign_key.to_s.presence = @child_table = model.table_name # unless a table rename, which would happen when a class is renamed?? @parent_table_name = [:parent_table]&.to_s @foreign_key_name = [:foreign_key]&.to_s || @foreign_key @index_name = [:index_name]&.to_s || model.connection.index_name(model.table_name, column: @foreign_key_name) # Empty constraint lets mysql generate the name @constraint_name = [:constraint_name]&.to_s || @index_name&.to_s || '' @on_delete_cascade = [:dependent] == :delete end |
Instance Attribute Details
#constraint_name ⇒ Object (readonly)
Returns the value of attribute constraint_name.
8 9 10 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 8 def constraint_name @constraint_name end |
#foreign_key ⇒ Object (readonly)
Returns the value of attribute foreign_key.
8 9 10 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 8 def foreign_key @foreign_key end |
#foreign_key_name ⇒ Object (readonly)
Returns the value of attribute foreign_key_name.
8 9 10 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 8 def foreign_key_name @foreign_key_name end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
8 9 10 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 8 def model @model end |
#on_delete_cascade ⇒ Object (readonly)
Returns the value of attribute on_delete_cascade.
8 9 10 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 8 def on_delete_cascade @on_delete_cascade end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 8 def end |
Class Method Details
.for_model(model, old_table_name) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 26 def for_model(model, old_table_name) show_create_table = model.connection.select_rows("show create table #{model.connection.quote_table_name(old_table_name)}").first.last constraints = show_create_table.split("\n").map { |line| line.strip if line['CONSTRAINT'] }.compact constraints.map do |fkc| name, foreign_key, parent_table = fkc.match(/CONSTRAINT `([^`]*)` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)`/).captures = { constraint_name: name, parent_table: parent_table, foreign_key: foreign_key } [:dependent] = :delete if fkc['ON DELETE CASCADE'] new(model, foreign_key, ) end end |
Instance Method Details
#<=>(rhs) ⇒ Object
67 68 69 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 67 def <=>(rhs) key <=> rhs.send(:key) end |
#parent_class ⇒ Object
returns the parent class as a Class object or nil if no :class_name option given
46 47 48 49 50 51 52 53 54 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 46 def parent_class if (class_name = [:class_name]) if class_name.is_a?(Class) class_name else class_name.to_s.constantize end end end |
#parent_table_name ⇒ Object
56 57 58 59 60 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 56 def parent_table_name @parent_table_name ||= parent_class&.try(:table_name) || foreign_key.sub(/_id\z/, '').camelize.constantize.table_name end |
#to_add_statement ⇒ Object
62 63 64 65 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 62 def to_add_statement "add_foreign_key(#{@child_table.inspect}, #{parent_table_name.inspect}, " + "column: #{@foreign_key_name.inspect}, name: #{@constraint_name.inspect})" end |