Commit 490cc71588ae9bd7291bb552ebddebe03e7e6af0

Authored by Michel Felipe
1 parent 5666676a

Refactory to restore finished proposal tasks, disabling associated proposal article

db/migrate/20160204200628_add_article_ref_to_tasks.rb 0 → 100644
@@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
  1 +class AddArticleRefToTasks < ActiveRecord::Migration
  2 +
  3 + def up
  4 + add_reference :tasks, :article, index: true, foreign_key: true, on_delete: :nullify
  5 + ProposalsDiscussionPlugin::ProposalTask.find_each do |task|
  6 +
  7 + if task.data[:article]
  8 + field = {name: task.data[:article][:name]}
  9 + if task.data[:article][:id]
  10 + field = {id: task.data[:article][:id]}
  11 + end
  12 + article = Article.find_by field
  13 +
  14 + if article.nil?
  15 + data = task.data[:article].merge({profile: task.target}).except(:type)
  16 + article = Article.create!(data)
  17 + end
  18 + task.update_column(:article_id, article.id)
  19 + end
  20 +
  21 + end
  22 + end
  23 +
  24 + def down
  25 + remove_reference :tasks, :article, index: true, foreign_key: true
  26 + end
  27 +
  28 +end
lib/ext/article.rb
@@ -2,6 +2,8 @@ require_dependency &#39;article&#39; @@ -2,6 +2,8 @@ require_dependency &#39;article&#39;
2 2
3 class Article 3 class Article
4 4
  5 + has_one :task
  6 +
5 def ranking_position 7 def ranking_position
6 self.kind_of?(ProposalsDiscussionPlugin::Proposal) && self.ranking_item.present? ? self.ranking_item.position : nil 8 self.kind_of?(ProposalsDiscussionPlugin::Proposal) && self.ranking_item.present? ? self.ranking_item.position : nil
7 end 9 end
lib/ext/entities.rb
@@ -11,7 +11,7 @@ module Noosfero @@ -11,7 +11,7 @@ module Noosfero
11 end 11 end
12 end 12 end
13 13
14 - class RankingItem < Entity 14 + class RankingItem < Grape::Entity
15 root :proposals, :proposal 15 root :proposals, :proposal
16 expose :id, :position, :abstract, :body, :votes_for, :votes_against 16 expose :id, :position, :abstract, :body, :votes_for, :votes_against
17 expose :hits, :effective_support, :proposal_id, :created_at 17 expose :hits, :effective_support, :proposal_id, :created_at
lib/proposals_discussion_plugin/proposal_task.rb
@@ -7,6 +7,7 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task @@ -7,6 +7,7 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
7 association_foreign_key: :category_id 7 association_foreign_key: :category_id
8 8
9 has_one :proposal_evaluation 9 has_one :proposal_evaluation
  10 + belongs_to :article_obj, class_name: 'Article', foreign_key:'article_id'
