Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 472
- Log:
Hide closed and expired polls by default, with a link to show them
- Author:
- rool
- Date:
- Sat Jan 26 10:35:05 +0000 2019
- Size:
- 5034 Bytes
1 | ######################################################################## |
2 | # File:: polls_controller.rb |
3 | # (C):: Hipposoft 2011 |
4 | # |
5 | # Purpose:: Manage the poll list. |
6 | # ---------------------------------------------------------------------- |
7 | # 30-Jan-2011 (ADH): Created. |
8 | # 16-Feb-2011 (ADH): Almost completely replaced with code |
9 | # imported from Artisan. |
10 | ######################################################################## |
11 | |
12 | class PollsController < ApplicationController |
13 | |
14 | uses_prototype() |
15 | uses_leightbox( :only => [ :index, :show ] ) |
16 | |
17 | @@hubssolib_permissions = HubSsoLib::Permissions.new( { |
18 | :create => [ :admin, :webmaster ], |
19 | :new => [ :admin, :webmaster ], |
20 | :edit => [ :admin, :webmaster ], |
21 | :update => [ :admin, :webmaster ], |
22 | :delete => [ :admin, :webmaster ], |
23 | :destroy => [ :admin, :webmaster ], |
24 | } ) |
25 | |
26 | def PollsController.hubssolib_permissions |
27 | @@hubssolib_permissions |
28 | end |
29 | |
30 | def skip_main_heading? |
31 | [ :show, :index ].include?( action_name.to_sym ) |
32 | end |
33 | |
34 | # GET /polls |
35 | # GET /users/<n>/polls |
36 | def index |
37 | @user = nil |
38 | extra_conditions = nil |
39 | user_id = params[ :user_id ] |
40 | omit_closed = params[ :closed ].blank? |
41 | |
42 | if ( user_id.nil? ) |
43 | if ( omit_closed ) |
44 | extra_conditions = [ "workflow_state NOT IN ('#{ Poll::STATE_COMPLETED}', '#{Poll::STATE_EXPIRED}')" ] |
45 | end |
46 | else |
47 | redirect_to polls_path and return unless ( current_user.admin? ) |
48 | |
49 | @user = User.find_by_id( user_id ) |
50 | redirect_to polls_path and return if ( @user.nil? ) |
51 | |
52 | extra_conditions = [ 'user_id = ?', @user.id ] |
53 | end |
54 | |
55 | appctrl_search_sort_and_paginate( |
56 | Poll, |
57 | :default_sorting => { 'down' => '', 'field' => 'workflow_state' }, |
58 | :extra_conditions => extra_conditions, |
59 | :always_sort_by => "\"polls\".#{ Poll.translated_column( :title ) }" |
60 | ) |
61 | end |
62 | |
63 | # GET /polls/1 |
64 | def show |
65 | @poll = Poll.find( params[ :id ] ) |
66 | |
67 | # The following is for the admin-only nested donations list. |
68 | |
69 | if ( current_user.try( :admin? ) ) |
70 | params[ :poll_id ] = @poll.id |
71 | appctrl_search_sort_and_paginate( |
72 | Donation, |
73 | :default_sorting => Donation.default_sort_hash(), |
74 | :extra_conditions => Donation.conditions_for( params, current_user ) |
75 | ) |
76 | end |
77 | end |
78 | |
79 | # GET /polls/new |
80 | def new |
81 | @poll = Poll.new |
82 | @poll.currency = Currency.get_best_currency( current_user() ) |
83 | end |
84 | |
85 | # GET /polls/1/edit |
86 | def edit |
87 | @poll = Poll.find( params[ :id ] ) |
88 | end |
89 | |
90 | # POST /polls |
91 | def create |
92 | @poll = Poll.new( params[ :poll ] ) |
93 | @poll.user = current_user() |
94 | @poll.votes = 0 |
95 | @poll.total_integer = '0' |
96 | @poll.total_fraction = '0' |
97 | |
98 | Poll.transaction do |
99 | if @poll.save |
100 | appctrl_set_flash :notice |
101 | redirect_to @poll |
102 | else |
103 | render :action => 'new' |
104 | end |
105 | end |
106 | end |
107 | |
108 | # PUT /polls/1 |
109 | def update |
110 | @poll = Poll.find( params[ :id ] ) |
111 | |
112 | begin |
113 | |
114 | # Use a transaction to set the state and update the other attributes. We |
115 | # have to do lots of checking on the allowed state and turn it into a |
116 | # state transition method name. The Poll model may then do a lot of |
117 | # processing depending upon the nature of the transition. |
118 | |
119 | result = Poll.transaction do |
120 | new_state = params[ :poll ][ :workflow_state ] |
121 | |
122 | unless ( new_state.blank? ) |
123 | new_state = new_state.to_sym |
124 | params[ :poll ].delete( :workflow_state ) |
125 | |
126 | if ( @poll.allowed_new_states.include?( new_state ) ) |
127 | @poll.current_state.events.each do | event_name, event_action | |
128 | if ( event_action.transitions_to.to_sym == new_state ) |
129 | @poll.send( "#{ event_name }!" ) |
130 | end |
131 | end |
132 | end |
133 | end |
134 | |
135 | @poll.update_attributes( params[ :poll ] ) # Becomes value of transaction block |
136 | end |
137 | |
138 | rescue => error |
139 | |
140 | # The "reload" makes sure that attempted but failed state changes are |
141 | # noticed properly - database rollback isn't enough to update the model's |
142 | # state table. We only expect the model to raise an empty error message, |
143 | # which indicates that it's added a message to the 'state' attribute. |
144 | # However, in the unlikely event that a different exception occurs, we |
145 | # add this as an error connected with no specific attribute so that the |
146 | # user can see what went wrong. |
147 | |
148 | @poll.reload |
149 | @poll.errors.add_to_base( error.message ) unless error.message.empty? |
150 | |
151 | result = false |
152 | |
153 | end |
154 | |
155 | if ( result ) |
156 | appctrl_set_flash :notice |
157 | redirect_to @poll |
158 | else |
159 | render :action => 'edit' |
160 | end |
161 | end |
162 | |
163 | # DELETE /polls/1 |
164 | def destroy |
165 | @poll = Poll.find( params[ :id ] ) |
166 | |
167 | if ( @poll.donations.count.zero? ) |
168 | @poll.destroy |
169 | appctrl_set_flash :notice |
170 | redirect_to( polls_url() ) |
171 | else |
172 | appctrl_set_flash :error |
173 | redirect_to( @poll ) |
174 | end |
175 | end |
176 | end |