class Fluffery::Forms::Validation::MessageBuilder
Public Class Methods
message_for(object, attribute, validator)
click to toggle source
# File lib/fluffery/forms/validation/base.rb, line 107 def self.message_for(object, attribute, validator) message = self.get_validation_message(validator) message = self.defaults(validator.kind) unless message.match(/translation missing/i).nil? message end
Private Class Methods
defaults(validator)
click to toggle source
# File lib/fluffery/forms/validation/base.rb, line 115 def self.defaults(validator) { :presence => "required", :format => 'invalid', :length => "wrong length", :numericality => "not a number", :uniqueness => 'taken', :confirmation => 'required', :acceptance => 'required', :inclusion => 'invalid', :exclusion => 'invalid' }[validator] end
get_validation_message(validator)
click to toggle source
# File lib/fluffery/forms/validation/base.rb, line 129 def self.get_validation_message(validator) key = { :presence => "blank", :format => 'invalid', :length => self.length_options(validator.options), :numericality => self.numericality_options(validator.options), :uniqueness => 'taken', :confirmation => 'confirmation', :acceptance => 'accepted', :inclusion => 'inclusion', :exclusion => 'exclusion' }[validator.kind] key.is_a?(Array) ? I18n.translate("errors.messages.#{key.first}").sub("%{#{:count}}", key.last.to_s) : I18n.translate("errors.messages.#{key}") end
length_options(opts)
click to toggle source
# File lib/fluffery/forms/validation/base.rb, line 144 def self.length_options(opts) if count = opts[:is] ["wrong_length", count] elsif count = opts[:minimum] ["too_short", count] elsif count = opts[:maximum] ["too_long", count] end end
numericality_options(opts)
click to toggle source
# File lib/fluffery/forms/validation/base.rb, line 154 def self.numericality_options(opts) if opts[:only_integer] 'not_a_number' elsif count = opts[:greater_than] ['greater_than', count] elsif count = opts[:greater_than_or_equal_to] ['greater_than_or_equal_to', count] elsif count = opts[:less_than] ['less_than', count] elsif count = opts[:less_than_or_equal_to] ['less_than_or_equal_to', count] elsif opts[:odd] 'odd' elsif opts[:even] 'even' else 'not_a_number' end end