module MagicAddresses::Association::ClassMethods

C L A S S - M E T H O D S # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Public Instance Methods

has_addresses() click to toggle source
# File lib/app/models/magic_addresses/association.rb, line 13
def has_addresses
  # has_many    :addresses,
  #             as: :owner,
  #             class_name: "MagicAddresses::Address",
  #             dependent: :destroy
  
  has_many    :addressibles,  
              as: :owner, 
              class_name: "MagicAddresses::Addressible", 
              dependent: :destroy
  
  has_many    :addresses, 
              through: :addressibles, 
              source: :address
  
  accepts_nested_attributes_for :addresses, allow_destroy: true, reject_if: :all_blank
end
has_nested_address() click to toggle source
# File lib/app/models/magic_addresses/association.rb, line 76
def has_nested_address
  send :include, NestedInstanceMethods
  # has_one     :address,
  #             as: :owner,
  #             class_name: "MagicAddresses::Address",
  #             autosave: true,
  #             dependent: :destroy
  
  has_one     :addressible, -> { where(default: true) }, 
              as: :owner, class_name: "MagicAddresses::Addressible", dependent: :destroy
  has_one     :address,     through: :addressibles, source: :address
  
  delegate    :street, :number, :postalcode, :city, :district, :subdistrict, :state, :country,
              :street=, :number=, :postalcode=, :city=, :country=, 
              to: :address, allow_nil: true
  accepts_nested_attributes_for :address, allow_destroy: true, reject_if: :all_blank
  # alias_method_chain :address, :build
end
has_one_address() click to toggle source
# File lib/app/models/magic_addresses/association.rb, line 32
def has_one_address
  send :include, OneInstanceMethods
  # has_one     :address, -> { where(default: true) },
  #             as: :owner,
  #             class_name: "MagicAddresses::Address",
  #             autosave: true,
  #             dependent: :destroy
  
  has_one     :addressible, 
              -> { where(default: true, named_address: ["", nil]) }, 
              as: :owner, 
              class_name: "MagicAddresses::Addressible", 
              dependent: :destroy
  
  has_one     :address, 
              through: :addressible, 
              source: :address
  
  # accepts_nested_attributes_for :addressible, :address, allow_destroy: true, reject_if: :all_blank
end
has_one_named_address( name = "address" ) click to toggle source
# File lib/app/models/magic_addresses/association.rb, line 53
def has_one_named_address( name = "address" )
  
  has_one     "#{name}_addressible".to_sym, 
              -> { where(default: true, named_address: name) }, 
              as: :owner, 
              class_name: "MagicAddresses::Addressible", 
              dependent: :destroy
  
  has_one     "#{name}".to_sym, 
              through: "#{name}_addressible".to_sym, 
              source: :address
  
  define_method "#{name}_attributes=" do |params|
    self.send( "#{name}=", MagicAddresses::Address.get_one( self, params ) )
  end
  
  define_method "#{name}_addressible_attributes=" do |params|
    self.send( "#{name}=", MagicAddresses::Address.get_one( self, params["#{name}_attributes".to_sym] ) )
  end
  
end