Class: Iry::Constraint::ForeignKey
- Inherits:
-
Object
- Object
- Iry::Constraint::ForeignKey
- Defined in:
- lib/iry/constraint/foreign_key.rb
Instance Attribute Summary collapse
Class Method Summary collapse
-
.infer_name(keys, table_name) ⇒ String
Infers the unique constraint name based on keys and table name.
Instance Method Summary collapse
- #apply(model) ⇒ ActiveModel::Error
-
#initialize(keys, name:, error_key:, message: :required) ⇒ ForeignKey
constructor
A new instance of ForeignKey.
Constructor Details
#initialize(keys, name:, error_key:, message: :required) ⇒ ForeignKey
Returns a new instance of ForeignKey.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/iry/constraint/foreign_key.rb', line 35 def initialize( keys, name:, error_key:, message: :required ) @keys = keys @message = @name = name @error_key = error_key end |
Instance Attribute Details
#error_key ⇒ Symbol
29 30 31 |
# File 'lib/iry/constraint/foreign_key.rb', line 29 def error_key @error_key end |
#keys ⇒ <Symbol>
23 24 25 |
# File 'lib/iry/constraint/foreign_key.rb', line 23 def keys @keys end |
#message ⇒ Symbol, String
25 26 27 |
# File 'lib/iry/constraint/foreign_key.rb', line 25 def @message end |
#name ⇒ String
27 28 29 |
# File 'lib/iry/constraint/foreign_key.rb', line 27 def name @name end |
Class Method Details
.infer_name(keys, table_name) ⇒ String
Infers the unique constraint name based on keys and table name
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/iry/constraint/foreign_key.rb', line 8 def self.infer_name(keys, table_name) if keys.size > 1 # PostgreSQL convention: return "#{table_name}_#{keys.join("_")}_fkey" end # Rails convention: column = keys.first id = "#{table_name}_#{column}_fk" hashed_id = OpenSSL::Digest::SHA256.hexdigest(id)[0..9] "fk_rails_#{hashed_id}" end |
Instance Method Details
#apply(model) ⇒ ActiveModel::Error
49 50 51 |
# File 'lib/iry/constraint/foreign_key.rb', line 49 def apply(model) model.errors.add(error_key, ) end |