aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2015-06-29 12:33:01 +0100
committerGareth Rees <gareth@mysociety.org>2015-06-29 12:33:01 +0100
commit3e9bc1a54ada31474a6bed10d38b95cd277d3712 (patch)
tree72010311b35b02c0431dda0f0e3dff650081b9fb
parentd3480d94da4c2ad3a76c8063b7e325bbaf48a396 (diff)
Set widget_vote cookie on update instead of show
A user hasn’t expressed any interest in the request just by viewing the widget, so don’t set the tracking cookie.
-rw-r--r--app/controllers/widgets_controller.rb19
-rw-r--r--spec/controllers/widgets_controller_spec.rb19
2 files changed, 25 insertions, 13 deletions
diff --git a/app/controllers/widgets_controller.rb b/app/controllers/widgets_controller.rb
index 8841c2aeb..aa76336df 100644
--- a/app/controllers/widgets_controller.rb
+++ b/app/controllers/widgets_controller.rb
@@ -23,9 +23,7 @@ class WidgetsController < ApplicationController
else
@tracking_cookie = cookies[:widget_vote]
end
- unless @user || cookies[:widget_vote]
- cookies.permanent[:widget_vote] = SecureRandom.hex(10)
- end
+
render :action => 'show', :layout => false
end
@@ -35,10 +33,17 @@ class WidgetsController < ApplicationController
# Track interest in a request from a non-logged in user
def update
- if !@user && cookies[:widget_vote]
- @info_request.widget_votes.
- where(:cookie => cookies[:widget_vote]).
- first_or_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)
diff --git a/spec/controllers/widgets_controller_spec.rb b/spec/controllers/widgets_controller_spec.rb
index eb4480ebf..7c60b847a 100644
--- a/spec/controllers/widgets_controller_spec.rb
+++ b/spec/controllers/widgets_controller_spec.rb
@@ -78,12 +78,6 @@ describe WidgetsController do
expect(assigns[:tracking_cookie]).to be_nil
end
- it 'should set a widget-vote cookie' do
- cookies[:widget_vote].should be_nil
- get :show, :request_id => @info_request.id
- cookies[:widget_vote].should_not be_nil
- end
-
end
context 'for a logged in user with tracks' do
@@ -200,6 +194,12 @@ describe WidgetsController do
context 'for a non-logged-in user without a tracking cookie' do
+ it 'sets a tracking cookie' do
+ SecureRandom.stub!(:hex).and_return('0300fd3e1177127cebff')
+ put :update, :request_id => @info_request.id
+ expect(cookies[:widget_vote]).to eq('0300fd3e1177127cebff')
+ end
+
it 'creates a widget vote' do
SecureRandom.stub!(:hex).and_return('0300fd3e1177127cebff')
votes = @info_request.
@@ -213,8 +213,15 @@ describe WidgetsController do
end
+
context 'for a non-logged-in user with a tracking cookie' do
+ it 'retains the existing tracking cookie' do
+ request.cookies['widget_vote'] = '0300fd3e1177127cebff'
+ put :update, :request_id => @info_request.id
+ expect(cookies[:widget_vote]).to eq('0300fd3e1177127cebff')
+ end
+
it 'creates a widget vote' do
request.cookies['widget_vote'] = '0300fd3e1177127cebff'
votes = @info_request.