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