Module | ActiveRecord::Validations::ClassMethods
In: | lib/gloc-rails.rb

### Methods

validates_length_of validates_size_of

### Public Instance methods

validates_length_of(*attrs)

The default Rails version of this function creates an error message and then passes it to ActiveRecord.Errors. The GLoc version of this method, sends an array to ActiveRecord.Errors that will be turned into a string by ActiveRecord.Errors which in turn allows for the message of this validation function to be a GLoc string key.

[Source]

         # File lib/gloc-rails.rb, line 164
    164:       def validates_length_of(*attrs)
    165:         # Merge given options with defaults.
    166:         options = {
    167:           :too_long     => ActiveRecord::Errors.default_error_messages[:too_long],
    168:           :too_short    => ActiveRecord::Errors.default_error_messages[:too_short],
    169:           :wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length]
    170:         }.merge(DEFAULT_VALIDATION_OPTIONS)
    171:         options.update(attrs.pop.symbolize_keys) if attrs.last.is_a?(Hash)
    172:
    173:         # Ensure that one and only one range option is specified.
    174:         range_options = ALL_RANGE_OPTIONS & options.keys
    175:         case range_options.size
    176:           when 0
    177:             raise ArgumentError, 'Range unspecified.  Specify the :within, :maximum, :minimum, or :is option.'
    178:           when 1
    179:             # Valid number of options; do nothing.
    180:           else
    181:             raise ArgumentError, 'Too many range options specified.  Choose only one.'
    182:         end
    183:
    184:         # Get range option and value.
    185:         option = range_options.first
    186:         option_value = options[range_options.first]
    187:
    188:         case option
    189:         when :within, :in
    190:           raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range)
    191:
    192:           too_short = [options[:too_short] , option_value.begin]
    193:           too_long  = [options[:too_long]  , option_value.end  ]
    194:
    195:           validates_each(attrs, options) do |record, attr, value|
    196:             if value.nil? or value.split(//).size < option_value.begin
    197:               record.errors.add(attr, too_short)
    198:             elsif value.split(//).size > option_value.end
    199:               record.errors.add(attr, too_long)
    200:             end
    201:           end
    202:         when :is, :minimum, :maximum
    203:           raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0
    204:
    205:           # Declare different validations per option.
    206:           validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" }
    207:           message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }
    208:
    209:           message = [(options[:message] || options[message_options[option]]) , option_value]
    210:
    211:           validates_each(attrs, options) do |record, attr, value|
    212:             if value.kind_of?(String)
    213:               record.errors.add(attr, message) unless !value.nil? and value.split(//).size.method(validity_checks[option])[option_value]
    214:             else
    215:               record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value]
    216:             end
    217:           end
    218:         end
    219:       end

validates_size_of(*attrs)

Alias for validates_length_of

[Validate]