29 May 2013

subclassing RefineryCMS user

In the refinerycms-stores engine I am building, I wanted to have a Customer class subclassed from User:
class Customer < ::Refinery::User
      has_many  :addresses, :class_name => ::Refinery::Addresses::Address 
      has_many  :orders, :class_name => ::Refinery::Orders::Order, :foreign_key => :order_customer_id 
    
      has_one   :billing_address, :class_name => ::Refinery::Addresses::Address,
         :conditions => { :is_billing => true, :order_id => nil }

      has_one   :shipping_address, :class_name => ::Refinery::Addresses::Address,
         :conditions => { :is_billing => false, :order_id => nil }
but there was a problem when I used current_refinery_user because the object was a User, and if I referenced current_refiner_user.billing_address, I would get a method_missing exception. Below is the solution I used to extend the added associations into the ::Refinery::User class.
Add to stores/lib/refinery/stores/engine.rb, the following section within the initializer block, and then everything works good.
      config.to_prepare do
        ::Refinery::User.class_eval do
      has_many  :addresses, :class_name => ::Refinery::Addresses::Address, :foreign_key => :customer_id 
      has_many  :orders, :class_name => ::Refinery::Orders::Order, :foreign_key => :order_customer_id
      has_one   :billing_address, :class_name => ::Refinery::Addresses::Address,
         :foreign_key => :customer_id,
         :conditions => { :is_billing => true, :order_id => nil 
      has_one   :shipping_address, :class_name => ::Refinery::Addresses::Address,
         :foreign_key => :customer_id,
         :conditions => { :is_billing => false, :order_id => nil }
        end  # extend user for customers
      end  # to prepare
 

No comments:

Post a Comment