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.
?
吆,我的眼睛都在流血只是試圖讓這件事直!無論如何,你可以通過@current_user對象獲得你正在尋找的東西嗎? – dogenpunk
@dogenpunk - 順便說一句:「我的眼睛在流血只是試圖讓這件事直」 ......你什麼意思? - 在我的情況下,我能得到什麼,我在尋找通過「@current_user」對象,但是,在這另一面,如問題描述我有同樣的問題。 – Backo
我認爲這是一個幹太遠個人:可讀性爲我拍幹,這使得代碼更難閱讀。 DRY應該是關於保持功能或值等在一個地方,而不是減少的代碼行。 –