Class: AwesomeTranslations::Handlers::ValidationsHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/awesome_translations/handlers/validations_handler.rb

Instance Method Summary collapse

Methods inherited from BaseHandler

#add_translations_for_hash, #enabled?, #translations_for_hash_recursive

Instance Method Details

#groupsObject



2
3
4
5
6
7
8
9
10
11
# File 'lib/awesome_translations/handlers/validations_handler.rb', line 2

def groups
  ArrayEnumerator.new do |yielder|
    AwesomeTranslations::ModelInspector.model_classes.each do |model_inspector|
      yielder << AwesomeTranslations::Group.new(
        id: model_inspector.clazz.name,
        handler: self
      )
    end
  end
end

#translations_for_group(group) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/awesome_translations/handlers/validations_handler.rb', line 13

def translations_for_group(group)
  ArrayEnumerator.new do |yielder|
    model_inspector = AwesomeTranslations::ModelInspector.model_classes.find { |model_inspector_i| model_inspector_i.clazz.name == group.name }
    raise "No inspector by that name: #{group.name}" unless model_inspector

    model_inspector.clazz._validators.each do |attribute_name, validators|
      validators.each do |validator|
        if validator.is_a?(ActiveModel::Validations::LengthValidator)
          translations_for_length_validator(validator, model_inspector, attribute_name, yielder)
        elsif validator.is_a?(ActiveModel::Validations::FormatValidator)
          translations_for_format_validator(validator, model_inspector, attribute_name, yielder)
        elsif validator.is_a?(ActiveRecord::Validations::UniquenessValidator)
          translations_for_uniqueness_validator(validator, model_inspector, attribute_name, yielder)
        elsif validator.class.name == "ActiveRecord::Validations::PresenceValidator" # rubocop:disable Style/ClassEqualityComparison:
          translations_for_presence_validator(validator, model_inspector, attribute_name, yielder)
        elsif validator.class.name == "EmailValidator" # rubocop:disable Style/ClassEqualityComparison:
          translations_for_email_validator(validator, model_inspector, attribute_name, yielder)
        elsif validator.class.name == "ActiveModel::Validations::ConfirmationValidator" # rubocop:disable Style/ClassEqualityComparison:
          translations_for_confirmation_validator(validator, model_inspector, attribute_name, yielder)
        else
          Rails.logger.error "Unhandeled validator: #{validator.class.name}"
        end
      end
    end
  end
end