加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – DRY范围方法

发布时间:2020-12-17 01:59:40 所属栏目:百科 来源:网络整理
导读:我正在使用 Ruby on Rails 3.0.7,我想干我的(不要重复自己)我的范围方法. 在模型文件中我有: class Articles::Category ActiveRecord::Base scope :article_related_to,lambda { |user| where('articles_categories_article_relationships.user_id = ?',use
我正在使用 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.

解决方法

我能提供的最好的是:

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,:source      => :comment

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读