diff options
author | Johan Sørensen <johan@johansorensen.com> | 2009-12-10 12:30:19 +0100 |
---|---|---|
committer | Johan Sørensen <johan@johansorensen.com> | 2009-12-10 12:30:19 +0100 |
commit | 631eeaa618b9cd9f2b13b8c6aac922c85e6ed3b2 (patch) | |
tree | b868d76c19b02c145c62376127f96a276e071ed4 | |
parent | 0ccbb48e4b4a10c748a34b07f7044a741b05981f (diff) | |
download | gitorious-mainline-outdated-631eeaa618b9cd9f2b13b8c6aac922c85e6ed3b2.zip gitorious-mainline-outdated-631eeaa618b9cd9f2b13b8c6aac922c85e6ed3b2.tar.gz gitorious-mainline-outdated-631eeaa618b9cd9f2b13b8c6aac922c85e6ed3b2.tar.bz2 |
Added #watched_by!(user) to the Watchable module, as the api for creating favorites
-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 |