2011-07-12 127 views
4

我使用on Rails的Ruby的3.0.7,我想变干(不要重复自己)我的范围的方法。DRY范围方法

在模型文件我有:

class Articles::Category < ActiveRecord::Base 
    scope :article_related_to, lambda { |user| where('articles_categories_article_relationships.user_id = ?', user.id) } 
    scope :comment_related_to, lambda { |user| where('comments_articles_article_category_relationships.user_id = ?', user.id) } 


    has_many :comment_article_category_relationships 
    has_many :comments, 
    :class_name => 'Comments::Articles::ArticleCategoryRelationship', 
    :through  => :comment_article_category_relationships, 
    :source  => :comment 

    has_many :article_relationships 
    :class_name => 'Articles::Categories::ArticleRelationship', 
    has_many :articles, 
    :through  => :article_relationships, 
    :source  => :article 
end 

通过使用上面的代码,我可以这样做:

@comment.article_categories.comment_related_to(@current_user) 
@comment.article_categories.article_related_to(@current_user) 

我如何“干”作用域,以使可能的方法均使用:article_related_to:comment_related_to以使用类似以下的内容

@comment.article_categories.related_to(@current_user) 

# In order to pass the correct "context" 'article' or 'comment' I thought 
# something like 
# 
# @comment.article_categories.related_to(@current_user, 'article') 
# @comment.article_categories.related_to(@current_user, 'comment') 
# 
# but, maybe, there is a way to retrieve automatically that "context" so to 
# write only one "DRYed" scope method. 

+1

吆,我的眼睛都在流血只是试图让这件事直!无论如何,你可以通过@current_user对象获得你正在寻找的东西吗? – dogenpunk

+0

@dogenpunk - 顺便说一句:“我的眼睛在流血只是试图让这件事直” ......你什么意思? - 在我的情况下,我能得到什么,我在寻找通过“@current_user”对象,但是,在这另一面,如问题描述我有同样的问题。 – Backo

+1

我认为这是一个干太远个人:可读性为我拍干,这使得代码更难阅读。 DRY应该是关于保持功能或值等在一个地方,而不是减少的代码行。 –

回答

1

我能提供的最好的是以下几点:

scope :related_to, lambda { |user, context| 
    tbl = context == :article ? :articles_categories_article_relationships 
          : :comments_articles_article_category_relationships 
    where("#{tbl}.user_id = ?", user.id) 
} 

,将给你@comment.article_categories.related_to(@current_user, :article)喜欢你的建议。但我同意Max Williams的意见。这会不必要地混淆您的代码而没有真正的收益。

如果你真的渴望混淆您的代码进一步可以做到这一点:

def self.method_missing(method, *args) 
    if method =~ /^(.*)_related_to$/ 
    related_to(*args, $1) 
    else 
    super 
    end 
end 

def self.related_to(user, context) 
    through = reflections[context.to_s.pluralize.to_sym].options[:through] 
    tbl = reflections[through].options[:class_name].underscore.pluralize.gsub('/', '_') 
    where("#{tbl}.user_id = ?", user.id) 
end 

请注意,我相信你的协会有几个错别字。也许应该是:

has_many :comment_article_category_relationships, 
    :class_name => 'Comments::Articles::ArticleCategoryRelationship' 
has_many :comments, 
    :through  => :comment_article_category_relationships, 
    :source  => :comment 

has_many :article_relationships, 
    :class_name => 'Articles::Categories::ArticleRelationship' 
has_many :articles, 
    :through  => :article_relationships, 
    :source  => :article