Sha256: b8ab18e9b56b67fb1a23b81b324ab7386b29029fc77700cada292eb4550c0458

Contents?: true

Size: 1.96 KB

Versions: 34

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacRailsUtils
  module Models
    module Validations
      common_concern

      class << self
        def column_errors(model_or_record, column, value)
          model = model_class_or_record_to_model(model_or_record)
          record = model.new
          model.validators_on(column).each do |validator|
            next if validator.options[:allow_blank] && value.blank?
            next if validator.options[:allow_nil] && value.nil?

            validator.validate_each(record, column, value)
          end
          record.errors
        end

        def column_required?(model_or_record, column)
          !column_valid?(model_or_record, column, nil)
        end

        def column_valid?(model_or_record, column, value)
          column_errors(model_or_record, column, value).empty?
        end

        private

        def model_class_or_record_to_model(model_class_or_record)
          model_class_or_record.is_a?(::Class) ? model_class_or_record : model_class_or_record.class
        end
      end

      module ClassMethods
        def column_errors(column, value)
          ::EacRailsUtils::Models::Validations.column_errors(self, column, value)
        end

        def column_required?(column)
          ::EacRailsUtils::Models::Validations.column_required?(self, column)
        end

        def column_valid?(attribute, value)
          column_errors(attribute, value).empty?
        end

        private

        def model_class_or_record_to_model(model_class_or_record)
          model_class_or_record.is_a?(::Class) ? model_class_or_record : model_class_or_record.class
        end
      end

      def attribute_errors(attribute)
        self.class.column_errors(attribute, send(attribute))
      end

      def attribute_required?(attribute)
        self.class.column_required?(attribute)
      end

      def attribute_valid?(attribute)
        attribute_errors(attribute).empty?
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
eac_rails_utils-0.25.0 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.24.0 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.23.4 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.23.3 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.23.2 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.23.1 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.23.0 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.22.3 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.22.2 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.22.1 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.22.0 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.21.0 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.20.0 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.19.0 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.18.0 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.17.2 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.17.1 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.17.0 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.16.0 lib/eac_rails_utils/models/validations.rb
eac_rails_utils-0.15.2 lib/eac_rails_utils/models/validations.rb