Class: DeclareSchema::Model::ForeignKeyDefinition

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/declare_schema/model/foreign_key_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(foreign_key_column, constraint_name: nil, child_table_name: nil, parent_class_name: nil, parent_table_name: nil, dependent: nil) ⇒ ForeignKeyDefinition

Caller needs to pass either constraint_name or child_table_name, and either parent_class_name or parent_table_name.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 14

def initialize(foreign_key_column, constraint_name: nil, child_table_name: nil, parent_class_name: nil, parent_table_name: nil, dependent: nil)
  @foreign_key_column = foreign_key_column&.to_s or raise ArgumentError "foreign key must not be empty: #{foreign_key_column.inspect}"
  @constraint_name = constraint_name&.to_s.presence || ::DeclareSchema::Model::IndexDefinition.default_index_name(child_table_name, [@foreign_key_column])
  @child_table_name = child_table_name&.to_s or raise ArgumentError, "child_table_name must not be nil"
  @parent_class_name =
    case parent_class_name
    when String, Symbol
      parent_class_name.to_s
    when Class
      @parent_class = parent_class_name
      @parent_class.name
    when nil
      @foreign_key_column.sub(/_id\z/, '').camelize
    end
  @parent_table_name = parent_table_name
  dependent.in?([nil, :delete]) or raise ArgumentError, "dependent: must be nil or :delete"
  @dependent = dependent
end

Instance Attribute Details

#child_table_nameObject (readonly)

Returns the value of attribute child_table_name.



10
11
12
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10

def child_table_name
  @child_table_name
end

#constraint_nameObject (readonly)

Returns the value of attribute constraint_name.



10
11
12
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10

def constraint_name
  @constraint_name
end

#dependentObject (readonly)

Returns the value of attribute dependent.



10
11
12
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10

def dependent
  @dependent
end

#foreign_key_columnObject (readonly)

Returns the value of attribute foreign_key_column.



10
11
12
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10

def foreign_key_column
  @foreign_key_column
end

#parent_class_nameObject (readonly)

Returns the value of attribute parent_class_name.



10
11
12
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10

def parent_class_name
  @parent_class_name
end

Class Method Details

.for_table(child_table_name, connection, dependent: nil) ⇒ Object

TODO: I think we might just be able to start using the AR built in moving forward



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 35

def for_table(child_table_name, connection, dependent: nil)
  connection.foreign_keys(child_table_name).map do |fkc|
    new(
      fkc.column,
      constraint_name: fkc.name,
      child_table_name: fkc.from_table,
      parent_table_name: fkc.to_table,
      dependent: dependent || fkc.on_delete == :cascade ? :delete : nil
    )
  end
end

Instance Method Details

#<=>(rhs) ⇒ Object



52
53
54
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 52

def <=>(rhs)
  key <=> rhs.key
end

#equivalent?(rhs) ⇒ Boolean

Returns:



58
59
60
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 58

def equivalent?(rhs)
  self == rhs
end

#hashObject



72
73
74
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 72

def hash
  key.hash
end

#keyObject



48
49
50
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 48

def key
  @key ||= [@child_table_name, @foreign_key_column, @dependent].freeze
end

#parent_classObject

returns the parent class as a Class object lazy loaded so that we don’t require the parent class until we need it



64
65
66
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 64

def parent_class
  @parent_class ||= @parent_class_name.constantize
end

#parent_table_nameObject



68
69
70
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 68

def parent_table_name
  @parent_table_name ||= parent_class.table_name
end