Sha256: 524b5547146d954202772b079eb645db077b71676e9ec535e72945de1a9c919a

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module Addressable
  extend ActiveSupport::Concern
  include FieldsValidator
  REQUIRED_DATABASE_FIELDS = [:title, :address1, :address2, :address3, :town, :county, :country, :postcode]

  included do
    validate_required_attributes
  end

  class_methods do
    def required_attributes
      result=defined?(super) ? super : []
      result+=required_addressable_attributes
    end

    def required_database_fields
      result=defined?(super) ? super : []
      result+= REQUIRED_DATABASE_FIELDS
    end

    def required_addressable_attributes
      [:address1,:postcode]
    end
  end

  public

  def address_array
    Array.new.tap { |a| REQUIRED_DATABASE_FIELDS.select { |key| a<<self.send(key) } }
  end

  def address_pretty
    stringify_array address_array
  end

  def city
    town
  end

  def city=(thing)
    self.town = thing
  end

  def state
    county
  end

  def state=(thing)
    self.county = thing
  end

  def zip
    postcode
  end

  def zip=(thing)
    self.postcode = thing
  end

  def zipcode
    postcode
  end

  def zipcode=(thing)
    self.postcode = thing
  end

  private

  def stringify_array(array)
    array.compact.join(", ")
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
morse_contactable-1.1.2 lib/addressable.rb