summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarius Mathiesen <marius@shortcut.no>2009-11-26 12:01:08 +0100
committerMarius Mathiesen <marius@shortcut.no>2009-12-03 14:18:58 +0100
commit8f0505fb07af575059b69b71ddf2a1c09e7df9f9 (patch)
treedda698553dc87849a7f603322aacb06e38d14510
parent0cee27f7c42ebe44f5601f78ba4b804709acfe82 (diff)
downloadgitorious-mainline-outdated-8f0505fb07af575059b69b71ddf2a1c09e7df9f9.zip
gitorious-mainline-outdated-8f0505fb07af575059b69b71ddf2a1c09e7df9f9.tar.gz
gitorious-mainline-outdated-8f0505fb07af575059b69b71ddf2a1c09e7df9f9.tar.bz2
FavoritesController: provides watching of repositories
- Also added Copyright header where missing
-rw-r--r--app/controllers/favorites_controller.rb45
-rw-r--r--app/helpers/favorites_helper.rb2
-rw-r--r--app/models/favorite.rb23
-rw-r--r--test/functional/favorites_controller_test.rb82
-rw-r--r--test/unit/favorite_test.rb23
-rw-r--r--test/unit/helpers/favorites_helper_test.rb4
6 files changed, 179 insertions, 0 deletions
diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb
new file mode 100644
index 0000000..7c84a15
--- /dev/null
+++ b/app/controllers/favorites_controller.rb
@@ -0,0 +1,45 @@
+# encoding: utf-8
+#--
+# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
+# Copyright (C) 2007, 2008 Johan Sørensen <johan@johansorensen.com>
+# Copyright (C) 2008 David A. Cuadrado <krawek@gmail.com>
+# Copyright (C) 2008 Patrick Aljord <patcito@gmail.com>
+# Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
+# Copyright (C) 2009 Fabio Akita <fabio.akita@gmail.com>
+#
+# 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/>.
+#++
+
+class FavoritesController < ApplicationController
+ before_filter :login_required
+ before_filter :find_watchable, :only => [:create]
+
+ def create
+ @favorite = current_user.favorites.create(:watchable => @watchable)
+ respond_to do |wants|
+ wants.html {redirect_to repo_owner_path(@watchable, [@watchable.project, @watchable])}
+ wants.js {render :status => :created, :nothing => true}
+ end
+ end
+
+ private
+ def find_watchable
+ begin
+ watchable_class = params[:watchable_type].constantize
+ rescue NameError
+ raise ActiveRecord::RecordNotFound
+ end
+ @watchable = watchable_class.find(params[:watchable_id])
+ end
+end
diff --git a/app/helpers/favorites_helper.rb b/app/helpers/favorites_helper.rb
new file mode 100644
index 0000000..4e9a950
--- /dev/null
+++ b/app/helpers/favorites_helper.rb
@@ -0,0 +1,2 @@
+module FavoritesHelper
+end
diff --git a/app/models/favorite.rb b/app/models/favorite.rb
index b1d28de..67693dc 100644
--- a/app/models/favorite.rb
+++ b/app/models/favorite.rb
@@ -1,3 +1,26 @@
+# encoding: utf-8
+#--
+# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
+# Copyright (C) 2007, 2008 Johan Sørensen <johan@johansorensen.com>
+# Copyright (C) 2008 David A. Cuadrado <krawek@gmail.com>
+# Copyright (C) 2008 Patrick Aljord <patcito@gmail.com>
+# Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
+# Copyright (C) 2009 Fabio Akita <fabio.akita@gmail.com>
+#
+# 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/>.
+#++
+
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :watchable, :polymorphic => true
diff --git a/test/functional/favorites_controller_test.rb b/test/functional/favorites_controller_test.rb
new file mode 100644
index 0000000..78ed175
--- /dev/null
+++ b/test/functional/favorites_controller_test.rb
@@ -0,0 +1,82 @@
+# encoding: utf-8
+#--
+# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
+# Copyright (C) 2007, 2008 Johan Sørensen <johan@johansorensen.com>
+# Copyright (C) 2008 David A. Cuadrado <krawek@gmail.com>
+# Copyright (C) 2008 Patrick Aljord <patcito@gmail.com>
+# Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
+# Copyright (C) 2009 Fabio Akita <fabio.akita@gmail.com>
+#
+# 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/>.
+#++
+
+require 'test_helper'
+
+class FavoritesControllerTest < ActionController::TestCase
+ def do_create_post(type, id, extra_options={})
+ post :create, extra_options.merge(:watchable_type => type,
+ :watchable_id => id)
+ end
+
+ context "Creating a new favorite" do
+ setup {
+ login_as :johan
+ @repository = repositories(:johans)
+ }
+
+ should "require login" do
+ session[:user_id] = nil
+ post :create
+ assert_redirected_to new_sessions_path
+ end
+
+ should "assign to watchable" do
+ do_create_post(@repository.class.name, @repository.id)
+ assert_response :redirect
+ assert_equal @repository, assigns(:watchable)
+ end
+
+ should "render not found when missing watchable" do
+ do_create_post(@repository.class.name, 999)
+ assert_response :not_found
+ end
+
+ should "render not found when invalid watchable type is provided" do
+ do_create_post("RRepository", @repository.id)
+ assert_response :not_found
+ end
+
+ should "create a favorite" do
+ do_create_post(@repository.class.name, @repository.id)
+ assert_not_nil(favorite = assigns(:favorite))
+ end
+
+ should "redirect to the watchable itself" do
+ do_create_post(@repository.class.name, @repository.id)
+ assert_redirected_to([@repository.project, @repository])
+ end
+
+ context "JS requests" do
+ should "render :created" do
+ do_create_post(@repository.class.name, @repository.id, {:format => "js"})
+ assert_response :created
+ end
+
+ should "render :not_found" do
+ do_create_post("Rrepository", @repository.id)
+ assert_response :not_found
+ end
+ end
+ end
+end
diff --git a/test/unit/favorite_test.rb b/test/unit/favorite_test.rb
index ac07427..1c304b0 100644
--- a/test/unit/favorite_test.rb
+++ b/test/unit/favorite_test.rb
@@ -1,3 +1,26 @@
+# encoding: utf-8
+#--
+# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
+# Copyright (C) 2007, 2008 Johan Sørensen <johan@johansorensen.com>
+# Copyright (C) 2008 David A. Cuadrado <krawek@gmail.com>
+# Copyright (C) 2008 Patrick Aljord <patcito@gmail.com>
+# Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
+# Copyright (C) 2009 Fabio Akita <fabio.akita@gmail.com>
+#
+# 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/>.
+#++
+
require 'test_helper'
class FavoriteTest < ActiveSupport::TestCase
diff --git a/test/unit/helpers/favorites_helper_test.rb b/test/unit/helpers/favorites_helper_test.rb
new file mode 100644
index 0000000..fabd87c
--- /dev/null
+++ b/test/unit/helpers/favorites_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class FavoritesHelperTest < ActionView::TestCase
+end