干燥范围方法

时间:2011-07-12 23:01:16

标签: ruby-on-rails ruby ruby-on-rails-3 scope dry

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

在模型文件中我有:

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 个答案:

答案 0 :(得分: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)。但我同意马克斯·威廉姆斯的观点。这会不必要地混淆您的代码而没有真正的收益。

如果您真的希望进一步混淆代码,可以这样做:

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