Created
February 8, 2010 16:18
-
-
Save rafer/298303 to your computer and use it in GitHub Desktop.
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
# A mixin that makes a record "seeable" by users. | |
module Seeable | |
def self.included(base) | |
base.has_many :seeings, :as => :thing | |
end | |
# Note that user seeing this thing (now) | |
# Returns true if the user has previously seen the object, and false otherwise | |
# essentially the same as doing a seen_by? before noting the new seeing. | |
def seen_by!(user) | |
raise ArgumentError, 'A user object must be provided' if user.nil? | |
unless seen_by? user | |
Seeing.create(:user => user, :thing => self) | |
return false | |
end | |
return true | |
end | |
# Has this thing been seen by user? | |
def seen_by?(user) | |
seeings.exists?(:user_id => user.id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment