问题描述
|
我有四个模型,可以通过四个单独的注释控制器进行注释。这四个注释控制器实际上执行相同的操作,并且仅稍有不同。
为了消除基本上相同的四个注释控制器的重复,我创建了一个Rails Engine作为gem,以随意处理我在route.rb中指定的任何模型上的注释。
因此,现在可以在我的routes.rb文件中使用:
comments_on :articles,:by => :users
在我的gem中实现了comments_on,如下所示:
def comments_on(*resources)
options = resources.extract_options!
[snip of some validation code]
topic_model = resources.first.to_s
user_model = options[:by].to_s
# Insert a nested route
Rails.application.routes.draw do
resources topic_model do
resources \"comments\"
end
end
end
路由显示在\“ rake route \”中,并且请求正确路由到我的gem \的\“ CommentsController \”,但这就是我gem \功能终止的地方。
在我的gem CommentController中检测上下文的最佳方法是什么,这样我就可以处理特定于如何调用comment_on的请求?
更具体地说,在实现上下文感知的情况下,我将如何实现以下索引操作?
def index
@article = Article.find(params[:article_id])
@comments = ArticleComment.find(:all,:conditions => { :article_id => @article.id })
end
谢谢您的帮助!
解决方法
您可以将主题指定为路线中的附加参数:
Rails.application.routes.draw do
resources topic_model do
resources \"comments\",:topic_model => topic_model.to_s
end
end
然后,您的控制器可以这样写:
def index
@topic = topic
@comments = topic.comments
end
protected
def topic
m = params[:topic_model]
Kernel.const_get(m).find(params[\"#{m.underscore}_id\"])
end
您也可以将许多逻辑移出控制器,也移入模型。 “ 5”可以是所有这些模型都应实现的命名范围。
我过去做过类似的模式,通常会有一个极端的案例将这个想法分解,最终您会进行比明智的“元”编程更多的事情。
我建议创建一个基本控制器,然后创建一个从中继承的简单控制器,或者尝试将这些常见行为拆分为模块。