Module: ActiveModel::ErrorCollecting::Emulation
- Included in:
- Errors
- Defined in:
- lib/active_model/error_collecting/emulation.rb
Constant Summary collapse
- MODEL_METHODS =
[ :clear, :include?, :get, :set, :delete, :[], :[]=, :each, :size, :values, :keys, :count, :empty?, :any?, :added?, :entries ]
- MESSAGE_REPORTER_METHODS =
[ :full_messages, :full_message, :generate_message ]
Class Method Summary collapse
Instance Method Summary collapse
- #add(attribute, message = nil, options = {}) ⇒ Object
- #add_on_blank(attributes, options = {}) ⇒ Object
- #add_on_empty(attributes, options = {}) ⇒ Object
- #as_json(options = nil) ⇒ Object
- #to_xml(options = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/active_model/error_collecting/emulation.rb', line 19 def self.included(base) base.class_eval do extend Forwardable def_delegators :error_collection, *MODEL_METHODS def_delegators :message_reporter, *MESSAGE_REPORTER_METHODS def_delegators :hash_reporter, :to_hash def_delegators :array_reporter, :to_a alias_method :blank?, :empty? alias_method :has_key?, :include? end end |
Instance Method Details
#add(attribute, message = nil, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/active_model/error_collecting/emulation.rb', line 47 def add(attribute, =nil, = {}) if [:strict] error = ErrorMessage.build(attribute, , ) = HumanMessageFormatter.new(@base, error). raise ActiveModel::StrictValidationFailed, (attribute, ) end error_collection.add attribute, , end |
#add_on_blank(attributes, options = {}) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/active_model/error_collecting/emulation.rb', line 40 def add_on_blank(attributes, = {}) [attributes].flatten.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
32 33 34 35 36 37 38 |
# File 'lib/active_model/error_collecting/emulation.rb', line 32 def add_on_empty(attributes, = {}) [attributes].flatten.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 |
#as_json(options = nil) ⇒ Object
60 61 62 |
# File 'lib/active_model/error_collecting/emulation.rb', line 60 def as_json(=nil) to_hash end |
#to_xml(options = {}) ⇒ Object
56 57 58 |
# File 'lib/active_model/error_collecting/emulation.rb', line 56 def to_xml(={}) to_a.to_xml .reverse_merge(:root => "errors", :skip_types => true) end |