Module: ActiveModel::BetterErrors::Emulation
- Included in:
- Errors
- Defined in:
- lib/active_model/better_errors/emulation.rb
Overview
Emulation The ActiveModel Emulation Layer
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
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/active_model/better_errors/emulation.rb', line 25 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
53 54 55 56 57 58 59 60 61 |
# File 'lib/active_model/better_errors/emulation.rb', line 53 def add(attribute, = nil, = {}) if [:strict] error = ErrorMessage.build(attribute, , ) = ::ActiveModel::BetterErrors.(@base, error) = (attribute, ) fail ActiveModel::StrictValidationFailed, end error_collection.add attribute, , end |
#add_on_blank(attributes, options = {}) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/active_model/better_errors/emulation.rb', line 46 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
38 39 40 41 42 43 44 |
# File 'lib/active_model/better_errors/emulation.rb', line 38 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
67 68 69 |
# File 'lib/active_model/better_errors/emulation.rb', line 67 def as_json( = nil) to_hash end |
#to_xml(options = {}) ⇒ Object
63 64 65 |
# File 'lib/active_model/better_errors/emulation.rb', line 63 def to_xml( = {}) to_a.to_xml .reverse_merge(root: 'errors', skip_types: true) end |