Sha256: ce2ffc53ee09370d0d5a7fe13fbc7d418c7e8ba4e7d65385928ad69f4a9feb1f

Contents?: true

Size: 712 Bytes

Versions: 35

Compression:

Stored size: 712 Bytes

Contents

# A custom validator "set".  It's similar to :presence validator
# Unlike :presence, :set doesn't care whether the associated record is valid or not, just that it is present
# Also unlike :presence, it only works on belongs_to
class SetValidator < ActiveModel::EachValidator

    def validate_each( record, attribute, value)
        association = record.class.reflect_on_association( attribute )

        if association.nil? || !association.belongs_to?
            raise ArgumentError, "Cannot validate existence on #{record.class} #{attribute}, not a :belongs_to association"
        end

        if record.send( attribute ).nil?
            record.errors.add( attribute, "is not set")
        end

    end
end

Version data entries

35 entries across 35 versions & 3 rubygems

Version Path
hippo-fw-0.9.9 lib/hippo/validators/set.rb
hippo-fw-0.9.8 lib/hippo/validators/set.rb
hippo-fw-0.9.7 lib/hippo/validators/set.rb
hippo-fw-0.9.6 lib/hippo/validators/set.rb
hippo-fw-0.9.5 lib/hippo/validators/set.rb
hippo-fw-0.9.4 lib/hippo/validators/set.rb
hippo-fw-0.9.3 lib/hippo/validators/set.rb
hippo-fw-0.9.2 lib/hippo/validators/set.rb
hippo-fw-0.9.1 lib/hippo/validators/set.rb
lanes-0.8.3 lib/lanes/validators/set.rb
lanes-0.8.2 lib/lanes/validators/set.rb
lanes-0.8.1 lib/lanes/validators/set.rb
lanes-0.8.0 lib/lanes/validators/set.rb
lanes-0.7.0 lib/lanes/validators/set.rb
lanes-0.6.1 lib/lanes/validators/set.rb
lanes-0.6.0 lib/lanes/validators/set.rb
lanes-0.5.6 lib/lanes/validators/set.rb
lanes-0.5.5 lib/lanes/validators/set.rb
lanes-0.5.0 lib/lanes/validators/set.rb
lanes-0.4.0 lib/lanes/validators/set.rb