Skip to content

Instantly share code, notes, and snippets.

@rafer
Created February 8, 2010 16:18
Show Gist options
  • Save rafer/298303 to your computer and use it in GitHub Desktop.
Save rafer/298303 to your computer and use it in GitHub Desktop.
# 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