Module: Regressor::Model::Validation::Length

Included in:
RegressionModel
Defined in:
lib/generators/regressor/model/validation/length.rb

Instance Method Summary collapse

Instance Method Details

#length_validatorsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/generators/regressor/model/validation/length.rb', line 5

def length_validators
  @model.constantize.validators.select do |validator|
    validator.class.to_s == ActiveModel::Validations::LengthValidator.to_s
  end.inject([]) do |result, validator|
    if validator.options[:minimum]
      validator.attributes.each do |attribute|
        result << "it { is_expected.to allow_value(Faker::Lorem.characters(#{validator.options[:minimum]})).for :#{attribute} }"
        result << "it { is_expected.not_to allow_value(Faker::Lorem.characters(#{validator.options[:minimum] - 1})).for :#{attribute} }"
      end
    end
    if validator.options[:maximum]
      validator.attributes.each do |attribute|
        result << "it { is_expected.to allow_value(Faker::Lorem.characters(#{validator.options[:maximum]})).for :#{attribute} }"
        result << "it { is_expected.not_to allow_value(Faker::Lorem.characters(#{validator.options[:maximum] + 1})).for :#{attribute} }"
      end
    end
    result
  end.flatten.compact.uniq.join("\n\t")
end