Redmine:如何覆盖模型方法visible_condition

时间:2015-02-19 09:31:02

标签: ruby-on-rails ruby

我想用我的插件覆盖模型方法。我尝试使用方法创建一个新模型Patch,如下所示:

require_dependency 'issue'

module IssuePatch
   def self.included(base) # :nodoc:
        base.send(:extend, ClassMethods)
        base.send(:include, InstanceMethods)
        base.class_eval do
            alias_method :visible_condition, :visible_condition_with_patch
        end
    end

    module ClassMethods

    end

    module InstanceMethods
        def visible_condition_with_patch(user, options={})

            <Do Something>
        end
    end
end


Rails.configuration.to_prepare do
    unless Issue.included_modules.include? IssuePatch
        # Issue.extend(InstanceMethods)
        Issue.send(:include, IssuePatch)
    end
end

1 个答案:

答案 0 :(得分:0)

试试这个。

require_dependency 'issue'

module IssuePatch
  def self.included(base)
    base.class_eval do
      def visible_condition(user, options={})
        # do something
      end
    end
  end
end

Issue.send(:include, IssuePatch) unless Issue.included_modules.include?(IssuePatch)