Module: AddresslogicRails
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/addresslogic_rails.rb,
lib/addresslogic_rails/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Method Summary collapse
-
#address_parts(*args) ⇒ Object
Returns the parts of an address in an array.
Instance Method Details
#address_parts(*args) ⇒ Object
Returns the parts of an address in an array. Example:
["Street1", "Street2", "City", "State Zip", "Country"]
This makes displaying addresses on your view pretty simple:
address.address_parts.join("<br />")
Options
-
only:
fields you want included in the result -
except:
any fields you want excluded from the result
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/addresslogic_rails.rb', line 39 def address_parts(*args) = args.last.is_a?(Hash) ? args.pop : {} [:only] = [[:only]] if [:only] && ![:only].is_a?(Array) [:except] = [[:except]] if [:except] && ![:except].is_a?(Array) fields = args[0] || self.class.[:fields] level = args[1] || 0 parts = [] fields.each do |field| if field.is_a?(Array) has_sub_array = field.find { |item| item.is_a?(Array) } separator = has_sub_array ? ", " : " " sub_parts = address_parts(field, level + 1, ).join(separator) next if sub_parts.empty? parts << sub_parts else next if !respond_to?(field) value = send(field) next if value.to_s.strip == "" || ([:only] && ![:only].include?(field)) || ([:except] && [:except].include?(field)) parts << value end end parts end |