Ruby 重写Rails 3中的渲染-设计审查/代码重构请求

Ruby 重写Rails 3中的渲染-设计审查/代码重构请求,ruby,ruby-on-rails-3,controller,render,Ruby,Ruby On Rails 3,Controller,Render,我正在寻找一种可扩展的方法,将功能注入控制器 module Social module Request def self.included( base ) base.alias_method_chain :render, :social base.alias_method_chain :process_action, :social end def process_action_with_s

我正在寻找一种可扩展的方法,将功能注入控制器

module Social
    module Request
        def self.included( base )
            base.alias_method_chain :render, :social
            base.alias_method_chain :process_action, :social
        end

        def process_action_with_social(method_name, *args)
            @action = method_name
            process_action_without_social(method_name, *args)
        end

        def render_with_social(action = nil, options = {}, &blk)
            if @action == "new"
                          # do something social
            end
            render_without_social
        end
    end
end

class RequestController < ApplicationController
    include Social::Request

    def new     
        @request = RecommendationRequest.new
    end
end
社交模块
模块请求
def自带(基本)
base.alias\u方法\u链:渲染,:社交
base.alias\u方法\u链:过程\u操作,:社会
终止
def process_action_与_social(方法名称,*args)
@action=方法名称
不带社交的过程动作(方法名称,*args)
终止
def render_with_social(action=nil,options={},&blk)
如果@action==“新建”
#做些社交活动
终止
在没有社交的情况下呈现
终止
终止
终止
类RequestController
上面的代码正在运行,但我很确定它不是最佳的ruby。我将为每个控制器创建不同的模块,实际上可能是一堆。每个控制器唯一的代码是render_with_social

因此,我的问题是:

  • 有没有办法把另外两种方法去掉,这样我就不能重复了?(对不起,这是我第一次在ruby中使用模块)
  • 有没有一种方法可以做到这一点,而不必将include Social::Request放在RequestController中?(我希望能够移除这些模块,并让我的控制器继续工作,就好像什么都没发生一样)
  • 如果我最终得到了这些指标的不同套件::Request、Scoring::Request等,那么在一个位置指定哪些模块将混合到哪些控制器中,最好的方法是什么
  • 谢谢大家