proposal_task.rb
6.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
class ProposalsDiscussionPlugin::ProposalTask < Task
has_and_belongs_to_many :categories,
class_name: "ProposalsDiscussionPlugin::TaskCategory",
join_table: :proposals_discussion_plugin_task_categories,
foreign_key: :task_id,
association_foreign_key: :category_id
has_one :proposal_evaluation
validates_presence_of :requestor_id, :target_id
validates_associated :article_object
validate :require_category
settings_items :name, :type => String
settings_items :ip_address, :type => String
settings_items :user_agent, :type => String
settings_items :referrer, :type => String
settings_items :article, :type => Hash, :default => {}
settings_items :closing_statment, :article_parent_id
scope :pending_evaluated, lambda { |profile, filter_type, filter_text|
self
.to(profile)
.without_spam.pending
.of(filter_type)
.like('data', filter_text)
.where(:status => ProposalsDiscussionPlugin::ProposalTask::Status.evaluated_statuses)
}
scope :filter_by_status, lambda { |profile, filter_type, filter_text, status_id|
self
.to(profile)
.without_spam.pending
.of(filter_type)
.like('data', filter_text)
.where(:status => status_id)
}
before_create do |task|
if !task.target.nil?
organization = task.target
responsible_candidates = organization.members.by_role(organization.roles.reject {|r| !r.has_permission?('view_tasks')})
task.responsible = responsible_candidates.sample
end
end
before_update do |task|
if task.target.present? && flagged? && task.changes[:status].present?
organization = task.target
responsible_candidates = organization.members.by_role(organization.roles.reject {|r| !r.has_permission?('perform_task')})
task.responsible = responsible_candidates.sample
end
end
def unflagged?
! flagged?
end
def flagged?
flagged_for_approval? || flagged_for_reproval?
end
def flagged_for_approval?
Status::FLAGGED_FOR_APPROVAL.eql?(self.status)
end
def flagged_for_reproval?
Status::FLAGGED_FOR_REPROVAL.eql?(self.status)
end
after_create :schedule_spam_checking
module Status
FLAGGED_FOR_APPROVAL = 5
FLAGGED_FOR_REPROVAL = 6
class << self
def names
[
nil,
N_('Active'), N_('Cancelled'), N_('Finished'),
N_('Hidden'), N_('Flagged for Approval'), N_('Flagged for Reproval')
]
end
def evaluated_statuses
[
FLAGGED_FOR_APPROVAL,
FLAGGED_FOR_REPROVAL
]
end
end
end
def flag_accept_proposal(evaluated_by)
transaction do
if evaluated_by
ProposalsDiscussionPlugin::ProposalEvaluation.new do |evaluation|
evaluation.evaluated_by = evaluated_by
evaluation.flagged_status = Status::FLAGGED_FOR_APPROVAL
evaluation.proposal_task = self
evaluation.save!
end
end
self.status = Status::FLAGGED_FOR_APPROVAL
self.save!
return true
end
end
def flag_reject_proposal(evaluated_by)
transaction do
if evaluated_by
ProposalsDiscussionPlugin::ProposalEvaluation.new do |evaluation|
evaluation.evaluated_by = evaluated_by
evaluation.flagged_status = Status::FLAGGED_FOR_REPROVAL
evaluation.proposal_task = self
evaluation.save!
end
end
self.status = Status::FLAGGED_FOR_REPROVAL
self.save!
return true
end
end
def schedule_spam_checking
self.delay.check_for_spam
end
def sender
requestor.name if requestor
end
def article_parent=(parent)
@article_parent = parent
end
def article_parent
@article_parent ||= Article.find_by_id article_parent_id.to_i
end
def article_object
if @article_object.nil?
@article_object = article_type.new(article.merge(target.present? ? {:profile => target} : {}).except(:type))
@article_object.parent = article_parent
@article_object.author = requestor if requestor.present?
end
@article_object
end
def article_type
if article[:type].present?
type = article[:type].constantize
return type if type < Article
end
TextArticle
end
def perform
article_object.save!
self.data[:article][:id] = article_object[:id]
self.save!
end
def title
_("New proposal")
end
def article_abstract
article[:abstract]
end
def abstract
article_abstract
end
def information
{:message => _("<span class=\"requestor\">%{requestor}</span> <span class=\"action-label\">wants to send the following proposal.</span><br/><span class=\"abstract\">%{abstract}</span>"), :variables => {:abstract => CGI.escapeHTML(abstract.to_s)}}
end
def icon
{:type => :profile_image, :profile => requestor, :url => requestor.url}
end
def target_notification_description
_('%{requestor} wants to send a proposal.') % {:requestor => requestor.name}
end
def target_notification_message
target_notification_description + "\n\n" +
_('You will need login to %{system} in order to accept or reject the proposal sent by %{requestor}') % { :system => target.environment.name, :requestor => requestor.name}
end
def accept_details
true
end
def reject_details
true
end
def default_decision
if article
'skip'
else
'reject'
end
end
def accept_disabled?
article.blank?
end
def task_finished_message
if !closing_statment.blank?
_("Your request for publishing the proposal \"%{article}\" was approved. Here is the comment left by the admin who approved your proposal:\n\n%{comment} ") % {:article => name, :comment => closing_statment}
else
_('Your request for publishing the proposal "%{article}" was approved.') % {:article => name}
end
end
def task_cancelled_message
message = _('Your request for publishing the proposal "%{article}" was rejected.') % {:article => name}
if !reject_explanation.blank?
message += " " + _("Here is the reject explanation left by the moderator who rejected your proposal: \n\n%{reject_explanation}") % {:reject_explanation => reject_explanation}
end
message
end
def after_spam!
SpammerLogger.log(ip_address, self)
self.delay.marked_as_spam
end
def after_ham!
self.delay.marked_as_ham
end
def to_liquid_with_proposal_task
hash = to_liquid_without_proposal_task
hash.merge(:article => article_object, :parent => article_parent)
end
alias_method_chain :to_liquid, :proposal_task
def categories_list(field = :name)
categories.pluck(field).join(',') if categories.count > 0
end
def proposal_source
parent = article_parent
parent.name if parent
end
protected
def require_category
if categories.count == 0 && flagged?
errors.add :categories, _('Select at least one category')
end
end
end