rails:3435
From: 久野@サイベイト <hisano@s...>
Date: Thu, 27 May 2010 15:58:27 +0900
Subject: [rails:3435] 制約条件付きModelのDRY化
こんにちは、久野と申します。
あるModelのコレクションを作成する際に、ログインユーザの権限によって
制限をかけるためにwith_scopeを使って以下のような記述を行いました。
現状これを複数のModelに記述しているため、DRYにしたいと考えてます。
どこにどのように記述すべきでしょうか。
class Foo < ActiveRecord::Base
def self.restricted
# ここに権限毎の制約条件を設定
{:find => {:conditions => ["owner_id = ?", User.current_user]}}
end
private_class_method :restricted
def self.find_with_privileged_scope(*args)
with_scope(restricted) do
find_without_privileged_scope(*args)
end
end
def self.count_with_privileged_scope(*args)
with_scope(restricted) do
count_without_privileged_scope(*args)
end
end
class << self
alias :count_without_privileged_scope :count
alias :count :count_with_privileged_scope
alias :find_without_privileged_scope :find
alias :find :find_with_privileged_scope
end
end
なおModel.restrictedはModelに応じて条件が異なるため、
オーバーライドします。
--
ML: rails@r...
使い方: http://QuickML.com/
-> 3435 2010-05-27 08:58 [hisano@s... ] 制約条件付きModelのDRY化 3436 2010-05-27 13:53 ┗[kawaji@g... ] 3437 2010-05-28 02:30 ┗[hisano@s... ] Re: 制約条件付きModelのDRY化【解決】