lib/lipstick/auto_validation.rb in aaf-lipstick-1.1.0 vs lib/lipstick/auto_validation.rb in aaf-lipstick-2.0.0
- old
+ new
@@ -1,69 +1,73 @@
-require 'active_model'
-
+# frozen_string_literal: true
module Lipstick
module AutoValidation
module ClassMethods
def self.extended(base)
return if base.respond_to?(:validators)
- fail('Lipstick::AutoValidation requires a class which responds' \
+ raise('Lipstick::AutoValidation requires a class which responds' \
' to the `validators` method. For example, as provided by' \
' ActiveModel::Validations')
end
def lipstick_auto_validators
validators.each_with_object({}) do |validator, map|
validator.attributes.each do |attr|
- out = semantic_ui_validator(attr, validator)
+ out = lipstick_validator(attr, validator)
next if out.nil?
map[attr.to_sym] ||= {}
map[attr.to_sym].merge!(out)
end
end
end
+ def lipstick_field_name(attr)
+ map = @lipstick_field_names || {}
+ return map[attr] if map.key?(attr)
+ attr.to_s.humanize(capitalize: false)
+ end
+
private
v = ActiveModel::Validations
VALIDATOR_TRANSLATORS = {
- v::PresenceValidator => :semantic_presence_validator,
- v::LengthValidator => :semantic_length_validator,
- v::NumericalityValidator => :semantic_numericality_validator
- }
+ v::PresenceValidator => :lipstick_presence_validator,
+ v::LengthValidator => :lipstick_length_validator,
+ v::NumericalityValidator => :lipstick_numericality_validator
+ }.freeze
private_constant :VALIDATOR_TRANSLATORS
- def semantic_ui_validator(attr, validator)
+ def lipstick_validator(attr, validator)
VALIDATOR_TRANSLATORS.each do |klass, sym|
next unless validator.is_a?(klass)
- return send(sym, attr, validator,
- attr.to_s.humanize(capitalize: false))
+ return send(sym, attr, validator, lipstick_field_name(attr.to_sym))
end
nil
end
- def semantic_presence_validator(_attr, _validator, humanized)
- { empty: "Please enter a value for #{humanized}" }
+ def lipstick_presence_validator(_attr, _validator, humanized)
+ { required: { message: "Please enter a value for #{humanized}" } }
end
- def semantic_length_validator(_attr, validator, humanized)
+ def lipstick_length_validator(_attr, validator, humanized)
min = validator.options[:minimum]
max = validator.options[:maximum]
- min_message = "Please enter a longer value for #{humanized}" \
- " (minimum #{min} characters)"
- max_message = "Please enter a shorter value for #{humanized}" \
- " (maximum #{max} characters)"
+ min_message = "Please enter a longer value for #{humanized} " \
+ "(minimum #{min} characters)"
+ max_message = "Please enter a shorter value for #{humanized} " \
+ "(maximum #{max} characters)"
{}.tap do |out|
- out["length[#{min}]"] = min_message if min
- out["maxLength[#{max}]"] = max_message if max
+ out[:minlength] = { param: min, message: min_message } if min
+ out[:maxlength] = { param: max, message: max_message } if max
end
end
- def semantic_numericality_validator(_attr, _validator, humanized)
- { integer: "Please enter a numeric value for #{humanized}" }
+ def lipstick_numericality_validator(_attr, _validator, humanized)
+ { digits: { message: "Please enter a numeric value for #{humanized}" } }
end
end
def self.included(base)
base.send(:extend, ClassMethods)