10 11
11 validates_presence_of :requestor_id, :target_id 12 validates_presence_of :requestor_id, :target_id
12 validates_associated :article_object 13 validates_associated :article_object
@@ -20,7 +21,7 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task @@ -20,7 +21,7 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
20 settings_items :article, :type => Hash, :default => {} 21 settings_items :article, :type => Hash, :default => {}
21 settings_items :closing_statment, :article_parent_id 22 settings_items :closing_statment, :article_parent_id
22 23
23 - attr_accessible :requestor, :target, :article, :status, :end_date, :closed_by 24 + attr_accessible :requestor, :target, :article, :article_obj, :article_parent_id, :status, :end_date, :closed_by
24 25
25 26
26 scope :pending_evaluated, lambda { |profile, filter_type, filter_text| 27 scope :pending_evaluated, lambda { |profile, filter_type, filter_text|
@@ -131,18 +132,26 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task @@ -131,18 +132,26 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
131 end 132 end
132 133
133 def self.undo_flags(tasks, user) 134 def self.undo_flags(tasks, user)
  135 + self.disable_article(tasks)
  136 +
134 fields = "status = #{Task::Status::ACTIVE}, end_date = NULL, closed_by_id = #{user.id}" 137 fields = "status = #{Task::Status::ACTIVE}, end_date = NULL, closed_by_id = #{user.id}"
135 - conditions = "status = #{Status::FLAGGED_FOR_REPROVAL} OR status = #{Status::FLAGGED_FOR_APPROVAL}" 138 + conditions = "status != #{Task::Status::ACTIVE}"
136 139
137 result = self.where(conditions).update_all(fields, ["id IN (?)",tasks]) 140 result = self.where(conditions).update_all(fields, ["id IN (?)",tasks])
138 - result 141 +
139 end 142 end
140 143
141 def undo_flags(user) 144 def undo_flags(user)
142 if flagged? 145 if flagged?
  146 + if flagged_for_approval?
  147 + self.article_obj.published = false
  148 + self.article_obj.save!
  149 + end
  150 +
143 self.status = Task::Status::ACTIVE 151 self.status = Task::Status::ACTIVE
144 self.end_date = nil 152 self.end_date = nil
145 self.closed_by = user 153 self.closed_by = user
  154 +
146 self.save! 155 self.save!
147 end 156 end
148 end 157 end
@@ -155,6 +164,13 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task @@ -155,6 +164,13 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
155 requestor.name if requestor 164 requestor.name if requestor
156 end 165 end
157 166
  167 + def self.disable_article(tasks,id = nil)
  168 +
  169 + conditions = "tasks.status = #{Status::FLAGGED_FOR_APPROVAL} OR tasks.status = #{Task::Status::FINISHED}"
  170 + Article.joins(:task).where(conditions).update_all('published = false, published_at = NULL', ["tasks.id IN (?)",tasks])
  171 +
  172 + end
  173 +
158 def article_parent=(parent) 174 def article_parent=(parent)
159 @article_parent = parent 175 @article_parent = parent
160 end 176 end
@@ -164,12 +180,15 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task @@ -164,12 +180,15 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
164 end 180 end
165 181
166 def article_object 182 def article_object
167 - if @article_object.nil?  
168 - @article_object = article_type.new(article.merge(target.present? ? {:profile => target} : {}).except(:type))  
169 - @article_object.parent = article_parent  
170 - @article_object.author = requestor if requestor.present? 183 + if self.article_obj.nil?
  184 + self.article_obj = article_type.new(article.merge(target.present? ? {:profile => target} : {}).except(:type))
  185 + self.article_obj.parent = article_parent
  186 + self.article_obj.author = requestor if requestor.present?
  187 + else
  188 + self.article_obj.published = true
  189 + self.article_obj.published_at = Time.now
171 end 190 end
172 - @article_object 191 + self.article_obj
173 end 192 end
174 193
175 def article_type 194 def article_type
@@ -181,8 +200,9 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task @@ -181,8 +200,9 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
181 end 200 end
182 201
183 def perform 202 def perform
184 - article_object.save!  
185 - self.data[:article][:id] = article_object[:id] 203 + self.article_obj = article_object
  204 + self.article_obj.save!
  205 + self.data[:article][:id] = self.article_obj.id
186 self.save! 206 self.save!
187 end 207 end
188 208
test/unit/proposal_task_test.rb
@@ -89,17 +89,21 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase @@ -89,17 +89,21 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase
89 task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal 1', :abstract => 'proposal 1'}) 89 task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal 1', :abstract => 'proposal 1'})
90 task_two = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal 2', :abstract => 'proposal 2'}) 90 task_two = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal 2', :abstract => 'proposal 2'})
91 task.categories = task_two.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)] 91 task.categories = task_two.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)]
92 - task.flag_reject_proposal(person2)  
93 - task_two.flag_reject_proposal(person2) 92 + task.flag_accept_proposal(person2)
  93 + task_two.flag_accept_proposal(person2)
94 assert task.flagged? 94 assert task.flagged?
95 assert task_two.flagged? 95 assert task_two.flagged?
96 96
  97 + task.perform
  98 + task_two.perform
  99 +
97 ProposalsDiscussionPlugin::ProposalTask.undo_flags([task.id,task_two.id], person) 100 ProposalsDiscussionPlugin::ProposalTask.undo_flags([task.id,task_two.id], person)
98 task.reload 101 task.reload
99 task_two.reload 102 task_two.reload
100 103
101 assert_equal [false,false], [task.flagged?,task_two.flagged?] 104 assert_equal [false,false], [task.flagged?,task_two.flagged?]
102 assert_equal [Task::Status::ACTIVE,Task::Status::ACTIVE], [task.status, task_two.status] 105 assert_equal [Task::Status::ACTIVE,Task::Status::ACTIVE], [task.status, task_two.status]
  106 + assert_equal [false,false], [task.article_obj.published,task_two.article_obj.published]
