aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/widgets_controller.rb
blob: 333a38e868851f1d6544763447a7e1bf663eed6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- encoding : utf-8 -*-
# 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 :check_widget_config, :find_info_request, :check_prominence
    skip_before_filter :set_x_frame_options_header, :only => [:show]

    def show
        medium_cache
        @track_thing = TrackThing.create_track_for_request(@info_request)
        @status = @info_request.calculate_status
        @count = @info_request.track_things.count + @info_request.widget_votes.count + 1

        if @user
            @existing_track = TrackThing.find_existing(@user, @track_thing)
        end
        unless @user || 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 !@user && cookies[:widget_vote]
            @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

    def check_widget_config
        unless AlaveteliConfiguration::enable_widgets
            raise ActiveRecord::RecordNotFound.new("Page not enabled")
        end
    end

    def check_prominence
        unless @info_request.prominence == 'normal'
            render :nothing => true, :status => :forbidden
        end
    end

end