Extending the Spree::Product model/class

2020-07-16 08:56发布

问题:

I'm using Spree in a Rails 3.2 app of mine and I want to extend Spree's Product class to better suit my needs as for example to establish a relationship with another model in my app. What's the best way to do this? I could not find anything about it in the project documentation

And what if I want to add new attributes/fields to the Product resource? I can't find it's migration either :/

Thanks in advance :)

回答1:

The best thing to do here is to create a product_decorator.rb in your app.

This will look like the following:

Spree::Product.class_eval do

end

In there, you can feel free to modify whatever you want!

Here's the Documentation for that:

https://guides.spreecommerce.com/developer/logic.html

To add a new field to an already existing Model, run a migration like this:

migration

class AddSubscribableFieldToVariants < ActiveRecord::Migration
  def change
    add_column :spree_variants, :subscribable, :boolean, default: false
  end
end

And then in the model add following:

spree/variants_decorator.rb

Spree::Variant.class_eval do
  attr_accessible :subscribable
end