Class: Perfectline::ValidatesExistence::Validator::ExistenceValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- Perfectline::ValidatesExistence::Validator::ExistenceValidator
- Defined in:
- lib/validates_existence.rb
Instance Method Summary collapse
- #exists?(target_class, value) ⇒ Boolean
-
#initialize(options) ⇒ ExistenceValidator
constructor
A new instance of ExistenceValidator.
- #validate(record) ⇒ Object
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ ExistenceValidator
Returns a new instance of ExistenceValidator.
8 9 10 11 12 13 14 |
# File 'lib/validates_existence.rb', line 8 def initialize() # set the default message if its unspecified [:message] ||= :existence [:both] = true unless .key?(:both) [:allow_new] = false unless .key?(:allow_new) super() end |
Instance Method Details
#exists?(target_class, value) ⇒ Boolean
70 71 72 |
# File 'lib/validates_existence.rb', line 70 def exists?(target_class, value) ([:allow_new] && value.new_record?) || target_class.exists?(value) end |
#validate(record) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/validates_existence.rb', line 16 def validate(record) attributes.each do |attribute| value = record.read_attribute_for_validation(attribute) target_id = if !attribute.match(/_id$/) && [:allow_nil] association = record.class.reflect_on_association(attribute) association.respond_to?(:foreign_key) ? record[association.foreign_key] : record[association.association_foreign_key] else value end next if (target_id.nil? && value.nil? && [:allow_nil]) || (value.blank? && [:allow_blank]) validate_each(record, attribute, value) end end |
#validate_each(record, attribute, value) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/validates_existence.rb', line 30 def validate_each(record, attribute, value) normalized = attribute.to_s.sub(/_id$/, "").to_sym association = record.class.reflect_on_association(normalized) if association.nil? || !association.belongs_to? raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association" end target_class = nil # dealing with polymorphic belongs_to if association.[:polymorphic] foreign_type = record.send(association.[:foreign_type] || association.foreign_type) target_class = foreign_type.constantize unless foreign_type.nil? else target_class = association.klass end if value.nil? || target_class.nil? || !exists?(target_class, value) errors = [attribute] # add the error on both :relation and :relation_id if [:both] if ActiveRecord::VERSION::MAJOR > 3 || (ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR >= 1) foreign_key = association.foreign_key else foreign_key = association.primary_key_name end errors << (attribute.to_s.ends_with?("_id") ? normalized : foreign_key) end = [:"#{record.class.i18n_scope}.errors.messages.existence", "does not exist"] errors.each do |error| record.errors.add(error, [:message], :message => ) end end end |