-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CRIMAPP-1403] Update payment fieldset form validation (#1307)
* Update payment fieldset form validation * Fix failing specs * Fix linting error * Fix failing spec * Refactor * Code coverage fix
- Loading branch information
Showing
19 changed files
with
156 additions
and
381 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Steps | ||
module PaymentFieldsetValidation | ||
extend ActiveSupport::Concern | ||
|
||
def validate_frequency | ||
errors.add(:frequency, :blank, payment_type: payment_type_label) if frequency.blank? | ||
return unless frequencies.exclude?(frequency) | ||
|
||
errors.add(:frequency, :inclusion, | ||
payment_type: payment_type_label&.capitalize) | ||
end | ||
|
||
def validate_amount | ||
errors.add(:amount, :blank, payment_type: payment_type_label) if amount.blank? | ||
errors.add(:amount, :not_a_number, payment_type: payment_type_label) if Type::Pence.new.serialize(amount).nil? | ||
errors.add(:amount, :greater_than, payment_type: payment_type_label) if amount.to_i <= 0 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class BasePaymentsValidator < ActiveModel::Validator | ||
attr_reader :record | ||
|
||
def validate(record) | ||
@record = record | ||
|
||
record.types.each do |type| | ||
next if type == 'none' | ||
|
||
payment = record.public_send(type) | ||
add_errors(payment) unless payment.valid? | ||
end | ||
|
||
return unless record.types.empty? | ||
|
||
record.errors.add(:base, :none_selected) if has_no_payments? | ||
end | ||
|
||
# :nocov: | ||
def has_no_payments? | ||
raise 'must be implemented in subclasses' | ||
end | ||
# :nocov: | ||
|
||
private | ||
|
||
def add_errors(payment) | ||
payment.errors.each do |error| | ||
attr_name = "#{payment.payment_type.dasherize}-#{error.attribute}" | ||
record.errors.add(attr_name, error.type, message: error.message) | ||
|
||
# We define the attribute getter as it doesn't really exist | ||
record.define_singleton_method(attr_name) do | ||
payment.public_send(error.attribute) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,5 @@ | ||
class IncomeBenefitsValidator < ActiveModel::Validator | ||
attr_reader :record | ||
|
||
def validate(record) | ||
@record = record | ||
|
||
record.types.each_with_index do |type, index| | ||
next if type == 'none' | ||
|
||
income_benefit = record.public_send(type) | ||
add_indexed_errors(income_benefit, index) unless income_benefit.valid? | ||
end | ||
|
||
return unless record.types.empty? | ||
|
||
record.errors.add(:base, :none_selected) if record.has_no_income_benefits.blank? | ||
end | ||
|
||
private | ||
|
||
def add_indexed_errors(income_benefit, index) | ||
income_benefit.errors.each do |error| | ||
attr_name = indexed_attribute(index, income_benefit, error.attribute) | ||
|
||
record.errors.add( | ||
attr_name, | ||
error.type, | ||
message: error_message(income_benefit, error) | ||
) | ||
|
||
# We define the attribute getter as it doesn't really exist | ||
record.define_singleton_method(attr_name) do | ||
income_benefit.public_send(error.attribute) | ||
end | ||
end | ||
end | ||
|
||
def indexed_attribute(_index, income_benefit, attr) | ||
"#{income_benefit.payment_type.dasherize}-#{attr}" | ||
end | ||
|
||
# `activemodel.errors.models.steps/income/income_benefit_fieldset_form.summary.x.y` | ||
def error_message(obj, error) | ||
payment_type = I18n.t( | ||
obj.payment_type, | ||
scope: [:helpers, :label, :steps_income_income_benefits_form, :types_options] | ||
) | ||
payment_type&.downcase! if obj.payment_type == IncomeBenefitType::OTHER.to_s | ||
|
||
I18n.t( | ||
"#{obj.model_name.i18n_key}.summary.#{error.attribute}.#{error.type}", | ||
scope: [:activemodel, :errors, :models], | ||
payment_type: payment_type | ||
) | ||
class IncomeBenefitsValidator < BasePaymentsValidator | ||
def has_no_payments? | ||
record.has_no_income_benefits.blank? | ||
end | ||
end |
Oops, something went wrong.