Class: Address

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/polymorphic/address.rb

Overview

Copyright © 2008-2013 Michael Dvorkin and contributors.

Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or www.opensource.org/licenses/mit-license.php


Schema Information

Table name: addresses

id               :integer         not null, primary key
street1          :string(255)
street2          :string(255)
city             :string(64)
state            :string(64)
zipcode          :string(16)
country          :string(64)
full_address     :string(255)
address_type     :string(16)
addressable_id   :integer
addressable_type :string(255)
created_at       :datetime
updated_at       :datetime
deleted_at       :datetime

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reject_address(attributes) ⇒ Object


Ensure blank address records don’t get created. If we have a new record and

address is empty then return true otherwise return false so that _destroy
is processed (if applicable) and the record is removed.

Intended to be called as follows:

accepts_nested_attributes_for :business_address, :allow_destroy => true, :reject_if => proc {|attributes| Address.reject_address(attributes)}


53
54
55
56
57
58
# File 'app/models/polymorphic/address.rb', line 53

def self.reject_address(attributes)
  exists = attributes['id'].present?
  empty = %w[street1 street2 city state zipcode country full_address].map { |name| attributes[name].blank? }.all?
  attributes[:_destroy] = 1 if exists && empty
  (!exists && empty)
end

Instance Method Details

#blank?Boolean

Checks if the address is blank for both single and compound addresses.


Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'app/models/polymorphic/address.rb', line 39

def blank?
  if Setting.compound_address
    %w[street1 street2 city state zipcode country].all? { |attr| send(attr).blank? }
  else
    full_address.blank?
  end
end