pairwise_content.rb
8.82 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
class PairwisePlugin::PairwiseContent < Article
include ActionView::Helpers::TagHelper
settings_items :pairwise_question_id
settings_items :allow_new_ideas, :type => :boolean, :default => true
before_save :send_question_to_service
validate :validate_choices, :on => :create
attr_accessible :pairwise_question_id, :allow_new_ideas, :choices, :choices_saved
REASONS_ARRAY = [
{:text => _("I like both ideas"), :compare => false},
{:text => _("I think both ideas are the same"), :compare => false},
{:text => _("I don't know enough about either idea"),:compare => false},
{:text => _("I don't like either idea"), :compare => false},
{:text => _("I don't know enough about: "),:compare => true},
{:text => _("I just can't decide"),:compare => false}
]
def initialize(*args)
super(*args)
self.published = false
self.accept_comments = false
self.notify_comments = false
@next_prompt = nil
end
def has_next_prompt?
@next_prompt.present?
end
alias_method :original_view_url, :view_url
def result_url
profile.url.merge(
:controller => :pairwise_plugin_profile,
:action => :result,
:id => id)
end
def prompt_url
profile.url.merge(
:controller => :pairwise_plugin_profile,
:action => :prompt_tab,
:id => id)
end
def self.short_description
'Pairwise question'
end
def self.description
'Question managed by pairwise'
end
def to_html(options = {})
source = options["source"]
embeded = options.has_key? "embeded"
prompt_id = options["prompt_id"]
pairwise_content = self
proc do
locals = {:pairwise_content => pairwise_content, :source => source, :embeded => embeded, :prompt_id => prompt_id }
render :file => 'content_viewer/prompt', :locals => locals
end
end
def pairwise_client
@pairwise_client ||= Pairwise::Client.build(profile.id, environment.settings[:pairwise_plugin])
@pairwise_client
end
def prepare_prompt(user_identifier, prompt_id=nil)
prepared_question = question
if has_next_prompt?
prepared_question.set_prompt @next_prompt
else
prepared_question = self.question_with_prompt_for_visitor(user_identifier, prompt_id)
end
prepared_question
end
def question
begin
@question ||= pairwise_client.find_question_by_id(pairwise_question_id)
rescue Exception => error
errors.add(:base, error.message)
end
@question
end
def question_with_prompt_for_visitor(visitor='guest', prompt_id=nil)
pairwise_client.question_with_prompt(pairwise_question_id, visitor, prompt_id)
end
def description=(value)
@description=value
end
def description
begin
@description ||= question.name
rescue
@description = ""
end
@description
end
def pending_choices(filter, order)
if(question)
@inactive_choices ||= question.pending_choices(filter, order)
else
[]
end
end
def reproved_choices(filter, order)
@reproved_choices ||= question ? question.reproved_choices(filter, order) : []
end
def inactive_choices(options={})
if(question)
@inactive_choices ||= (question.choices_include_inactive - question.get_choices)
else
[]
end
end
def raw_choices(filter=nil, order=nil)
return [] if pairwise_question_id.nil?
@raw_choices ||= question ? question.get_choices(filter, order) : []
end
def choices
if raw_choices.nil?
@choices = []
else
begin
@choices ||= question.get_choices.map {|q| { q.id.to_s => q.data } }
rescue
@choices = []
end
end
@choices
end
def choices=(value)
@choices = value
end
def choices_saved
@choices_saved
end
def choices_saved=value
@choices_saved = value
end
def vote_to(prompt_id, direction, visitor, appearance_id)
raise _("Excepted question not found") if question.nil?
next_prompt = pairwise_client.vote(question.id, prompt_id, direction, visitor, appearance_id)
touch #invalidates cache
set_next_prompt(next_prompt)
next_prompt
end
def skip_prompt(prompt_id, visitor, appearance_id, reason=nil)
next_prompt = pairwise_client.skip_prompt(question.id, prompt_id, visitor, appearance_id, reason)
touch #invalidates cache
set_next_prompt(next_prompt)
next_prompt
end
def ask_skip_reasons(prompt)
reasons = REASONS_ARRAY.map do |item|
if item[:compare]
[ item[:text] + prompt.left_choice_text, item[:text] + prompt.right_choice_text]
else
item[:text]
end
end
reasons.flatten
end
def validate_choices
errors.add(:base, _("Choices empty")) if choices.nil?
errors.add(:base, _("Choices invalid format")) unless choices.is_a?(Array)
errors.add(:base, _("Choices invalid")) if choices.size == 0
choices.each do | choice |
if choice.empty?
errors.add(:base, _("Choice empty"))
break
end
end
end
def update_choice(choice_id, choice_text, active)
begin
return pairwise_client.update_choice(question, choice_id, choice_text, active)
rescue Exception => e
errors.add(:base, N_("Choices:") + " " + N_(e.message))
return false
end
end
def approve_choice(choice_id)
begin
return pairwise_client.approve_choice(question, choice_id)
rescue Exception => e
errors.add(:base, N_("Choices:") + " " + N_(e.message))
return false
end
end
def flag_choice(choice_id, explanation=nil)
pairwise_client.flag_choice(question, choice_id, explanation || 'reproved')
end
def find_choice id
return nil if question.nil?
question.find_choice id
end
def toggle_autoactivate_ideas(active_flag)
pairwise_client.toggle_autoactivate_ideas(question, active_flag)
end
def send_question_to_service
if new_record?
@question = create_pairwise_question
self.pairwise_question_id = @question.id
toggle_autoactivate_ideas(false)
else
#add new choices
unless @choices.nil?
@choices.each do |choice_text|
begin
unless choice_text.empty?
choice = pairwise_client.add_choice(pairwise_question_id, choice_text)
pairwise_client.approve_choice(question, choice.id)
end
rescue Exception => e
errors.add(:base, N_("Choices: Error adding new choice to question") + N_(e.message))
return false
end
end
end
#change old choices
unless @choices_saved.nil?
@choices_saved.each do |id,data|
begin
pairwise_client.update_choice(question, id, data, true)
rescue Exception => e
errors.add(:base, (N_("Choices:") + " " + N_(e.message)))
return false
end
end
end
begin
pairwise_client.update_question(pairwise_question_id, name)
rescue Exception => e
errors.add(:base, (N_("Question not saved: ") + N_(e.message)))
return false
end
end
end
def create_pairwise_question
question = pairwise_client.create_question(name, choices)
question
end
def ideas_contributors(options=nil)
question.get_ideas_contributors(options)
end
def allow_new_ideas?
allow_new_ideas
end
def add_new_idea(text, visitor=nil)
return false unless allow_new_ideas?
pairwise_client.add_new_idea(pairwise_question_id, text, visitor)
end
def join_choices(ids_choices_to_join, id_choice_elected, user)
ids_choices_to_join.each do |id_choice|
unless id_choice.eql?(id_choice_elected)
choice = question.find_choice(id_choice)
choice_related = PairwisePlugin::ChoicesRelated.new do |cr|
cr.question = self
cr.choice_id = choice.id
cr.parent_choice_id = id_choice_elected
cr.user = user
cr.save!
end
end
end
end
def copy(options = {})
attrs = attributes.reject! { |key, value| ATTRIBUTES_NOT_COPIED.include?(key.to_sym) }
attrs.merge!(options)
obj = self.class.new(attrs)
obj.pairwise_question_id = self.pairwise_question_id
obj.allow_new_ideas = self.allow_new_ideas
id = obj.send(:create_without_callbacks)
raise "object not saved" unless id
end
def copy!(options = {})
attrs = attributes.reject! { |key, value| ATTRIBUTES_NOT_COPIED.include?(key.to_sym) }
attrs.merge!(options)
#self.class.create!(attrs)
obj = self.class.new(attrs)
obj.pairwise_question_id = self.pairwise_question_id
obj.allow_new_ideas = self.allow_new_ideas
id = obj.send(:create_without_callbacks)
raise "object not saved" unless id
end
def page_size
20
end
private
def set_next_prompt(prompt)
@next_prompt = Pairwise::Prompt.new(prompt["prompt"])
end
end