Module: Lipstick::AutoValidation::ClassMethods

Defined in:
lib/lipstick/auto_validation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/lipstick/auto_validation.rb', line 6

def self.extended(base)
  return if base.respond_to?(:validators)

  raise('Lipstick::AutoValidation requires a class which responds' \
        ' to the `validators` method. For example, as provided by' \
        ' ActiveModel::Validations')
end

Instance Method Details

#lipstick_auto_validatorsObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lipstick/auto_validation.rb', line 14

def lipstick_auto_validators
  validators.each_with_object({}) do |validator, map|
    validator.attributes.each do |attr|
      out = lipstick_validator(attr, validator)
      next if out.nil?

      map[attr.to_sym] ||= {}
      map[attr.to_sym].merge!(out)
    end
  end
end

#lipstick_field_name(attr) ⇒ Object



26
27
28
29
30
31
# File 'lib/lipstick/auto_validation.rb', line 26

def lipstick_field_name(attr)
  map = @lipstick_field_names || {}
  return map[attr] if map.key?(attr)

  attr.to_s.humanize(capitalize: false)
end