Class: MagicAddresses::Address

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

Constant Summary collapse

PARAMS =
> C O N S T A N T S <=============================================================== #
[ :id, :street, :street_additional, :number, :postalcode, :city, :country, :country_code, :owner, :_destroy ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_one(owner, params) ⇒ Object

> C L A S S - M E T H O D S <======================================================= #


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/app/models/magic_addresses/address.rb', line 96

def self.get_one( owner, params )
  # => that = self.new( city: params[:city], postalcode: params[:postalcode], street: params[:street], number:  params[:number], country: params[:country] )
  # => that.owner = owner
  # => that.save
  # => that.trigger_build_address_associations
  # puts "> get_one( #{ owner.class },  #{params}  )"
  if params[:id].present? && self.find( params[:id].to_i )
    current = self.find( params[:id].to_i )
    # if params[:postalcode].present? || params[:postalcode].present? || params[:postalcode].present? || params[:postalcode].present?
    checker = []
    [:street, :number, :postalcode, :city, :country].each do |prm|
      if params[prm].present? && ( "#{ params[prm] }" != "#{ current.send("#{prm}") }" )
        checker << true
        puts "#{prm}:  #{ params[prm] }  !=  #{ current.send("#{prm}") }"
      end
    end
    if checker.include?( true )
      that = self.new( city: params[:city], postalcode: params[:postalcode], street: params[:street], number:  params[:number], country: params[:country] )
      that.trigger_build_address_associations
    else
      current
    end
  else
    if params[:postalcode].present? && params[:city].present?
      that = self.new( city: params[:city], postalcode: params[:postalcode], street: params[:street], number:  params[:number], country: params[:country] )
      that.trigger_build_address_associations
    else
      nil
    end
  end
end

Instance Method Details

#country_codeObject

attr_accessor :country_code



66
67
68
# File 'lib/app/models/magic_addresses/address.rb', line 66

def country_code
  fetch_address && fetch_address["fetch_country_code"] || self.send("magic_country") && self.send("magic_country").iso_code
end

#display(*args) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/app/models/magic_addresses/address.rb', line 152

def display( *args )
  options = args.extract_options!
  ## style =>  'inline' | 'full'
  style = args.first || options[:style] if args.first.present? || options[:style].present?
  show_state        = options[:state].present? ? options[:state] : false
  show_country      = options[:country].present? ? options[:country] : false
  show_district     = options[:district].present? ? options[:district] : false
  show_subdistrict  = options[:subdistrict].present? ? options[:subdistrict] : false
  adr = []
  adr << "#{street} #{number}".strip if street.present?
  adr << "#{postalcode} #{city}".strip if zipcode.present? || city.present?
  adr << "#{district.present? && show_district ? district : ''} #{subdistrict.present? && show_subdistrict ? "(#{subdistrict})" : ''}".strip if show_district || show_subdistrict
  adr << state     if state.present? && show_state
  adr << country   if country.present? && show_country
  if adr.count == 0
    I18n.t("addresses.no_address_given")
  else
    type == "full" ? adr.join("<br />") : adr.join(", ")
  end
end

#keyObject

add associations (set in configuration)



17
18
19
# File 'lib/app/models/magic_addresses/address.rb', line 17

MagicAddresses.configuration.address_owners.each do |key,type|
  has_many key.to_sym, through: :addressibles, source: :owner, source_type: type
end

#numberObject

attr_accessor :number (:street_number)



58
59
60
# File 'lib/app/models/magic_addresses/address.rb', line 58

def number
  fetch_address && fetch_address["fetch_number"] || street_number
end

#owner=(this) ⇒ Object



177
178
179
# File 'lib/app/models/magic_addresses/address.rb', line 177

def owner=(this)
  self.addressibles.where( owner_type: this.class.to_s, owner_id: this.id ).first_or_create!
end

#ownersObject



173
174
175
# File 'lib/app/models/magic_addresses/address.rb', line 173

def owners
  addressibles.map { |that| ::MagicAddresses::OwnerProxy.new( that ) }
end

#postalcodeObject

attr_accessor :postalcode (:zipcode)



62
63
64
# File 'lib/app/models/magic_addresses/address.rb', line 62

def postalcode
  fetch_address && fetch_address["fetch_zipcode"] || zipcode
end

#presentation(style = "full", type = "inline") ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/app/models/magic_addresses/address.rb', line 136

def presentation( style = "full", type = "inline" )
  adr = []
  adr << "#{street} #{number}".strip if street.present?
  adr << "#{postalcode} #{city}" if zipcode.present? || city.present?
  adr << country if country.present? if style == "full"
  if adr.count == 0
    I18n.t("addresses.no_address_given")
  else
    if type == "full"
      adr.join("<br />")
    else
      adr.join(", ")
    end
  end
end

#streetObject

attr_accessor :street



54
55
56
# File 'lib/app/models/magic_addresses/address.rb', line 54

def street
  fetch_address && fetch_address["fetch_street"] || street_name
end

#touch_ownersObject



181
182
183
# File 'lib/app/models/magic_addresses/address.rb', line 181

def touch_owners
  addressibles.map { |that| that.owner.touch }
end

#trigger_build_address_associationsObject



185
186
187
# File 'lib/app/models/magic_addresses/address.rb', line 185

def trigger_build_address_associations
  build_address_associations()
end

#trigger_complete_translated_attributesObject



189
190
191
# File 'lib/app/models/magic_addresses/address.rb', line 189

def trigger_complete_translated_attributes
  complete_translated_attributes()
end