Created
November 26, 2009 17:17
-
-
Save thadd/243580 to your computer and use it in GitHub Desktop.
How to scope all of a model's finders based on a user preference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
around_filter :check_for_prefers_recent | |
protected | |
def check_for_prefers_recent | |
if current_user && current_user.show_recent_only? | |
Gift.scope_to_recent do | |
yield | |
end | |
else | |
yield | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Gift < ActiveRecord::Base | |
def self.scope_to_recent | |
with_scope(:find => {:conditions => ['gifts.updated_at > ?', 6.months.ago]}) do | |
yield | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment