Class: Iry::Constraint::Unique
- Inherits:
-
Object
- Object
- Iry::Constraint::Unique
- Defined in:
- lib/iry/constraint/unique.rb
Constant Summary collapse
- MAX_INFER_NAME_BYTE_SIZE =
62
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: :taken) ⇒ Unique
constructor
A new instance of Unique.
Constructor Details
#initialize(keys, name:, error_key:, message: :taken) ⇒ Unique
Returns a new instance of Unique.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/iry/constraint/unique.rb', line 45 def initialize( keys, name:, error_key:, message: :taken ) @keys = keys @message = @name = name @error_key = error_key end |
Instance Attribute Details
#error_key ⇒ Symbol
39 40 41 |
# File 'lib/iry/constraint/unique.rb', line 39 def error_key @error_key end |
#keys ⇒ <Symbol>
33 34 35 |
# File 'lib/iry/constraint/unique.rb', line 33 def keys @keys end |
#message ⇒ Symbol, String
35 36 37 |
# File 'lib/iry/constraint/unique.rb', line 35 def @message end |
#name ⇒ String
37 38 39 |
# File 'lib/iry/constraint/unique.rb', line 37 def name @name end |
Class Method Details
.infer_name(keys, table_name) ⇒ String
Infers the unique constraint name based on keys and table name
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/iry/constraint/unique.rb', line 10 def self.infer_name(keys, table_name) # PostgreSQL convention: # "#{table_name}_#{keys.join("_")}_key" # Rails convention: # index_trip_hikers_on_trip_id_and_hiker_card_id # index_TABLENAME_on_COLUMN1_and_COLUMN2 name = "index_#{table_name}_on_#{keys.join("_and_")}" if name.bytesize <= MAX_INFER_NAME_BYTE_SIZE return name end digest = OpenSSL::Digest::SHA256.hexdigest(name)[0..9] hashed_id = "_#{digest}" name = "idx_on_#{keys.join("_")}" short_limit = MAX_INFER_NAME_BYTE_SIZE - hashed_id.bytesize short_name = name.mb_chars.limit(short_limit).to_s "#{short_name}#{hashed_id}" end |
Instance Method Details
#apply(model) ⇒ ActiveModel::Error
59 60 61 |
# File 'lib/iry/constraint/unique.rb', line 59 def apply(model) model.errors.add(error_key, ) end |