Class: ActionSpec::ValidationResult
- Inherits:
-
Object
- Object
- ActionSpec::ValidationResult
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Defined in:
- lib/action_spec/validation_result.rb
Constant Summary collapse
- BUILT_IN_SCOPES =
%i[path query body headers cookies].freeze
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#px ⇒ Object
readonly
Returns the value of attribute px.
Class Method Summary collapse
- .empty_px ⇒ Object
- .human_attribute_name(attribute, options = {}) ⇒ Object
- .lookup_ancestors ⇒ Object
- .model_name ⇒ Object
Instance Method Summary collapse
- #add_error(attribute, type, message: nil, **options) ⇒ Object
- #apply_scope_options!(options_by_scope) ⇒ Object
- #assign(location, key, value, scopes: []) ⇒ Object
-
#initialize ⇒ ValidationResult
constructor
A new instance of ValidationResult.
- #invalid? ⇒ Boolean
- #read_attribute_for_validation(_attribute) ⇒ Object
Constructor Details
#initialize ⇒ ValidationResult
Returns a new instance of ValidationResult.
18 19 20 21 |
# File 'lib/action_spec/validation_result.rb', line 18 def initialize @errors = ActiveModel::Errors.new(self) @px = build_px end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
16 17 18 |
# File 'lib/action_spec/validation_result.rb', line 16 def errors @errors end |
#px ⇒ Object (readonly)
Returns the value of attribute px.
16 17 18 |
# File 'lib/action_spec/validation_result.rb', line 16 def px @px end |
Class Method Details
.empty_px ⇒ Object
11 12 13 |
# File 'lib/action_spec/validation_result.rb', line 11 def empty_px new.px end |
.human_attribute_name(attribute, options = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/action_spec/validation_result.rb', line 65 def self.human_attribute_name(attribute, = {}) key = attribute.to_s defaults = [ :"activemodel.attributes.#{model_name.i18n_key}.#{key}", :"activemodel.attributes.#{model_name.i18n_key}.#{key.tr('.', '_')}", key.tr(".", " ").humanize ] I18n.translate(defaults.shift, **, default: defaults) end |
.lookup_ancestors ⇒ Object
57 58 59 |
# File 'lib/action_spec/validation_result.rb', line 57 def self.lookup_ancestors [self] end |
.model_name ⇒ Object
61 62 63 |
# File 'lib/action_spec/validation_result.rb', line 61 def self.model_name @model_name ||= ActiveModel::Name.new(self, nil, "ActionSpec::Parameters") end |
Instance Method Details
#add_error(attribute, type, message: nil, **options) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/action_spec/validation_result.rb', line 43 def add_error(attribute, type, message: nil, **) return errors.add(attribute, ) if if ( = ActionSpec.config.(attribute, type, )) errors.add(attribute, ) else errors.add(attribute, type, **) end end |
#apply_scope_options!(options_by_scope) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/action_spec/validation_result.rb', line 33 def () .each do |scope_name, | bucket = px.scope[scope_name] next unless bucket bucket.compact! if [:compact] bucket.delete_if { |_key, value| value.blank? } if [:compact_blank] end end |
#assign(location, key, value, scopes: []) ⇒ Object
27 28 29 30 31 |
# File 'lib/action_spec/validation_result.rb', line 27 def assign(location, key, value, scopes: []) bucket(location)[key] = value px[key] = value if root_bucket?(location) Array(scopes).each { |scope_name| scope_bucket(scope_name)[key] = value } end |
#invalid? ⇒ Boolean
23 24 25 |
# File 'lib/action_spec/validation_result.rb', line 23 def invalid? errors.any? end |
#read_attribute_for_validation(_attribute) ⇒ Object
53 54 55 |
# File 'lib/action_spec/validation_result.rb', line 53 def read_attribute_for_validation(_attribute) nil end |