This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
/
ideas_controller.rb
229 lines (191 loc) · 6.26 KB
/
ideas_controller.rb
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# encoding: UTF-8
class IdeasController < ApplicationController
include Traits::RequiresLogin
before_filter :require_account!
before_filter :cleanup_session
before_filter :map_state_names, only:[:create, :update]
before_filter :map_no_category, only:[:create, :update]
before_filter :can_create_idea, only:[:new, :create]
VALID_ANGLES = %w(discussable followed)
DEFAULT_ANGLE = VALID_ANGLES.last
VALID_ORDERS = %w(impact activity progress creation)
DEFAULT_ORDER = VALID_ORDERS.first
VALID_FILTERS = %w(all authored commented vetted backed)
DEFAULT_FILTER = VALID_FILTERS.first
VALID_VIEWS = %w(cards board)
DEFAULT_VIEW = VALID_VIEWS.first
def index
@angle = set_angle_from_params
@order = set_order_from_params_and_angle
@filter = set_filter_from_params_and_angle
@category = set_category_from_params_and_angle
@view = set_view_from_params
@ideas = current_account.ideas.
send(:"#{@angle}_by", current_user).
send(:"by_#{@order}")
@ideas = @ideas.send(:"#{@filter}_by", current_user) unless @filter == 'all'
if @category == 'none'
@ideas = @ideas.where(category: nil)
elsif @category != 'all'
@ideas = @ideas.where(category: @category)
end
# eager-load participants
@ideas.map(&:participants)
end
def show
@idea = find_idea(params[:id])
self.current_account = @idea.account
@just_submitted = session.delete(:just_submitted)
if request.xhr?
case params['part']
when 'attachments'
render_ujs @idea.attachments
else
render nothing:true, status:400
end
end
end
def new
@idea = current_user.ideas.new
end
def create
@idea = current_user.ideas.new(params[:idea])
@idea.account = current_account
if @idea.save
session[:just_submitted] = true
redirect_to @idea
else
render :action => 'new'
end
end
def edit
@idea = find_idea(params[:id])
end
def update
@idea = find_idea(params[:id])
# specifics on account change
if new_account_id = params[:idea].andand.delete(:account_id)
update_account @idea, new_account_id
return
end
# specifics on state changes
if state = params[:idea].andand[:state]
# auto-set product manager when picking
if state == Idea.state_value(:picked)
@idea.product_manager = current_user
end
if @idea.draft? && state == Idea.state_value(:submitted)
# this is a draft promoted to submitted
session[:just_submitted] = true
end
end
if @idea.update_attributes(params[:idea])
current_user.bookmarked_ideas.add!(@idea)
redirect_to @idea, notice:_("Successfully updated idea.")
else
session.delete(:just_submitted)
render :action => 'edit'
end
end
def destroy
@idea = Idea.find(params[:id])
@idea.destroy
redirect_to ideas_url, :notice => "Successfully destroyed idea."
end
private
include AuthorizationHelper
def update_account(idea, new_account_id)
new_account = Account.find(new_account_id)
# check autorization
unless can?(:move, @idea)
flash[:error] = _('You cannot move this idea unless you are the author, the product manager, or an account owner.')
redirect_to @idea
return
end
unless current_login.accounts.include?(new_account)
flash[:error] = _('You must be a member of the target account to move and idea there.')
redirect_to @idea
return
end
IdeaMoverService.new(idea:@idea, account:new_account).run
flash[:notice] = _('Successfully changed idea account')
redirect_to @idea
end
def find_idea(id)
idea = Idea.where(account_id:current_login.accounts.value_of(:id)).find(id)
self.current_account = idea.account
return idea
end
def cleanup_session
{
ideas_angle: String,
ideas_order: Hash,
ideas_filter: Hash
}.each_pair do |session_key, type|
next if session[session_key].kind_of?(type)
session.delete(session_key)
end
end
def set_angle_from_params
params[:angle] =
session[:ideas_angle] = begin
(VALID_ANGLES.include?(params[:angle]) and params[:angle]) ||
(VALID_ANGLES.include?(session[:ideas_angle]) and session[:ideas_angle]) ||
session[:ideas_angle] ||
DEFAULT_ANGLE
end
end
def set_view_from_params
params[:view] =
session[:ideas_view] = begin
(VALID_VIEWS.include?(params[:view]) and params[:view]) ||
(VALID_VIEWS.include?(session[:ideas_view]) and session[:ideas_view]) ||
session[:ideas_view] ||
DEFAULT_VIEW
end
end
def set_order_from_params_and_angle
session[:ideas_order] ||= {}
params[:order] =
session[:ideas_order][@angle] = begin
(VALID_ORDERS.include?(params[:order]) and params[:order]) ||
(VALID_ORDERS.include?(session[:ideas_order][@angle]) and session[:ideas_order][@angle]) ||
DEFAULT_ORDER
end
end
def set_filter_from_params_and_angle
session[:ideas_filter] ||= {}
params[:filter] =
session[:ideas_filter][@angle] = begin
(VALID_FILTERS.include?(params[:filter]) and params[:filter]) ||
(VALID_FILTERS.include?(session[:ideas_filter][@angle]) and session[:ideas_filter][@angle]) ||
DEFAULT_FILTER
end
end
def set_category_from_params_and_angle
session[:ideas_category] ||= {}
params[:category] =
session[:ideas_category][@angle] = begin
valid_categories = (current_user.account.andand.categories || []) + %w(all none)
(valid_categories.include?(params[:category]) and params[:category]) ||
(valid_categories.include?(session[:ideas_category][@angle]) and session[:ideas_category][@angle]) ||
'all'
end
end
def map_state_names
# map state names to values
return unless state = params[:idea].andand[:state]
params[:idea][:state] = Idea.state_value(state)
end
def map_no_category
# map 'none' category to the valid nil
return unless category = params[:idea].andand[:category]
return unless category == 'none'
params[:idea][:category] = nil
end
def can_create_idea
return if can?(:create, Idea)
flash[:error] = not_authorized_message(:create, Idea)
redirect_back_or_to ideas_path
end
end