Class: DatabaseValidations::UniquenessOptions
- Inherits:
-
Object
- Object
- DatabaseValidations::UniquenessOptions
- Defined in:
- lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb
Constant Summary collapse
- CUSTOM_OPTIONS =
%i[where index_name].freeze
- DEFAULT_OPTIONS =
{ allow_nil: true, case_sensitive: true, allow_blank: false }.freeze
Instance Attribute Summary collapse
-
#calculated_index_name ⇒ Object
readonly
Returns the value of attribute calculated_index_name.
-
#field ⇒ Object
readonly
Returns the value of attribute field.
Class Method Summary collapse
Instance Method Summary collapse
- #case_sensitive ⇒ Boolean|nil
- #column_key ⇒ String
- #columns ⇒ Array<String>
- #handle_unique_error(instance) ⇒ Object
- #index_key ⇒ String
- #index_name ⇒ String|Symbol|nil
-
#initialize(field, options, adapter) ⇒ UniquenessOptions
constructor
A new instance of UniquenessOptions.
- #message ⇒ String|nil
- #scope ⇒ Array<String|Symbol>
- #where_clause ⇒ String|nil
Constructor Details
#initialize(field, options, adapter) ⇒ UniquenessOptions
Returns a new instance of UniquenessOptions.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 16 def initialize(field, , adapter) @field = field @options = @adapter = adapter return if ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK'] index = responsible_index raise_if_index_missed!(index) @calculated_index_name = index.name end |
Instance Attribute Details
#calculated_index_name ⇒ Object (readonly)
Returns the value of attribute calculated_index_name.
14 15 16 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 14 def calculated_index_name @calculated_index_name end |
#field ⇒ Object (readonly)
Returns the value of attribute field.
14 15 16 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 14 def field @field end |
Class Method Details
.validator_options(attributes, options) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 6 def self.(attributes, ) DEFAULT_OPTIONS .merge(attributes: attributes) .merge() .except(*CUSTOM_OPTIONS) .tap { |opts| opts[:conditions] = -> { where([:where]) } if [:where] } end |
Instance Method Details
#case_sensitive ⇒ Boolean|nil
74 75 76 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 74 def case_sensitive @case_sensitive ||= [:case_sensitive] end |
#column_key ⇒ String
44 45 46 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 44 def column_key @column_key ||= Helpers.generate_key_for_uniqueness(columns) end |
#columns ⇒ Array<String>
49 50 51 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 49 def columns @columns ||= Helpers.unify_columns(field, scope) end |
#handle_unique_error(instance) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 31 def handle_unique_error(instance) = .except(:case_sensitive, :scope, :conditions, :attributes, *CUSTOM_OPTIONS) [:value] = instance.public_send([:attributes]) instance.errors.add([:attributes], :taken, ) end |
#index_key ⇒ String
39 40 41 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 39 def index_key @index_key ||= Helpers.generate_key_for_uniqueness_index(index_name || calculated_index_name) end |
#index_name ⇒ String|Symbol|nil
69 70 71 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 69 def index_name @index_name ||= [:index_name] end |
#message ⇒ String|nil
59 60 61 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 59 def @message ||= [:message] end |
#scope ⇒ Array<String|Symbol>
64 65 66 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 64 def scope @scope ||= Array.wrap([:scope]) end |
#where_clause ⇒ String|nil
54 55 56 |
# File 'lib/database_validations/lib/validates_db_uniqueness_of/uniqueness_options.rb', line 54 def where_clause @where_clause ||= [:where] end |