使用自定义function包装许多rails属性
我有许多具有属性的模型,我需要在读取它们时应用一些新的行为。 我对Ruby / Rails相当陌生,所以现在我只是为一个属性定义一个getter并在其上面应用我的function(类似于亵渎过滤的东西),但是我将会这样做很多对象/属性,并希望更清洁。
例如,对于Post
对象的body
属性,这就是我现在完成的方法:
class Post < ActiveRecord::Base include Replaceable #... # A key point is that we want to keep the original content in the db def body profanity_filter(self[:body]) end end
……我的担忧看起来像这样:
module Replaceable extend ActiveSupport::Concern def profanity_filter(content) # filter and update content... content end end
这很有效,我很高兴,除了现在我必须将它应用到整个应用程序的许多领域,我想要比在各处覆盖吸气剂更优雅的东西。
我调查了代表,以便我可以做类似的事情
delegate :body, :title, :etc, :to => :profanity_filter
…但这不起作用,因为我无法传递需要过滤的内容。
任何帮助,将不胜感激!
这是实现自己的类宏的最佳时机。
我将重用你的Replaceable
模块来定义这个宏。
首先,让我们看看宏看起来像什么。
class Post < ActiveRecord::Base include Replaceable profanity_attrs :body, :foo, :bar, ... end
然后我们实现它
module Replaceable extend ActiveSupport::Concern def profanity_filter(content) # filter and update content... content end # This module will be `extend`ed by the model classes module Macro def profanity_attrs(attributes) # Note the implicit `self` here refer to the model class attributes.each do |attr| class_eval do define_method(attr) do # Note the `self` here refer to the model instance profanity_filter(self[attr]) end end end end end included do extend Macro end end
PS我真的不知道profanity
意味着什么,所以随时改变宏的名称:)