用户可配置全局属性“默认值"的 Rails 最佳实践 [英] Rails Best Practice for User-Configurable Global Attribute "Defaults"

查看:58
本文介绍了用户可配置全局属性“默认值"的 Rails 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉标题的措辞笨拙 - 不太确定标题的最佳方式,但这是我所看到的帮助:

Sorry about the awkward phrasing of the title -- not quite sure of the best way to title this but here's what I'm seeing assistance with:

在 Rails 应用中,假设我们有一个 Product 的模型,并且该产品的属性之一是 Price.

In a Rails app, let's say we've got a model for a Product, and one of the attributes of the product is Price.

在我的应用程序的管理端,我希望能够设置一个默认"价格,如果创建的任何新 Product 未分配 价格.如果 Product 确实 具有 Price 的值,那么它将被使用.

On the admin side of my app, I'd like to be able to set a "default" price that could be referred to if any new Product created isn't assigned a Price. If a Product does have a value for Price, then it would be used.

这当然是一个例子——我问这个问题是因为我必须想象这是一个常见的模式.这可以应用于可能具有用户可配置的全局默认值或特定于资源的值的任何资源.

This is, of course, an example -- and I ask this question because I've got to imagine this is a common pattern. This can be applied to any resource that might have user-configurable global defaults or resource-specific values.

在纯 Ruby 中,我认为可以使用类变量来解决这个问题,因此我可以在 Product 类中定义 @@default_price 并且如果实例化对象的值不存在,则能够引用 Product.default_price.

In pure Ruby, this would be solved, I think, with a class variable, so I'd be able to define @@default_price within the Product class and be able to refer to Product.default_price if the instantiated object's value doesn't exist.

我在这里的研究使我转向了 rails-settings-cached gem,它会允许类似 MyApp.default_price 的东西,但我想知道是否有一种更优雅的(非插件)方式来在基本 Rails 框架中实现这一点.

My research here has pointed me towards the rails-settings-cached gem, which would allow for something like MyApp.default_price, but I'm wondering if there's a more elegant (non-plugin) way to accomplish this within the base Rails framework.

注意我想在代码中设置这个结构,但我希望能够通过我的应用程序定义实际值(即配置文件不是我正在寻找的解决方案).

Note I'd like to setup this structure in code, but I want to be able to define the actual values through my app (i.e. config files aren't the solution I'm looking for).

有人能用 Rails 处理这个问题的方式启发我吗?

Can someone enlighten me with the Rails way of handling this?

推荐答案

ActiveRecord 从数据库架构中选取默认属性值.但是,这些都包含在迁移和表架构中,不可配置.

ActiveRecord picks up default attribute values from the database schema. However, these are baked into the migration and table schema and not configurable.

如果你想要可配置性,我使用的模式是 before_validation 回调方法,如果属性为空,则设置一个值,例如:

If you want configurability, the pattern that I've used is a before_validation callback method to set a value if the attribute is blank, e.g.:

class Product < ActiveRecord::Base

  before_validation :set_price_if_blank
  validates :price, :presence => true  # sanity check in case the default is missing

  has_one :price

  private

  def set_price_if_blank
    self.price = Price.default if self.price.blank?
  end      

end

class Price < ActiveRecord::Base

  def self.default
    @@default ||= Price.where(:default => true).first
  end

end

这假设您的价格表中填充了具有 default 标志的行.你可以实现这一点,例如通过 seeds.rb 文件.我添加了一个验证规则,以确保如果不存在默认值,您仍然会收到错误消息.它为您的应用程序增加了健壮性.

This assumes that your price table is populated with a row that has a default flag. You could achieve this, e.g. through a seeds.rb file. I've added a validation rule to make sure that you still get an error if no default exists. It adds robustness to your application.

另请注意,最好对价格数据使用整数或小数,而不是浮点数.请参阅此答案.

Also note that it's best to use Integers or Decimals for price data, not floats. See this answer.

这篇关于用户可配置全局属性“默认值"的 Rails 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