Class: ActiveModel::Hints
- Inherits:
-
Object
- Object
- ActiveModel::Hints
- Includes:
- Enumerable
- Defined in:
- lib/active_model/hints.rb
Overview
Introspects declared validators and builds proactive hint messages (before valid? fails).
Conditional options (:if, :unless, :on) are not evaluated; hints reflect static rules. For format validators, prefer a custom message: or per-attribute I18n keys.
Constant Summary collapse
- MESSAGES_FOR_OPTIONS =
%w[ within in is minimum maximum greater_than greater_than_or_equal_to equal_to less_than less_than_or_equal_to odd even only_integer ].freeze
- VALIDATORS_WITHOUT_MAIN_KEYS =
%w[exclusion format inclusion length numericality].freeze
- RANGE_OPTIONS =
%w[within in].freeze
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
Instance Method Summary collapse
- #[](attribute) ⇒ Object
- #[]=(attribute, hint) ⇒ Object
- #add(attribute, message = :invalid, options = {}) ⇒ Object
- #add_on_blank(attributes, options = {}) ⇒ Object
- #add_on_empty(attributes, options = {}) ⇒ Object
- #added?(attribute, message = :invalid, options = {}) ⇒ Boolean
- #as_json(_options = nil) ⇒ Object
- #clear ⇒ Object
- #count ⇒ Object
- #delete(key) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean (also: #blank?)
- #full_message(attribute, message) ⇒ Object
- #full_messages ⇒ Object
- #full_messages_for(attribute) ⇒ Object
- #generate_message(attribute, type, options = {}) ⇒ Object
- #get(key) ⇒ Object
- #hints_for(attribute) ⇒ Object
- #include?(attribute) ⇒ Boolean (also: #has_key?)
-
#initialize(base) ⇒ Hints
constructor
A new instance of Hints.
- #initialize_dup(other) ⇒ Object
- #keys ⇒ Object
- #set(key, value) ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
- #to_hash ⇒ Object
- #to_xml(options = {}) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(base) ⇒ Hints
Returns a new instance of Hints.
22 23 24 25 26 27 28 |
# File 'lib/active_model/hints.rb', line 22 def initialize(base) @base = base @messages = {} attribute_names_for_hints.each do |attribute| @messages[attribute] = hints_for(attribute) end end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
20 21 22 |
# File 'lib/active_model/hints.rb', line 20 def @messages end |
Instance Method Details
#[](attribute) ⇒ Object
68 69 70 |
# File 'lib/active_model/hints.rb', line 68 def [](attribute) get(attribute.to_sym) || set(attribute.to_sym, []) end |
#[]=(attribute, hint) ⇒ Object
72 73 74 |
# File 'lib/active_model/hints.rb', line 72 def []=(attribute, hint) self[attribute] << hint end |
#add(attribute, message = :invalid, options = {}) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/active_model/hints.rb', line 119 def add(attribute, = :invalid, = {}) = (attribute, , ) if [:strict] raise ActiveModel::StrictValidationFailed, (attribute, ) end self[attribute] << end |
#add_on_blank(attributes, options = {}) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/active_model/hints.rb', line 136 def add_on_blank(attributes, = {}) Array(attributes).each do |attribute| value = @base.send(:read_attribute_for_validation, attribute) add(attribute, :blank, ) if value.blank? end end |
#add_on_empty(attributes, options = {}) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/active_model/hints.rb', line 128 def add_on_empty(attributes, = {}) Array(attributes).each do |attribute| value = @base.send(:read_attribute_for_validation, attribute) is_empty = value.respond_to?(:empty?) ? value.empty? : false add(attribute, :empty, ) if value.nil? || is_empty end end |
#added?(attribute, message = :invalid, options = {}) ⇒ Boolean
143 144 145 146 |
# File 'lib/active_model/hints.rb', line 143 def added?(attribute, = :invalid, = {}) = (attribute, , ) self[attribute].include?() end |
#as_json(_options = nil) ⇒ Object
111 112 113 |
# File 'lib/active_model/hints.rb', line 111 def as_json( = nil) to_hash end |
#clear ⇒ Object
47 48 49 |
# File 'lib/active_model/hints.rb', line 47 def clear .clear end |
#count ⇒ Object
98 99 100 |
# File 'lib/active_model/hints.rb', line 98 def count to_a.size end |
#delete(key) ⇒ Object
64 65 66 |
# File 'lib/active_model/hints.rb', line 64 def delete(key) .delete(key) end |
#each ⇒ Object
76 77 78 79 80 |
# File 'lib/active_model/hints.rb', line 76 def each .each_key do |attribute| self[attribute].each { |hint| yield attribute, hint } end end |
#empty? ⇒ Boolean Also known as: blank?
102 103 104 |
# File 'lib/active_model/hints.rb', line 102 def empty? .values.all?(&:empty?) end |
#full_message(attribute, message) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/active_model/hints.rb', line 152 def (attribute, ) return if attribute == :base attr_name = attribute.to_s.tr(".", "_").humanize attr_name = @base.class.human_attribute_name(attribute, default: attr_name, base: @base) format_defaults = i18n_format_defaults format_key = format_defaults.shift I18n.t( format_key, default: format_defaults, attribute: attr_name, message: ) end |
#full_messages ⇒ Object
148 149 150 |
# File 'lib/active_model/hints.rb', line 148 def map { |attribute, | (attribute, ) } end |
#full_messages_for(attribute) ⇒ Object
39 40 41 |
# File 'lib/active_model/hints.rb', line 39 def (attribute) hints_for(attribute).map { || (attribute, ) } end |
#generate_message(attribute, type, options = {}) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/active_model/hints.rb', line 169 def (attribute, type, = {}) = .dup type = .delete(:message) if [:message].is_a?(Symbol) value = (attribute != :base ? @base.read_attribute_for_validation(attribute) : nil) interpolation = { model: @base.model_name.human, attribute: @base.class.human_attribute_name(attribute, base: @base), value: value, object: @base, count: [:count], minimum: [:minimum], maximum: [:maximum] }.compact defaults = i18n_defaults(attribute, type, ) key = defaults.shift I18n.translate(key, **interpolation.merge(default: defaults)) end |
#get(key) ⇒ Object
56 57 58 |
# File 'lib/active_model/hints.rb', line 56 def get(key) [key] end |
#hints_for(attribute) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/active_model/hints.rb', line 30 def hints_for(attribute) attribute = attribute.to_sym result = [] @base.class.validators_on(attribute).each do |validator| result.concat((attribute, validator)) end result end |
#include?(attribute) ⇒ Boolean Also known as: has_key?
51 52 53 |
# File 'lib/active_model/hints.rb', line 51 def include?(attribute) (value = [attribute.to_sym]) && value.any? end |
#initialize_dup(other) ⇒ Object
43 44 45 |
# File 'lib/active_model/hints.rb', line 43 def initialize_dup(other) @messages = other..transform_values(&:dup) end |
#keys ⇒ Object
90 91 92 |
# File 'lib/active_model/hints.rb', line 90 def keys .keys end |
#set(key, value) ⇒ Object
60 61 62 |
# File 'lib/active_model/hints.rb', line 60 def set(key, value) [key] = value end |
#size ⇒ Object
82 83 84 |
# File 'lib/active_model/hints.rb', line 82 def size values.flatten.size end |
#to_a ⇒ Object
94 95 96 |
# File 'lib/active_model/hints.rb', line 94 def to_a end |
#to_hash ⇒ Object
115 116 117 |
# File 'lib/active_model/hints.rb', line 115 def to_hash .dup end |
#to_xml(options = {}) ⇒ Object
107 108 109 |
# File 'lib/active_model/hints.rb', line 107 def to_xml( = {}) to_a.to_xml(.reverse_merge(root: "hints", skip_types: true)) end |
#values ⇒ Object
86 87 88 |
# File 'lib/active_model/hints.rb', line 86 def values .values end |