Class: Forms::Validations::ManualRules
- Inherits:
-
Object
- Object
- Forms::Validations::ManualRules
- Defined in:
- lib/forms/validations/manual_rules.rb
Overview
Accepts an inline rules hash and produces the same data-* hash
shape that Introspector#data_attributes_for returns. Used when
a caller wants client-side validation on a form not backed by
an ActiveModel object (e.g. plain form objects, marketplace
submissions that bypass the model in test setup, etc.).
Supported shape:
ManualRules.new(
length: { maximum: 60, minimum: 3 },
presence: true,
format: { with: /\A[a-z]+\z/i },
numericality: { greater_than_or_equal_to: 0 },
inclusion: { in: %w[a b c] },
exclusion: { in: %w[admin] },
confirmation: true,
acceptance: true,
)
Instance Method Summary collapse
- #data_attributes ⇒ Object
-
#initialize(rules) ⇒ ManualRules
constructor
A new instance of ManualRules.
Constructor Details
#initialize(rules) ⇒ ManualRules
Returns a new instance of ManualRules.
24 25 26 |
# File 'lib/forms/validations/manual_rules.rb', line 24 def initialize(rules) @rules = rules end |
Instance Method Details
#data_attributes ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/forms/validations/manual_rules.rb', line 28 def data_attributes # Build a tiny ad-hoc model whose validators reproduce the # rules, then delegate to the Introspector. Saves us # reimplementing the per-validator → data-attr conversion in # two places. klass = build_adhoc_model Introspector.new(klass).data_attributes_for(:value) end |