From f72f1823637f72be50e346a82c50012269bc07d5 Mon Sep 17 00:00:00 2001 From: Nihal Date: Wed, 22 Sep 2021 13:30:28 +0530 Subject: [PATCH] Redo business_address_empty? method --- app/models/enterprise.rb | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index c7fb01f8bd..1d2af37a91 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -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