103 end 107 end
104 108
105 should 'undo flags from a specific proposal task' do 109 should 'undo flags from a specific proposal task' do
@@ -113,14 +117,47 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase @@ -113,14 +117,47 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase
113 117
114 task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal', :abstract => 'proposal'}) 118 task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal', :abstract => 'proposal'})
115 task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)] 119 task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)]
116 - task.flag_reject_proposal(person1) 120 + task.flag_accept_proposal(person1)
  121 + assert task.flagged?
  122 +
  123 + task.perform
  124 +
  125 + task.undo_flags(person)
  126 + task.reload
  127 +
  128 + assert_equal false, task.flagged?
  129 + assert_equal Task::Status::ACTIVE, task.status
  130 + assert_equal false, task.article_obj.published
  131 +
  132 + end
  133 +
  134 + should 'redo flags from a specific proposal task' do
  135 + role1 = Role.create!(:name => 'profile_role2', :permissions => ['perform_task'], :environment => Environment.default)
  136 + role2 = Role.create!(:name => 'profile_role', :permissions => ['view_tasks'], :environment => Environment.default)
  137 +
  138 + person1 = fast_create(Person)
  139 + person1.define_roles([role1], profile)
  140 + person2 = fast_create(Person)
  141 + person2.define_roles([role2], profile)
  142 +
  143 + task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal', :abstract => 'proposal'})
  144 + task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)]
  145 + task.flag_accept_proposal(person1)
117 assert task.flagged? 146 assert task.flagged?
118 147
  148 + task.perform
  149 +
119 task.undo_flags(person) 150 task.undo_flags(person)
120 task.reload 151 task.reload
121 152
122 assert_equal false, task.flagged? 153 assert_equal false, task.flagged?
123 assert_equal Task::Status::ACTIVE, task.status 154 assert_equal Task::Status::ACTIVE, task.status
  155 + assert_equal false, task.article_obj.published
  156 +
  157 + task.flag_accept_proposal(person1)
  158 + task.perform
  159 +
  160 + assert_equal true, task.article_obj.published
124 end 161 end
125 162
126 should 'do not fail on task information with integer as abstract' do 163 should 'do not fail on task information with integer as abstract' do
@@ -132,9 +169,10 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase @@ -132,9 +169,10 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase
132 should 'create a proposal with category when accept a task' do 169 should 'create a proposal with category when accept a task' do
133 c1 = fast_create(Category) 170 c1 = fast_create(Category)
134 discussion.categories << c1 171 discussion.categories << c1
135 - task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article_parent_id => @discussion.id, :article => {:name => 'proposal 1', :abstract => 'proposal 1', :type => "ProposalsDiscussionPlugin::Proposal"}) 172 +
  173 + task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article_parent_id => @discussion.id, :article => {:name => 'proposal 1', :abstract => 'proposal 1', :type => 'ProposalsDiscussionPlugin::Proposal'})
136 task.perform 174 task.perform
137 - assert_equal [c1], ProposalsDiscussionPlugin::Proposal.last.categories 175 + assert_equal c1, ProposalsDiscussionPlugin::Proposal.last.categories.first
138 end 176 end
139 177
140 end 178 end
test/unit/task_category_test.rb
@@ -16,8 +16,7 @@ class TaskCategoryTest &lt; ActiveSupport::TestCase @@ -16,8 +16,7 @@ class TaskCategoryTest &lt; ActiveSupport::TestCase
16 task_data = { 16 task_data = {
17 article: {name: "test proposal", abstract: "teste adadd"}, 17 article: {name: "test proposal", abstract: "teste adadd"},
18 requestor: person, 18 requestor: person,
19 - target: profile,  
20 - spam: false 19 + target: profile
21 } 20 }
22 21
23 task = ProposalsDiscussionPlugin::ProposalTask.new task_data 22 task = ProposalsDiscussionPlugin::ProposalTask.new task_data
@@ -35,8 +34,7 @@ class TaskCategoryTest &lt; ActiveSupport::TestCase @@ -35,8 +34,7 @@ class TaskCategoryTest &lt; ActiveSupport::TestCase
35 task_data = { 34 task_data = {
36 article: {name: "test proposal", abstract: "teste adadd"}, 35 article: {name: "test proposal", abstract: "teste adadd"},
37 requestor: person, 36 requestor: person,
38 - target: profile,  
39 - spam: false 37 + target: profile
40 } 38 }
41 39
42 task = ProposalsDiscussionPlugin::ProposalTask.create! task_data 40 task = ProposalsDiscussionPlugin::ProposalTask.create! task_data
@@ -51,8 +49,7 @@ class TaskCategoryTest &lt; ActiveSupport::TestCase @@ -51,8 +49,7 @@ class TaskCategoryTest &lt; ActiveSupport::TestCase
51 task_data = { 49 task_data = {
52 article: {name: "test proposal", abstract: "teste adadd"}, 50 article: {name: "test proposal", abstract: "teste adadd"},
53 requestor: person, 51 requestor: person,
54 - target: profile,  
55 - spam: false 52 + target: profile
56 } 53 }
57 task = ProposalsDiscussionPlugin::ProposalTask.create! task_data 54 task = ProposalsDiscussionPlugin::ProposalTask.create! task_data
58 evaluated_by = false 55 evaluated_by = false