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
因此,我的问题是: