diff options
-rw-r--r-- | app/controllers/favorites_controller.rb | 2 | ||||
-rw-r--r-- | lib/watchable.rb | 8 |
2 files changed, 7 insertions, 3 deletions
diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index d56ff07..9953265 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -21,7 +21,7 @@ class FavoritesController < ApplicationController before_filter :find_watchable, :only => [:create] def create - @favorite = current_user.favorites.create!(:watchable => @watchable) + @favorite = @watchable.watched_by!(current_user) respond_to do |wants| wants.html { flash[:notice] = "You are now watching this #{@watchable.class.name.downcase}" diff --git a/lib/watchable.rb b/lib/watchable.rb index 6d5ddcd..71e3767 100644 --- a/lib/watchable.rb +++ b/lib/watchable.rb @@ -20,13 +20,17 @@ # - has_many :favorites # - receives instance methods module Watchable - + def self.included(base) base.has_many :favorites, :as => :watchable, :dependent => :destroy base.has_many :watchers, :through => :favorites, :source => :user end - + def watched_by?(user) watchers.include?(user) end + + def watched_by!(a_user) + a_user.favorites.create!(:watchable => self) + end end |