Class: ActiveRecordAutoValidations::AutoUniqueIndex
- Inherits:
-
Object
- Object
- ActiveRecordAutoValidations::AutoUniqueIndex
- Defined in:
- app/services/active_record_auto_validations/auto_unique_index.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
Class Method Summary collapse
Instance Method Summary collapse
- #index_columns ⇒ Object
-
#initialize(columns:, index:, model_class:) ⇒ AutoUniqueIndex
constructor
A new instance of AutoUniqueIndex.
- #last_column_name ⇒ Object
- #perform ⇒ Object
- #validates_args ⇒ Object
Constructor Details
#initialize(columns:, index:, model_class:) ⇒ AutoUniqueIndex
Returns a new instance of AutoUniqueIndex.
8 9 10 11 12 |
# File 'app/services/active_record_auto_validations/auto_unique_index.rb', line 8 def initialize(columns:, index:, model_class:) @columns = columns @index = index @model_class = model_class end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
6 7 8 |
# File 'app/services/active_record_auto_validations/auto_unique_index.rb', line 6 def columns @columns end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
6 7 8 |
# File 'app/services/active_record_auto_validations/auto_unique_index.rb', line 6 def index @index end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
6 7 8 |
# File 'app/services/active_record_auto_validations/auto_unique_index.rb', line 6 def model_class @model_class end |
Class Method Details
.execute!(**args) ⇒ Object
2 3 4 |
# File 'app/services/active_record_auto_validations/auto_unique_index.rb', line 2 def self.execute!(**args) ActiveRecordAutoValidations::AutoUniqueIndex.new(**args).perform end |
Instance Method Details
#index_columns ⇒ Object
18 19 20 |
# File 'app/services/active_record_auto_validations/auto_unique_index.rb', line 18 def index_columns @index_columns ||= index.columns.map { |column_name| columns.find { |column| column.name == column_name } } end |
#last_column_name ⇒ Object
22 23 24 |
# File 'app/services/active_record_auto_validations/auto_unique_index.rb', line 22 def last_column_name @last_column_name ||= index.columns.last end |
#perform ⇒ Object
14 15 16 |
# File 'app/services/active_record_auto_validations/auto_unique_index.rb', line 14 def perform model_class.validates last_column_name.to_sym, **validates_args end |
#validates_args ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/services/active_record_auto_validations/auto_unique_index.rb', line 26 def validates_args rest_of_columns = index.columns.clone rest_of_columns.pop validates_args = {} if rest_of_columns.length.positive? validates_args[:uniqueness] ||= {} validates_args[:uniqueness][:scope] = rest_of_columns end validates_args[:allow_blank] = true if index_columns.any?(&:null) validates_args[:uniqueness] ||= true validates_args end |