Question

I have three applications: Website, POS and Manager. The Manager app has the Manager Engine mounted in root. The Manager engine is where all updates to the core models (Events, Wines, etc.) are made. The other two apps mount the engine as well, but they act as "slave engines". i.e., they synchronize to the master manager app periodically, but aren't allowed to change the data in the manager_ tables otherwise.

I'd like to know what the best way to 'merge' the necessary attributes in the Manager::Wine object to the POS's Item object.

Item contains category_id, barcode, name, price, sale_price, stock, force_price_negative, ...etc. Some of these attributes overlap with the Manager::Wine such as barcode, name, price, sale_price, stock. I want the Item object to inherit these properties from the manager_wines table, but only when the category_id is 1 (Wines). An item can belong to any other category, so it's not like all items will be using the properties in manager_wines.

Off the top of my head, the most 'generic' way of doing it is having two additional attributes in Item (model, model_id...where model is Manager::Wine and the id is the corresponding id in manager_wines). I just don't know if there's a simpler or more robust way of doing this.

Was it helpful?

Solution

Whelp, I drudged through a solution.

In Item model:

belongs_to :manager, :polymorphic => true

# Override local methods with Manager object methods
[:barcode, :name, :price, :sale_price, :stock, :taxed, :tax_included].each do |att|
    define_method(att) do
        if manager_id.blank?
            read_attribute(att)
        else
            manager.send(att)
        end
    end
end

In items table, added:

manager_id :integer  
manager_type :string

And it seems to be working OK. One thing to note though, is that eager loading the polymorphic association fails if I specifically try to use order('categories.name') for some reason - even though the categories table doesn't have anything to do with the engine model. Otherwise it's good.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top