Module: MerchantSidekick::Addressable::ClassMethods

Defined in:
lib/merchant_sidekick/addressable/addressable.rb

Overview

Addressable adds address associations in the following way:

* supports multiple types of addresses, e.g. BusinessAddress
* associations for each type
* adds scopes, finders and other helpers

E.g.

class User < ActiveRecord::base
has_addresses :personal, :business, :billing
...
# => @user.personal_addresses
# => @user.find_personal_address
# => @user.find_or_build_personal_address
end

or

class User < ActiveRecord::base
has_address
...
# => @user.address
end


class User < ActiveRecord::base
has_address :mailing
...
# => @user.mailing_address
# => @user.find_mailing_address
# => @user.find_or_build_mailing_address
end

Instance Method Summary collapse

Instance Method Details

#has_address(*arguments) ⇒ Object

Defines a single address or a single address per address type



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
127
128
129
130
131
132
133
134
135
# File 'lib/merchant_sidekick/addressable/addressable.rb', line 44

def has_address(*arguments)
  attributes, options = [], {:has_one => true, :has_many => false}
  arguments.each do |argument|
    case argument.class.name
    when 'Hash'
      options = options.merge(argument)
    else
      attributes << argument
    end
  end

  if attributes.empty?
    has_one :address, :as => :addressable, :dependent => :destroy,
      :class_name => "MerchantSidekick::Addressable::Address"

    class_eval("      def build_address_with_addressable(attributes={}, options={})\n        build_address_without_addressable(attributes.merge(:addressable => self), options)\n      end\n      alias_method_chain :build_address, :addressable\n\n      def address_attributes=(attributes)\n        self.address ? self.address.attributes = attributes : self.build_address(attributes)\n      end\n    END\n  else\n    attributes.each do |attribute|\n      # Address decendent\n      # Note: the <attribute>.pluralize.classify makes sure that classify works\n      #       for singular attribute, e.g. :business -> BusinessAddress, otherwise,\n      #       Rails default would inflect :business -> BusinesAddress\n      address_class = <<-ADDRESS\n      class \#{attribute.to_s.pluralize.classify}Address < MerchantSidekick::Addressable::Address\n        def self.kind\n          '\#{attribute}'.to_sym\n        end\n\n        \#{ attributes.collect {|a| \"def self.\#{a}?; \#{a == attribute ? 'true' : 'false'}; end\" }.join(\"\\n\") }\n\n        def kind\n          '\#{attribute}'.to_sym\n        end\n\n        \#{ attributes.collect {|a| \"def \#{a}?; \#{a == attribute ? 'true' : 'false'}; end\" }.join(\"\\n\") }\n      end\n      ADDRESS\n      eval address_class, TOPLEVEL_BINDING\n\n      has_one \"\#{attribute}_address\".to_sym,\n      :class_name => \"\#{attribute.to_s.pluralize.classify}Address\",\n      :as => :addressable,\n      :dependent => :destroy\n\n      class_eval(<<-END, __FILE__, __LINE__+1)\n        def build_\#{attribute}_address_with_addressable(attributes={}, options={})\n          build_\#{attribute}_address_without_addressable(attributes.merge(:addressable => self), options)\n        end\n        alias_method_chain :build_\#{attribute}_address, :addressable\n\n        def find_\#{attribute}_address(options={})\n          find_address(:\#{attribute}, options)\n        end\n\n        def find_default_\#{attribute}_address\n          find_default_address(:\#{attribute})\n        end\n        alias_method :default_\#{attribute}_address, :find_default_\#{attribute}_address\n\n        def find_or_build_\#{attribute}_address(options={})\n          find_or_build_address(:\#{attribute}, options)\n        end\n\n        def find_\#{attribute}_address_or_clone_from(from_address, options={})\n          find_or_clone_address(:\#{attribute}, from_address, options)\n        end\n\n        def \#{attribute}_address_attributes=(attributes)\n          self.\#{attribute}_address ? self.\#{attribute}_address.attributes = attributes : self.build_\#{attribute}_address(attributes)\n        end\n      END\n    end\n  end\n\n  class_attribute :acts_as_addressable_options, :instance_writer => false\n  self.acts_as_addressable_options = {\n    :attributes       => attributes,\n    :association_type => options[:has_one] ? :has_one : :has_many\n  }\n\n  include MerchantSidekick::Addressable::InstanceMethods\n  extend MerchantSidekick::Addressable::SingletonMethods\nend\n", __FILE__, __LINE__+1)

#has_addresses(*arguments) ⇒ Object

Defines a single address or a single address per address type



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/merchant_sidekick/addressable/addressable.rb', line 138

def has_addresses(*arguments)
  attributes, options = [], {:has_one => false, :has_many => true}
  arguments.each do |argument|
    case argument.class.name
    when 'Hash'
      options = defaults.merge(argument)
    else
      attributes << argument
    end
  end

  has_many :addresses, :as => :addressable, :dependent => :destroy,
  :class_name => "MerchantSidekick::Addressable::Address"

  attributes.each do |attribute|
    address_class = "      class \#{attribute.to_s.pluralize.classify}Address < MerchantSidekick::Addressable::Address\n        def self.kind\n          '\#{attribute}'.to_sym\n        end\n\n        \#{ attributes.collect {|a| \"def self.\#{a}?; \#{a == attribute ? 'true' : 'false'}; end\" }.join(\"\\n\") }\n\n        def kind\n          '\#{attribute}'.to_sym\n        end\n\n        \#{ attributes.collect {|a| \"def \#{a}?; \#{a == attribute ? 'true' : 'false'}; end\" }.join(\"\\n\") }\n      end\n    ADDRESS\n    eval address_class, TOPLEVEL_BINDING\n\n    has_many \"\#{attribute.to_s.classify}Address\".pluralize.underscore.to_sym,\n    :class_name => \"\#{attribute.to_s.pluralize.classify}Address\",\n    :as => :addressable,\n    :dependent => :destroy\n\n    class_eval(<<-END, __FILE__, __LINE__+1)\n      def find_\#{attribute}_addresses(options={})\n        find_addresses(:all, :\#{attribute}, options)\n      end\n\n      def find_default_\#{attribute}_address\n        find_default_address(:\#{attribute})\n      end\n      alias_method :default_\#{attribute}_address, :find_default_\#{attribute}_address\n\n      def find_or_build_\#{attribute}_address(options={})\n        find_or_build_address(:\#{attribute}, options)\n      end\n\n      def find_\#{attribute}_address_or_clone_from(from_address, options={})\n        find_or_clone_address(:\#{attribute}, from_address, options)\n      end\n    END\n  end\n\n  class_attribute :acts_as_addressable_options, :instance_writer => false\n  self.acts_as_addressable_options = {\n    :attributes       => attributes,\n    :association_type => options[:has_one] ? :has_one : :has_many\n  }\n\n  include MerchantSidekick::Addressable::InstanceMethods\n  extend MerchantSidekick::Addressable::SingletonMethods\nend\n"