Add before_validation callback to set unused addressed fields in customer, order, user, subscription, enterprise_group

This commit is contained in:
Nihal
2021-09-20 14:37:03 +05:30
committed by Nihal M. Kelanthodika
parent 372326debc
commit 5f1a326a05
5 changed files with 27 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ class Customer < ApplicationRecord
before_validation :downcase_email
before_validation :empty_code
before_validation :set_unused_address_fields
validates :code, uniqueness: { scope: :enterprise_id, allow_nil: true }
validates :email, presence: true, 'valid_email_2/email': true,
@@ -52,4 +53,9 @@ class Customer < ApplicationRecord
errors.add(:base, I18n.t('admin.customers.destroy.has_associated_orders'))
throw :abort
end
def set_unused_address_fields
ship_address.company = 'Company' if ship_address.present?
bill_address.company = 'Company' if bill_address.present?
end
end

View File

@@ -54,7 +54,7 @@ class EnterpriseGroup < ApplicationRecord
}
def set_unused_address_fields
address.firstname = address.lastname = I18n.t(:unused)
address.firstname = address.lastname = address.company = I18n.t(:unused)
end
def set_undefined_address_fields

View File

@@ -86,6 +86,7 @@ module Spree
before_validation :clone_billing_address, if: :use_billing?
before_validation :associate_customer, unless: :customer_id?
before_validation :ensure_customer, unless: :customer_is_valid?
before_validation :set_unused_address_fields
before_create :link_by_email
after_create :create_tax_charge!
@@ -611,6 +612,11 @@ module Spree
private
def set_unused_address_fields
ship_address.company = 'Company' if ship_address.present?
bill_address.company = 'Company' if bill_address.present?
end
def fee_handler
@fee_handler ||= OrderFeesHandler.new(self)
end

View File

@@ -20,7 +20,8 @@ module Spree
before_validation :set_login
before_destroy :check_completed_orders
before_validation :set_unused_address_fields
roles_table_name = Role.table_name
scope :admin, lambda { includes(:spree_roles).where("#{roles_table_name}.name" => "admin") }
@@ -150,6 +151,11 @@ module Spree
private
def set_unused_address_fields
ship_address.company = 'Company' if ship_address.present?
bill_address.company = 'Company' if bill_address.present?
end
def check_completed_orders
raise DestroyWithOrdersError if orders.complete.present?
end

View File

@@ -27,6 +27,8 @@ class Subscription < ApplicationRecord
accepts_nested_attributes_for :subscription_line_items, allow_destroy: true
accepts_nested_attributes_for :bill_address, :ship_address
before_validation :set_unused_address_fields
scope :not_ended, -> {
where('subscriptions.ends_at > (?) OR subscriptions.ends_at IS NULL', Time.zone.now)
}
@@ -75,6 +77,11 @@ class Subscription < ApplicationRecord
private
def set_unused_address_fields
ship_address.company = 'Company' if ship_address.present?
bill_address.company = 'Company' if bill_address.present?
end
def pending?
return true unless begins_at