Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 390
- Log:
Initial import of Canvass, a donations-based poll application.
- Author:
- rool
- Date:
- Mon Mar 21 14:58:04 +0000 2011
- Size:
- 4834 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 | |
41 | unless ( user_id.nil? ) |
42 | redirect_to polls_path and return unless ( current_user.admin? ) |
43 | |
44 | @user = User.find_by_id( user_id ) |
45 | redirect_to polls_path and return if ( @user.nil? ) |
46 | |
47 | extra_conditions = [ 'user_id = ?', @user.id ] |
48 | end |
49 | |
50 | appctrl_search_sort_and_paginate( |
51 | Poll, |
52 | :default_sorting => { 'down' => '', 'field' => 'workflow_state' }, |
53 | :extra_conditions => extra_conditions, |
54 | :always_sort_by => "\"polls\".#{ Poll.translated_column( :title ) }" |
55 | ) |
56 | end |
57 | |
58 | # GET /polls/1 |
59 | def show |
60 | @poll = Poll.find( params[ :id ] ) |
61 | |
62 | # The following is for the admin-only nested donations list. |
63 | |
64 | if ( current_user.try( :admin? ) ) |
65 | params[ :poll_id ] = @poll.id |
66 | appctrl_search_sort_and_paginate( |
67 | Donation, |
68 | :default_sorting => Donation.default_sort_hash(), |
69 | :extra_conditions => Donation.conditions_for( params, current_user ) |
70 | ) |
71 | end |
72 | end |
73 | |
74 | # GET /polls/new |
75 | def new |
76 | @poll = Poll.new |
77 | @poll.currency = Currency.get_best_currency( current_user() ) |
78 | end |
79 | |
80 | # GET /polls/1/edit |
81 | def edit |
82 | @poll = Poll.find( params[ :id ] ) |
83 | end |
84 | |
85 | # POST /polls |
86 | def create |
87 | @poll = Poll.new( params[ :poll ] ) |
88 | @poll.user = current_user() |
89 | @poll.votes = 0 |
90 | @poll.total_integer = '0' |
91 | @poll.total_fraction = '0' |
92 | |
93 | Poll.transaction do |
94 | if @poll.save |
95 | appctrl_set_flash :notice |
96 | redirect_to @poll |
97 | else |
98 | render :action => 'new' |
99 | end |
100 | end |
101 | end |
102 | |
103 | # PUT /polls/1 |
104 | def update |
105 | @poll = Poll.find( params[ :id ] ) |
106 | |
107 | begin |
108 | |
109 | # Use a transaction to set the state and update the other attributes. We |
110 | # have to do lots of checking on the allowed state and turn it into a |
111 | # state transition method name. The Poll model may then do a lot of |
112 | # processing depending upon the nature of the transition. |
113 | |
114 | result = Poll.transaction do |
115 | new_state = params[ :poll ][ :workflow_state ] |
116 | |
117 | unless ( new_state.blank? ) |
118 | new_state = new_state.to_sym |
119 | params[ :poll ].delete( :workflow_state ) |
120 | |
121 | if ( @poll.allowed_new_states.include?( new_state ) ) |
122 | @poll.current_state.events.each do | event_name, event_action | |
123 | if ( event_action.transitions_to.to_sym == new_state ) |
124 | @poll.send( "#{ event_name }!" ) |
125 | end |
126 | end |
127 | end |
128 | end |
129 | |
130 | @poll.update_attributes( params[ :poll ] ) # Becomes value of transaction block |
131 | end |
132 | |
133 | rescue => error |
134 | |
135 | # The "reload" makes sure that attempted but failed state changes are |
136 | # noticed properly - database rollback isn't enough to update the model's |
137 | # state table. We only expect the model to raise an empty error message, |
138 | # which indicates that it's added a message to the 'state' attribute. |
139 | # However, in the unlikely event that a different exception occurs, we |
140 | # add this as an error connected with no specific attribute so that the |
141 | # user can see what went wrong. |
142 | |
143 | @poll.reload |
144 | @poll.errors.add_to_base( error.message ) unless error.message.empty? |
145 | |
146 | result = false |
147 | |
148 | end |
149 | |
150 | if ( result ) |
151 | appctrl_set_flash :notice |
152 | redirect_to @poll |
153 | else |
154 | render :action => 'edit' |
155 | end |
156 | end |
157 | |
158 | # DELETE /polls/1 |
159 | def destroy |
160 | @poll = Poll.find( params[ :id ] ) |
161 | |
162 | if ( @poll.donations.count.zero? ) |
163 | @poll.destroy |
164 | appctrl_set_flash :notice |
165 | redirect_to( polls_url() ) |
166 | else |
167 | appctrl_set_flash :error |
168 | redirect_to( @poll ) |
169 | end |
170 | end |
171 | end |