summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarius Mathiesen <marius@shortcut.no>2009-11-26 10:25:39 +0100
committerMarius Mathiesen <marius@shortcut.no>2009-12-03 14:18:57 +0100
commit0cee27f7c42ebe44f5601f78ba4b804709acfe82 (patch)
tree664dc57f73f3b1c67aed5c66b57e368bcda9137e
parent9fa97f6449b9a6afa30e845b472d0fa4b2c37659 (diff)
downloadgitorious-mainline-outdated-0cee27f7c42ebe44f5601f78ba4b804709acfe82.zip
gitorious-mainline-outdated-0cee27f7c42ebe44f5601f78ba4b804709acfe82.tar.gz
gitorious-mainline-outdated-0cee27f7c42ebe44f5601f78ba4b804709acfe82.tar.bz2
Improve the favorite/favoritable model:
- Validations - Extract watchable logic into module
-rw-r--r--app/models/favorite.rb1
-rw-r--r--app/models/repository.rb1
-rw-r--r--app/models/user.rb4
-rw-r--r--lib/watchable.rb32
-rw-r--r--test/fixtures/favorites.yml4
-rw-r--r--test/unit/favorite_test.rb24
6 files changed, 63 insertions, 3 deletions
diff --git a/app/models/favorite.rb b/app/models/favorite.rb
index a8df7e1..b1d28de 100644
--- a/app/models/favorite.rb
+++ b/app/models/favorite.rb
@@ -2,4 +2,5 @@ class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :watchable, :polymorphic => true
validates_presence_of :user_id, :watchable_id, :watchable_type
+ validates_uniqueness_of :user_id, :scope => [:watchable_id, :watchable_type]
end
diff --git a/app/models/repository.rb b/app/models/repository.rb
index 8b2c79b..7de3851 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -26,6 +26,7 @@
class Repository < ActiveRecord::Base
include ActiveMessaging::MessageSender
include RecordThrottling
+ include Watchable
KIND_PROJECT_REPO = 0
KIND_WIKI = 1
diff --git a/app/models/user.rb b/app/models/user.rb
index 776eaba..23fbf15 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -367,6 +367,10 @@ class User < ActiveRecord::Base
end
end
+ def watched_objects
+ favorites.collect(&:watchable)
+ end
+
protected
# before filter
def encrypt_password
diff --git a/lib/watchable.rb b/lib/watchable.rb
new file mode 100644
index 0000000..6d5f4e7
--- /dev/null
+++ b/lib/watchable.rb
@@ -0,0 +1,32 @@
+# encoding: utf-8
+#--
+# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#++
+
+# By including this module in an AR::Base descendant, this class becomes watchable:
+# - has_many :favorites
+# - receives instance methods
+module Watchable
+
+ def self.included(base)
+ base.has_many :favorites, :as => :watchable
+ base.has_many :watchers, :through => :favorites, :source => :user
+ end
+
+ def watched_by?(user)
+ watchers.include?(user)
+ end
+end
diff --git a/test/fixtures/favorites.yml b/test/fixtures/favorites.yml
new file mode 100644
index 0000000..81f728b
--- /dev/null
+++ b/test/fixtures/favorites.yml
@@ -0,0 +1,4 @@
+johans_first_favorite:
+ user_id: 1
+ watchable_type: Repository
+ watchable_id: 1 \ No newline at end of file
diff --git a/test/unit/favorite_test.rb b/test/unit/favorite_test.rb
index 60b1c7d..ac07427 100644
--- a/test/unit/favorite_test.rb
+++ b/test/unit/favorite_test.rb
@@ -1,18 +1,24 @@
require 'test_helper'
class FavoriteTest < ActiveSupport::TestCase
+ def create_favorited_repo
+ user = Factory.create(:user)
+ project = Factory.create(:project, :user => user, :owner => user)
+ repo = Factory.create(:repository, :user => user, :project => project, :owner => user)
+ [user, project, repo]
+ end
+
context "In general" do
should_require_attributes(:watchable_type, :watchable_id,
:user_id)
should_belong_to :user
should_belong_to :watchable
+ should_validate_uniqueness_of :user_id, :scoped_to => [:watchable_id, :watchable_type]
end
context "Watching a repository" do
setup do
- @user = Factory.create(:user)
- @project = Factory.create(:project, :user => @user, :owner => @user)
- @repo = Factory.create(:repository, :user => @user, :project => @project, :owner => @user)
+ @user, @project, @repo = create_favorited_repo
end
should "work" do
@@ -22,5 +28,17 @@ class FavoriteTest < ActiveSupport::TestCase
assert favorite.save
assert @user.favorites.include?(favorite)
end
+
+ should "give access to the watched object" do
+ favorite = @user.favorites.create(:watchable => @repo)
+ assert @user.watched_objects.include?(@repo)
+ end
+
+ should "know if the user watches the repo" do
+ assert !@repo.watched_by?(@user)
+ favorite = @user.favorites.create(:watchable => @repo)
+ @repo.favorites.reload
+ assert @repo.watched_by?(@user)
+ end
end
end