[英]Extending the Spree::Product model/class
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.我在我的 Rails 3.2 应用程序中使用 Spree,我想扩展 Spree 的 Product 类以更好地满足我的需求,例如与我的应用程序中的另一个模型建立关系。 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?如果我想向 Product 资源添加新的属性/字段怎么办? I can't find it's migration either :/
我也找不到它的迁移:/
Thanks in advance :)提前致谢 :)
The best thing to do here is to create a product_decorator.rb
in your app.最好的做法是在您的应用程序中创建一个
product_decorator.rb
。
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. 这是文档。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.