Properly translate all error messages for StandingOrderForm

This commit is contained in:
Rob Harrington
2017-11-30 14:44:01 +11:00
parent 7ca60e1990
commit 4362ef2cea
3 changed files with 28 additions and 12 deletions

View File

@@ -12,7 +12,7 @@ class StandingOrderForm
delegate :schedule, :schedule_id, to: :standing_order
delegate :shipping_method, :shipping_method_id, :payment_method, :payment_method_id, to: :standing_order
delegate :shipping_method_id_changed?, :shipping_method_id_was, :payment_method_id_changed?, :payment_method_id_was, to: :standing_order
delegate :credit_card_id, to: :standing_order
delegate :credit_card_id, :credit_card, to: :standing_order
validates_presence_of :shop, :customer, :schedule, :payment_method, :shipping_method
validates_presence_of :bill_address, :ship_address, :begins_at
@@ -196,36 +196,36 @@ class StandingOrderForm
# Note: presence of begins_at validated on the model
return if begins_at.blank? || ends_at.blank?
return if ends_at > begins_at
errors.add(:ends_at, "must be after begins at")
errors.add(:ends_at, :after_begins_at)
end
def customer_allowed?
return unless customer
return if customer.enterprise == shop
errors[:customer] << "does not belong to #{shop.name}"
errors.add(:customer, :does_not_belong_to_shop, shop: shop.name)
end
def schedule_allowed?
return unless schedule
return if schedule.coordinators.include?(shop)
errors[:schedule] << "is not coordinated by #{shop.name}"
errors.add(:schedule, :not_coordinated_by_shop, shop: shop.name)
end
def payment_method_allowed?
return unless payment_method
if payment_method.distributors.exclude?(shop)
errors[:payment_method] << "is not available to #{shop.name}"
errors.add(:payment_method, :not_available_to_shop, shop: shop.name)
end
return if StandingOrder::ALLOWED_PAYMENT_METHOD_TYPES.include? payment_method.type
errors[:payment_method] << "must be a Cash or Stripe method"
errors.add(:payment_method, :invalid_type)
end
def shipping_method_allowed?
return unless shipping_method
return if shipping_method.distributors.include?(shop)
errors[:shipping_method] << "is not available to #{shop.name}"
errors.add(:shipping_method, :not_available_to_shop, shop: shop.name)
end
def standing_line_items_present?
@@ -245,9 +245,9 @@ class StandingOrderForm
def credit_card_ok?
return unless payment_method.andand.type == "Spree::Gateway::StripeConnect"
return errors[:credit_card] << "is required" unless credit_card_id
return errors.add(:credit_card, :blank) unless credit_card_id
return if customer.andand.user.andand.credit_card_ids.andand.include? credit_card_id
errors[:credit_card] << "is not available"
errors.add(:credit_card, :not_available)
end
def variant_ids_for_shop_and_schedule

View File

@@ -80,6 +80,20 @@ en:
standing_line_items:
at_least_one_product: "^Please add at least one product"
not_available: "^%{name} is not available from the selected schedule"
ends_at:
after_begins_at: "must be after begins at"
customer:
does_not_belong_to_shop: "does not belong to %{shop}"
schedule:
not_coordinated_by_shop: "is not coordinated by %{shop}"
payment_method:
not_available_to_shop: "is not available to %{shop}"
invalid_type: "must be a Cash or Stripe method"
shipping_method:
not_available_to_shop: "is not available to %{shop}"
credit_card:
not_available: "is not available"
blank: "is required"
devise:
confirmations:
send_instructions: "You will receive an email with instructions about how to confirm your account in a few minutes."

View File

@@ -1,4 +1,6 @@
describe StandingOrderForm do
let(:error_t_scope) { 'activemodel.errors.models.standing_order_form.attributes' }
describe "creating a new standing order" do
let!(:shop) { create(:distributor_enterprise) }
let!(:customer) { create(:customer, enterprise: shop) }
@@ -223,7 +225,7 @@ describe StandingOrderForm do
expect(payments.with_state('void').count).to be 0
expect(payments.with_state('checkout').count).to be 1
expect(payments.with_state('checkout').first.payment_method).to eq payment_method
expect(form.errors[:credit_card]).to include "is required"
expect(form.errors[:credit_card]).to include I18n.t("#{error_t_scope}.credit_card.blank")
end
end
end
@@ -239,7 +241,7 @@ describe StandingOrderForm do
expect(payments.with_state('void').count).to be 0
expect(payments.with_state('checkout').count).to be 1
expect(payments.with_state('checkout').first.payment_method).to eq payment_method
expect(form.errors[:payment_method]).to include "must be a Cash or Stripe method"
expect(form.errors[:payment_method]).to include I18n.t("#{error_t_scope}.payment_method.invalid_type")
end
end
end
@@ -255,7 +257,7 @@ describe StandingOrderForm do
expect(payments.with_state('void').count).to be 0
expect(payments.with_state('checkout').count).to be 1
expect(payments.with_state('checkout').first.payment_method).to eq payment_method
expect(form.errors[:payment_method]).to include "is not available to #{standing_order.shop.name}"
expect(form.errors[:payment_method]).to include I18n.t("#{error_t_scope}.payment_method.not_available_to_shop", shop: standing_order.shop.name)
end
end
end