Redo business_address_empty? method

This commit is contained in:
Nihal
2021-09-22 13:30:28 +05:30
committed by Nihal M. Kelanthodika
parent 912b869407
commit f72f182363

View File

@@ -59,7 +59,7 @@ class Enterprise < ApplicationRecord
delegate :latitude, :longitude, :city, :state_name, to: :address
accepts_nested_attributes_for :address
accepts_nested_attributes_for :business_address, :reject_if => :business_address_empty?, allow_destroy: true
accepts_nested_attributes_for :business_address, reject_if: :business_address_empty?, allow_destroy: true
accepts_nested_attributes_for :producer_properties, allow_destroy: true,
reject_if: lambda { |pp|
pp[:property_name].blank?
@@ -212,20 +212,10 @@ class Enterprise < ApplicationRecord
}
def business_address_empty?(attributes)
is_empty_attributes =
attributes[:company].blank? &&
attributes[:address1].blank? &&
attributes[:city].blank? &&
attributes[:phone].blank? &&
attributes[:zipcode].blank?
if is_empty_attributes
if attributes[:id].present?
attributes.merge!({:_destroy => 1}) && false
else
true
end
end
attributes_exists = attributes['id'].present?
attributes_empty = attributes.slice(:company, :address1, :city, :phone, :zipcode).values.all?(&:blank?)
attributes.merge!(_destroy: 1) if attributes_exists and attributes_empty
!attributes_exists && attributes_empty
end
# Force a distinct count to work around relation count issue https://github.com/rails/rails/issues/5554