Class: OverlapValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- OverlapValidator
- Defined in:
- lib/validates_overlap/overlap_validator.rb
Defined Under Namespace
Classes: UnsupportedColumnType
Instance Method Summary collapse
-
#initialize(args) ⇒ OverlapValidator
constructor
A new instance of OverlapValidator.
-
#validate(record) ⇒ Object
NOTE: Rails registers ONE validator instance per model class, shared by every validation of that class (including concurrent ones) — so the query being built must never be stored on the validator itself (issue #50).
Constructor Details
#initialize(args) ⇒ OverlapValidator
Returns a new instance of OverlapValidator.
10 11 12 13 14 |
# File 'lib/validates_overlap/overlap_validator.rb', line 10 def initialize(args) attributes_are_range(args[:attributes]) super end |
Instance Method Details
#validate(record) ⇒ Object
NOTE: Rails registers ONE validator instance per model class, shared by every validation of that class (including concurrent ones) — so the query being built must never be stored on the validator itself (issue #50)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/validates_overlap/overlap_validator.rb', line 19 def validate(record) reject_unsupported_column_types(record) relation, sql_conditions, sql_values = initialize_query(record, ) if overlapped_exists?(relation, sql_conditions, sql_values) if [:load_overlapped] record.instance_variable_set(:@overlapped_records, get_overlapped(relation, sql_conditions, sql_values)) end if record.respond_to? attributes.first if [:message_title].is_a?(Array) [:message_title].each do |key| record.errors.add(key, [:message_content] || :overlap) end else record.errors.add([:message_title] || attributes.first, [:message_content] || :overlap) end else record.errors.add([:message_title] || :base, [:message_content] || :overlap) end end end |