Module: AddressConcern::Address::Base::ClassMethods

Defined in:
lib/address_concern/address.rb

Overview

These (Base) class methods are added to ActiveRecord::Base so that they will be available from any model class. Unlike the main AddressConcern::Address methods which are only included after you call acts_as_address on a model.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#acts_as_address_configObject (readonly)

Returns the value of attribute acts_as_address_config.



19
20
21
# File 'lib/address_concern/address.rb', line 19

def acts_as_address_config
  @acts_as_address_config
end

Instance Method Details

#acts_as_address(**options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/address_concern/address.rb', line 20

def acts_as_address(**options)
  # Have to use yield_self(&not_null) intead of presence because NullColumn.present? => true.
  not_null = ->(column) {
    column.type.nil? ? nil : column
  }
  options = options.deep_symbolize_keys
  default_config = {
    country: {
      #normalize: false,
      #validate: false,

      # By default, code (same as alpha_2_code) will be used
      carmen_code: :code, # or alpha_2_code, alpha_3_code, :numeric_code

      code_attribute: column_for_attribute(:country_code).yield_self(&not_null)&.name ||
                     (column_for_attribute(:country).yield_self(&not_null)&.name unless options.dig(:country, :name_attribute).to_s == 'country'),

      name_attribute: column_for_attribute(:country_name).yield_self(&not_null)&.name ||
                     (column_for_attribute(:country).yield_self(&not_null)&.name unless options.dig(:country, :code_attribute).to_s == 'country'),

      on_unknown: ->(record, name_or_code, value) { },
    },

    state: {
      #normalize: false,
      #validate: false,

      code_attribute: column_for_attribute(:state_code).yield_self(&not_null)&.name ||
                     (column_for_attribute(:state).yield_self(&not_null)&.name unless options.dig(:state, :name_attribute).to_s == 'state'),

      name_attribute: column_for_attribute(:state_name).yield_self(&not_null)&.name ||
                     (column_for_attribute(:state).yield_self(&not_null)&.name unless options.dig(:state, :code_attribute).to_s == 'state'),

      on_unknown: ->(record, name_or_code, carmen_country, value) { },
      debug_unknown: false
    },

    street_address: {
      #normalize: false,
      #validate: false,

      # Try to auto-detect address columns
      attributes: column_names.grep(/^address$|^address_\d$/),
    }
  }
  @acts_as_address_config = config = {
    **default_config
  }.deep_merge(options)

  [:state, :country].each do |group|
    # Can't use the same column for code and name, so if it would be the same (by default or
    # otherwise), let it be used for name only instead.
    if config[group][:code_attribute] == config[group][:name_attribute]
      config[group].delete(:code_attribute)
    end
  end

  include ::AddressConcern::Address
end

#belongs_to_addressable(**options) ⇒ Object



80
81
82
# File 'lib/address_concern/address.rb', line 80

def belongs_to_addressable(**options)
  belongs_to :addressable, polymorphic: true, touch: true, optional: true, **options
end