Module: EacRailsUtils::Models::Validations

Defined in:
lib/eac_rails_utils/models/validations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.column_errors(model_or_record, column, value) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/eac_rails_utils/models/validations.rb', line 11

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

.column_required?(model_or_record, column) ⇒ Boolean

Returns:

  • (Boolean)

23
24
25
# File 'lib/eac_rails_utils/models/validations.rb', line 23

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

.column_valid?(model_or_record, column, value) ⇒ Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/eac_rails_utils/models/validations.rb', line 27

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

Instance Method Details

#attribute_errors(attribute) ⇒ Object


58
59
60
# File 'lib/eac_rails_utils/models/validations.rb', line 58

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

#attribute_required?(attribute) ⇒ Boolean

Returns:

  • (Boolean)

62
63
64
# File 'lib/eac_rails_utils/models/validations.rb', line 62

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

#attribute_valid?(attribute) ⇒ Boolean

Returns:

  • (Boolean)

66
67
68
# File 'lib/eac_rails_utils/models/validations.rb', line 66

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