模型或助手中模型属性的动态翻译?
假设我有一个 Partner
模型,它代表广告网络中的联属合作伙伴。每个合作伙伴都有一个佣金,佣金可以是固定的(绝对货币)或可变的(百分比)。 partners
表使用 commission_type
、commission_fixed
和 commission_variable
列对此进行建模。这是一个简化,实际上有7种委托类型,因此单表继承或多模型将需要大量额外代码。
在我看来,我想显示佣金,像这样:(
<div class="partner_commission"><%= partner.commission %></div>
使用模型方法)
或
<div class="partner_commission"><%= commission(partner) %></div>
(使用辅助方法)
现在,需要注意的一个重要方面是整个应用程序是多语言的,因此视图中的每个字符串都必须翻译使用 I18n.translate
/ I18n.t
帮助器。翻译不仅仅包含数字/货币格式,因此需要对佣金字符串进行自定义翻译(不管为什么)。
该方法如下所示(简化模型版本):
def commission
case self.commission_type
when 'commission_fixed'
t(:commission_fixed, :value => self.commission_fixed)
when 'commission_variable'
t(:commission_variable, :value => self.commission_variable)
end
end
问题是:我应该在哪里实现 commission
方法才能符合 MVC 理念、最佳实践等?
将其放在 helper 中的原因是它只涉及视图中佣金数据的呈现,而不涉及数据本身。另一方面,必须实现一个仅涉及单个模型但在多个视图中使用的辅助方法,这感觉是错误的。
将其放入模型的原因是佣金字符串可以被视为派生属性,因此应该在模型中定义。另一方面,我必须手动包含 i18n 视图助手才能在模型中使用 I18n.translate
,这感觉很奇怪。
你怎么认为?
Let's say I have a Partner
model which represents an affiliate partner in an ad network. Each partner is associated with a commission which can be fixed (absolute currency) or variable (percentage). The partners
table models this with columns commission_type
, commission_fixed
and commission_variable
. This is a simplification, there are actually 7 commission types, so single-table inheritance or multiple models would require lots of extra code.
In my view, I want to display the commission, like this:
<div class="partner_commission"><%= partner.commission %></div>
(using model method)
or
<div class="partner_commission"><%= commission(partner) %></div>
(using helper method)
Now, one important aspect to note is that the entire application is multilingual, so every string in the view has to be translated with the I18n.translate
/ I18n.t
helper. The translation contains more than just number / currency format, so custom translations for the commission string are needed (never mind why).
The method looks like this (simplified model version):
def commission
case self.commission_type
when 'commission_fixed'
t(:commission_fixed, :value => self.commission_fixed)
when 'commission_variable'
t(:commission_variable, :value => self.commission_variable)
end
end
The question is: Where should I implement the commission
method to be in compliance with MVC philosophy, best practices, etc.?
The reason for putting it in the helper would be that it only concerns the presentation of the commission data in the view, not the data itself. On the other hand, a helper method has to be implemented which only concerns a single model, but is used across many views, which feels wrong.
The reason for putting it in the model would be that the commission string can be seen as a derived attribute and hence should be defined in the model. On the other hand, I have to manually include the i18n view helpers to use I18n.translate
in the model, which feels strange.
What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说它应该放在模型中。模型应该控制您的数据,而这正是您希望佣金方法执行的操作。在 MVC 中,使用模型方法来组合/修改数据以便以良好的形式返回数据是完全可以的。
I would say it should go in the model. Models are supposed to control your data, and that is exactly what you want your commission method to be doing. It is perfectly OK in MVC to use model methods to combine/modify data in order to return it in a nice form.