aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2015-06-29 14:50:00 +0100
committerGareth Rees <gareth@mysociety.org>2015-06-29 14:50:00 +0100
commit71f83c58c0782741d41f3b8a6636af2a6c243330 (patch)
tree5c52a0d35b954f75fc7498dd26d765b444d9a9be
parent3020d70292fbbb503585f1db5e0babdcdf3c675d (diff)
Extract WidgetsController#update to WidgetVotesController#create
The action is creating a WidgetVote resource, so should be in WidgetVotesController#create. A “Widget” is not the same concept as a WidgetVote.
-rw-r--r--app/controllers/widget_votes_controller.rb51
-rw-r--r--app/controllers/widgets_controller.rb21
-rw-r--r--app/views/widgets/show.html.erb2
-rw-r--r--config/routes.rb3
-rw-r--r--spec/controllers/widget_votes_controller_spec.rb95
-rw-r--r--spec/controllers/widgets_controller_spec.rb84
6 files changed, 149 insertions, 107 deletions
diff --git a/app/controllers/widget_votes_controller.rb b/app/controllers/widget_votes_controller.rb
new file mode 100644
index 000000000..5da462011
--- /dev/null
+++ b/app/controllers/widget_votes_controller.rb
@@ -0,0 +1,51 @@
+# -*- encoding : utf-8 -*-
+# app/controllers/widget_votes_controller.rb:
+# Handle widget votes, if enabled
+#
+# Copyright (c) 2014 UK Citizens Online Democracy. All rights reserved.
+# Email: hello@mysociety.org; WWW: http://www.mysociety.org/
+
+require 'securerandom'
+
+class WidgetVotesController < ApplicationController
+
+ before_filter :check_widget_config, :find_info_request, :check_prominence
+
+ # Track interest in a request from a non-logged in user
+ def create
+ unless @user
+ cookie = cookies[:widget_vote]
+
+ if cookie.nil?
+ cookies.permanent[:widget_vote] = SecureRandom.hex(10)
+ cookie = cookies[:widget_vote]
+ end
+
+ @info_request.widget_votes.
+ where(:cookie => cookie).
+ first_or_create
+ end
+
+ track_thing = TrackThing.create_track_for_request(@info_request)
+ redirect_to do_track_path(track_thing), status => :temporary_redirect
+ end
+
+ private
+
+ def check_widget_config
+ unless AlaveteliConfiguration::enable_widgets
+ raise ActiveRecord::RecordNotFound.new("Page not enabled")
+ end
+ end
+
+ def find_info_request
+ @info_request = InfoRequest.find(params[:request_id])
+ end
+
+ def check_prominence
+ unless @info_request.prominence == 'normal'
+ render :nothing => true, :status => :forbidden
+ end
+ end
+
+end
diff --git a/app/controllers/widgets_controller.rb b/app/controllers/widgets_controller.rb
index aa76336df..32bfebf50 100644
--- a/app/controllers/widgets_controller.rb
+++ b/app/controllers/widgets_controller.rb
@@ -5,8 +5,6 @@
# Copyright (c) 2014 UK Citizens Online Democracy. All rights reserved.
# Email: hello@mysociety.org; WWW: http://www.mysociety.org/
-require 'securerandom'
-
class WidgetsController < ApplicationController
before_filter :check_widget_config, :find_info_request, :check_prominence
@@ -31,25 +29,6 @@ class WidgetsController < ApplicationController
long_cache
end
- # Track interest in a request from a non-logged in user
- def update
- unless @user
- cookie = cookies[:widget_vote]
-
- if cookie.nil?
- cookies.permanent[:widget_vote] = SecureRandom.hex(10)
- cookie = cookies[:widget_vote]
- end
-
- @info_request.widget_votes.
- where(:cookie => cookie).
- first_or_create
- end
-
- track_thing = TrackThing.create_track_for_request(@info_request)
- redirect_to do_track_path(track_thing), status => :temporary_redirect
- end
-
private
def find_info_request
diff --git a/app/views/widgets/show.html.erb b/app/views/widgets/show.html.erb
index 44437cc9c..0517b1637 100644
--- a/app/views/widgets/show.html.erb
+++ b/app/views/widgets/show.html.erb
@@ -44,7 +44,7 @@
</div>
</a>
<% else %>
- <%= form_tag request_widget_url(@info_request), :method => 'put', :target => '_blank' do %>
+ <%= form_tag request_widget_votes_url(@info_request), :method => 'post', :target => '_blank' do %>
<%= submit_tag _('I also want to know!'), :class => 'alaveteli-widget__button' %>
<% end %>
<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index 3f21d82c8..6ed01d7ce 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -65,7 +65,8 @@ Alaveteli::Application.routes.draw do
resources :request, :only => [] do
resource :report, :only => [:new, :create]
- resource :widget, :only => [:new, :show, :update]
+ resource :widget, :only => [:new, :show]
+ resources :widget_votes, :only => [:create]
end
resources :info_request_batch, :only => :show
diff --git a/spec/controllers/widget_votes_controller_spec.rb b/spec/controllers/widget_votes_controller_spec.rb
new file mode 100644
index 000000000..2b7ad49cd
--- /dev/null
+++ b/spec/controllers/widget_votes_controller_spec.rb
@@ -0,0 +1,95 @@
+# -*- encoding : utf-8 -*-
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe WidgetVotesController do
+
+ include LinkToHelper
+
+ describe 'POST create' do
+
+ before do
+ @info_request = FactoryGirl.create(:info_request)
+ AlaveteliConfiguration.stub!(:enable_widgets).and_return(true)
+ end
+
+ it 'should find the info request' do
+ post :create, :request_id => @info_request.id
+ assigns[:info_request].should == @info_request
+ end
+
+ it 'should redirect to the track path for the info request' do
+ post :create, :request_id => @info_request.id
+ track_thing = TrackThing.create_track_for_request(@info_request)
+ expect(response).to redirect_to(do_track_path(track_thing))
+ end
+
+ context 'for a non-logged-in user without a tracking cookie' do
+
+ it 'sets a tracking cookie' do
+ SecureRandom.stub!(:hex).and_return(mock_cookie)
+ post :create, :request_id => @info_request.id
+ expect(cookies[:widget_vote]).to eq(mock_cookie)
+ end
+
+ it 'creates a widget vote' do
+ SecureRandom.stub!(:hex).and_return(mock_cookie)
+ votes = @info_request.
+ widget_votes.
+ where(:cookie => mock_cookie)
+
+ post :create, :request_id => @info_request.id
+
+ expect(votes).to have(1).item
+ end
+
+ end
+
+ context 'for a non-logged-in user with a tracking cookie' do
+
+ it 'retains the existing tracking cookie' do
+ request.cookies['widget_vote'] = mock_cookie
+ post :create, :request_id => @info_request.id
+ expect(cookies[:widget_vote]).to eq(mock_cookie)
+ end
+
+ it 'creates a widget vote' do
+ request.cookies['widget_vote'] = mock_cookie
+ votes = @info_request.
+ widget_votes.
+ where(:cookie => mock_cookie)
+
+ post :create, :request_id => @info_request.id
+
+ expect(votes).to have(1).item
+ end
+
+ end
+
+ context 'when widgets are not enabled' do
+
+ it 'raises ActiveRecord::RecordNotFound' do
+ AlaveteliConfiguration.stub!(:enable_widgets).and_return(false)
+ lambda{ post :create, :request_id => @info_request.id }.should
+ raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ end
+
+ context "when the request's prominence is not 'normal'" do
+
+ it 'should return a 403' do
+ @info_request.prominence = 'hidden'
+ @info_request.save!
+ post :create, :request_id => @info_request.id
+ response.code.should == "403"
+ end
+
+ end
+
+ end
+
+end
+
+def mock_cookie
+ '0300fd3e1177127cebff'
+end
diff --git a/spec/controllers/widgets_controller_spec.rb b/spec/controllers/widgets_controller_spec.rb
index a8702f2a5..0e12d6ecb 100644
--- a/spec/controllers/widgets_controller_spec.rb
+++ b/spec/controllers/widgets_controller_spec.rb
@@ -173,90 +173,6 @@ describe WidgetsController do
end
- describe 'PUT update' do
-
- before do
- @info_request = FactoryGirl.create(:info_request)
- AlaveteliConfiguration.stub!(:enable_widgets).and_return(true)
- end
-
- it 'should find the info request' do
- put :update, :request_id => @info_request.id
- assigns[:info_request].should == @info_request
- end
-
- it 'should redirect to the track path for the info request' do
- put :update, :request_id => @info_request.id
- track_thing = TrackThing.create_track_for_request(@info_request)
- expect(response).to redirect_to(do_track_path(track_thing))
- end
-
- context 'for a non-logged-in user without a tracking cookie' do
-
- it 'sets a tracking cookie' do
- SecureRandom.stub!(:hex).and_return(mock_cookie)
- put :update, :request_id => @info_request.id
- expect(cookies[:widget_vote]).to eq(mock_cookie)
- end
-
- it 'creates a widget vote' do
- SecureRandom.stub!(:hex).and_return(mock_cookie)
- votes = @info_request.
- widget_votes.
- where(:cookie => mock_cookie)
-
- put :update, :request_id => @info_request.id
-
- expect(votes).to have(1).item
- end
-
- end
-
-
- context 'for a non-logged-in user with a tracking cookie' do
-
- it 'retains the existing tracking cookie' do
- request.cookies['widget_vote'] = mock_cookie
- put :update, :request_id => @info_request.id
- expect(cookies[:widget_vote]).to eq(mock_cookie)
- end
-
- it 'creates a widget vote' do
- request.cookies['widget_vote'] = mock_cookie
- votes = @info_request.
- widget_votes.
- where(:cookie => mock_cookie)
-
- put :update, :request_id => @info_request.id
-
- expect(votes).to have(1).item
- end
-
- end
-
- context 'when widgets are not enabled' do
-
- it 'raises ActiveRecord::RecordNotFound' do
- AlaveteliConfiguration.stub!(:enable_widgets).and_return(false)
- lambda{ put :update, :request_id => @info_request.id }.should
- raise_error(ActiveRecord::RecordNotFound)
- end
-
- end
-
- context "when the request's prominence is not 'normal'" do
-
- it 'should return a 403' do
- @info_request.prominence = 'hidden'
- @info_request.save!
- put :show, :request_id => @info_request.id
- response.code.should == "403"
- end
-
- end
-
- end
-
end
def mock_cookie