diff options
author | Louise Crow <louise.crow@gmail.com> | 2014-12-23 17:40:18 +0000 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2015-04-27 16:50:32 +0100 |
commit | 28428468c52037ea4ee40a31f37edce7a334973c (patch) | |
tree | 3158d36d3515cf2376834dd0f8b677d3f3ca4d7b | |
parent | 5b99495ef39feda47da098f896a801beafc892c5 (diff) |
Use resource-based RESTful routing and separate controller
-rw-r--r-- | app/controllers/request_controller.rb | 18 | ||||
-rw-r--r-- | app/controllers/track_controller.rb | 10 | ||||
-rw-r--r-- | app/controllers/widgets_controller.rb | 43 | ||||
-rw-r--r-- | app/views/widgets/new.html.erb (renamed from app/views/request/create_widget.html.erb) | 4 | ||||
-rw-r--r-- | app/views/widgets/show.html.erb (renamed from app/views/request/widget.html.erb) | 8 | ||||
-rw-r--r-- | config/routes.rb | 3 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 61 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 46 | ||||
-rw-r--r-- | spec/controllers/widgets_controller_spec.rb | 111 |
9 files changed, 160 insertions, 144 deletions
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index 4f86fa2dc..e847cae1e 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -7,7 +7,6 @@ require 'zip/zip' require 'open-uri' -require 'securerandom' class RequestController < ApplicationController before_filter :check_read_only, :only => [ :new, :show_response, :describe_state, :upload_response ] @@ -162,23 +161,6 @@ class RequestController < ApplicationController @show_no_more_than = (@matches_estimated > MAX_RESULTS) ? MAX_RESULTS : @matches_estimated end - def widget - medium_cache - @info_request = InfoRequest.find(params[:id]) - @track_thing = TrackThing.create_track_for_request(@info_request) - @status = @info_request.calculate_status - unless @user or cookies[:widget_vote] - cookies.permanent[:widget_vote] = SecureRandom.hex(10) - end - render :template => 'request/widget', :layout => false - end - - def create_widget - long_cache - @info_request = InfoRequest.find(params[:id]) - render :template => 'request/create_widget' - end - def list medium_cache @view = params[:view] diff --git a/app/controllers/track_controller.rb b/app/controllers/track_controller.rb index afe10be91..8b8055b55 100644 --- a/app/controllers/track_controller.rb +++ b/app/controllers/track_controller.rb @@ -214,14 +214,4 @@ class TrackController < ApplicationController redirect_to URI.parse(params[:r]).path end - # Track interest in a request from a non-logged in user - def widget_vote - @info_request = InfoRequest.find(params[:info_request_id]) - if not @user and cookies[:widget_vote] - wv = @info_request.widget_votes.where(:cookie => cookies[:widget_vote]).first_or_create - end - - track_thing = TrackThing.create_track_for_request(@info_request) - redirect_to do_track_path(track_thing), status => :temporary_redirect - end end diff --git a/app/controllers/widgets_controller.rb b/app/controllers/widgets_controller.rb new file mode 100644 index 000000000..bdbabeac4 --- /dev/null +++ b/app/controllers/widgets_controller.rb @@ -0,0 +1,43 @@ +# app/controllers/widget_controller.rb: +# Handle widgets, if enabled +# +# 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 :find_info_request + + def show + medium_cache + @track_thing = TrackThing.create_track_for_request(@info_request) + @status = @info_request.calculate_status + unless @user or cookies[:widget_vote] + cookies.permanent[:widget_vote] = SecureRandom.hex(10) + end + render :action => 'show', :layout => false + end + + def new + long_cache + end + + # Track interest in a request from a non-logged in user + def update + if not @user and cookies[:widget_vote] + wv = @info_request.widget_votes.where(:cookie => cookies[:widget_vote]).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 + @info_request = InfoRequest.find(params[:request_id]) + end + +end diff --git a/app/views/request/create_widget.html.erb b/app/views/widgets/new.html.erb index d6a71768a..072645d09 100644 --- a/app/views/request/create_widget.html.erb +++ b/app/views/widgets/new.html.erb @@ -4,12 +4,12 @@ To add a widget for <b><%= @info_request.title %></b>, copy and paste the following code to your web page: <textarea autofocus readonly rows='4' cols='60' id='widgetbox'> -<iframe src='<%= widget_url(@info_request) %>' width='320' height='215' frameborder='0' marginwidth='0' marginheight='0'> +<iframe src='<%= request_widget_url(@info_request) %>' width='320' height='215' frameborder='0' marginwidth='0' marginheight='0'> </textarea> </p> <p> The widget will look like this: <br> -<iframe src='<%= widget_url(@info_request) %>' width='320' height='215' frameborder='0' marginwidth='0' marginheight='0'> +<iframe src='<%= request_widget_url(@info_request) %>' width='320' height='215' frameborder='0' marginwidth='0' marginheight='0'> </p> diff --git a/app/views/request/widget.html.erb b/app/views/widgets/show.html.erb index 89e151f17..4de1eb446 100644 --- a/app/views/request/widget.html.erb +++ b/app/views/widgets/show.html.erb @@ -75,11 +75,9 @@ </div> </a> <% else %> - <a href="<%= url_for :controller => 'track', :action => 'widget_vote', :info_request_id => @info_request %>" target="_blank"> - <div id="alawidget-button"> - <%= _('I also want to know!') %> - </div> - </a> + <%= form_tag request_widget_url(@info_request), :method => 'put', :target => '_blank' do %> + <%= submit_tag _('I also want to know!'), :id => "alawidget-button" %> + <% end %> <% end %> <% end %> </div> diff --git a/config/routes.rb b/config/routes.rb index 011fe3167..7319f92fc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,8 +54,6 @@ Alaveteli::Application.routes.draw do match '/request/:id/response/:incoming_message_id' => 'request#show_response', :as => :show_response match '/request/:id/response/:incoming_message_id/attach/html/:part/*file_name' => 'request#get_attachment_as_html', :format => false, :as => :get_attachment_as_html match '/request/:id/response/:incoming_message_id/attach/:part(/*file_name)' => 'request#get_attachment', :format => false, :as => :get_attachment - match '/request/:id/widget' => 'request#widget', :as => :widget - match '/request/:id/widget/create' => 'request#create_widget', :as => :create_widget match '/request_event/:info_request_event_id' => 'request#show_request_event', :as => :info_request_event @@ -67,6 +65,7 @@ Alaveteli::Application.routes.draw do resources :request, :only => [] do resource :report, :only => [:new, :create] + resource :widget, :only => [:new, :show, :update] end resources :info_request_batch, :only => :show diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index cdd1b43f0..c4f3c847e 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -2764,64 +2764,3 @@ describe RequestController, "#select_authorities" do end end - -describe RequestController, "#widget" do - - before do - @info_request = FactoryGirl.create(:info_request) - end - - it 'should render the widget template' do - get :widget, :id => @info_request.id - expect(response).to render_template('widget') - end - - it 'should find the info request' do - get :widget, :id => @info_request.id - assigns[:info_request].should == @info_request - end - - it 'should create a track thing for the request' do - get :widget, :id => @info_request.id - assigns[:track_thing].info_request.should == @info_request - end - - it 'should assign the request status' do - get :widget, :id => @info_request.id - assigns[:status].should == @info_request.calculate_status - end - - context 'for a non-logged-in user' do - - context 'if no widget-vote cookie is set' do - - it 'should set a widget-vote cookie' do - cookies[:widget_vote].should be_nil - get :widget, :id => @info_request.id - cookies[:widget_vote].should_not be_nil - end - - end - - end - - -end - -describe RequestController, "#create_widget" do - - before do - @info_request = FactoryGirl.create(:info_request) - end - - it 'should render the create widget template' do - get :create_widget, :id => @info_request.id - expect(response).to render_template('create_widget') - end - - it 'should find the info request' do - get :create_widget, :id => @info_request.id - assigns[:info_request].should == @info_request - end - -end diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index b7f679cdb..29f5c7fe1 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -275,49 +275,3 @@ describe TrackController, "when tracking a public body" do end end - -describe TrackController do - include LinkToHelper - - describe :widget_vote do - - before do - @info_request = FactoryGirl.create(:info_request) - end - - it 'should find the info request' do - get :widget_vote, :info_request_id => @info_request.id - assigns[:info_request].should == @info_request - end - - it 'should redirect to the track path for the info request' do - get :widget_vote, :info_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 'when there is no logged-in user and a widget vote cookie' do - - before do - @cookie_value = 'x' * 20 - end - - it 'should create a widget vote if none exists for the info request and cookie' do - @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 0 - request.cookies['widget_vote'] = @cookie_value - get :widget_vote, :info_request_id => @info_request.id - @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 1 - end - - it 'should not create a widget vote if one exists for the info request and cookie' do - @info_request.widget_votes.create(:cookie => @cookie_value) - request.cookies['widget_vote'] = @cookie_value - get :widget_vote, :info_request_id => @info_request.id - @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 1 - end - - end - - end - -end diff --git a/spec/controllers/widgets_controller_spec.rb b/spec/controllers/widgets_controller_spec.rb new file mode 100644 index 000000000..e60f1b3cb --- /dev/null +++ b/spec/controllers/widgets_controller_spec.rb @@ -0,0 +1,111 @@ +# coding: utf-8 +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe WidgetsController do + + include LinkToHelper + + describe "#show" do + + before do + @info_request = FactoryGirl.create(:info_request) + end + + it 'should render the widget template' do + get :show, :request_id => @info_request.id + expect(response).to render_template('show') + end + + it 'should find the info request' do + get :show, :request_id => @info_request.id + assigns[:info_request].should == @info_request + end + + it 'should create a track thing for the request' do + get :show, :request_id => @info_request.id + assigns[:track_thing].info_request.should == @info_request + end + + it 'should assign the request status' do + get :show, :request_id => @info_request.id + assigns[:status].should == @info_request.calculate_status + end + + context 'for a non-logged-in user' do + + context 'if no widget-vote cookie is set' do + + 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 + + end + + + end + + describe "#new" do + + before do + @info_request = FactoryGirl.create(:info_request) + end + + it 'should render the create widget template' do + get :new, :request_id => @info_request.id + expect(response).to render_template('new') + end + + it 'should find the info request' do + get :new, :request_id => @info_request.id + assigns[:info_request].should == @info_request + end + + end + + describe :update do + + before do + @info_request = FactoryGirl.create(:info_request) + end + + it 'should find the info request' do + get :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 + get :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 'when there is no logged-in user and a widget vote cookie' do + + before do + @cookie_value = 'x' * 20 + end + + it 'should create a widget vote if none exists for the info request and cookie' do + @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 0 + request.cookies['widget_vote'] = @cookie_value + get :update, :request_id => @info_request.id + @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 1 + end + + it 'should not create a widget vote if one exists for the info request and cookie' do + @info_request.widget_votes.create(:cookie => @cookie_value) + request.cookies['widget_vote'] = @cookie_value + get :update, :request_id => @info_request.id + @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 1 + end + + end + + end + +end + |