我想扩展类角色,以便可以在 Spree 中的角色表中添加更多角色。我的应用程序会根据角色有不同的价格。
默认情况下,角色有:(“admin”和“user”)。我想在表格中添加更多类型。
Q1:我可以在我的一个扩展中扩展 Role 类吗?Q2:如何实现(实际上是在 app/models/Variant.rb 上扩展)基于不同角色的价格,以便从一个地方获取价格?这样我就不必更改使用价格的 *_html.erb 文件中的代码。
如果我能让它工作,这将是 github 上的一个很酷的扩展。
谢谢
我想扩展类角色,以便可以在 Spree 中的角色表中添加更多角色。我的应用程序会根据角色有不同的价格。
默认情况下,角色有:(“admin”和“user”)。我想在表格中添加更多类型。
Q1:我可以在我的一个扩展中扩展 Role 类吗?Q2:如何实现(实际上是在 app/models/Variant.rb 上扩展)基于不同角色的价格,以便从一个地方获取价格?这样我就不必更改使用价格的 *_html.erb 文件中的代码。
如果我能让它工作,这将是 github 上的一个很酷的扩展。
谢谢
To extend classes in Spree, you can use Modules or class_eval. Spree extensions tend to use class_eval. Here's an example for extending User and Variant in a custom extension.
class CustomRoleExtension < Spree::Extension
# main extension method
def activate
# extend User
User.class_eval do
def business?
self.roles.include?("business")
end
def sponsor?
self.roles.include?("sponsor")
end
def developer?
self.roles.include?("developer")
end
end
# extend Variant
Variant.class_eval do
def price_for(role)
# ...
end
end
end
end
To add more roles, I just added a defaults/roles.yml to my extension, with custom yaml blocks:
coach_role:
id: 3
name: coach
trainer_role:
id: 4
name: trainer
graduate_role:
id: 5
name: graduate
Then when you run rake db:bootstrap, it will add all those roles to the database.
Let me know if that works.