Sha256: 25574a5e882c5bd01cd6e7e804952434bd28ea7ab0a8924cbc595387f8d4411a

Contents?: true

Size: 1.01 KB

Versions: 17

Compression:

Stored size: 1.01 KB

Contents

module CsvRowModel
  module Validators
    # adds validates_attributes method to validate the attributes of an attributes
    module ValidateAttributes
      extend ActiveSupport::Concern

      class AttributeValidator < ActiveModel::EachValidator # :nodoc:
        def validate_each(record, attribute, value)
          return unless value && (record.try(:using_warnings?) ? value.unsafe? : value.invalid?)
          record.errors.add(attribute)
        end
      end

      class_methods do
        protected

        # Adds validation check to add errors any attribute of `attributes` passed is truthy and invalid.
        # Inspired by: https://github.com/rails/rails/blob/2bb0abbec0e4abe843131f188129a1189b1bf714/activerecord/lib/active_record/validations/associated.rb#L46
        #
        # @param [Array<Symbol>] attributes array of attributes to validate their attributes
        def validate_attributes(*attributes)
          validates_with AttributeValidator, { attributes: attributes }
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
csv_row_model-1.0.0.beta1 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.4.1 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.4.0 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.10 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.9 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.8 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.7 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.6 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.5 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.4 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.3 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.2 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.1 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.3.0 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.2.1 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.1.1 lib/csv_row_model/validators/validate_attributes.rb
csv_row_model-0.1.0 lib/csv_row_model/validators/validate_attributes.rb