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 @@
  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 2  
3 3 class Article
4 4  
  5 + has_one :task
  6 +
5 7 def ranking_position
6 8 self.kind_of?(ProposalsDiscussionPlugin::Proposal) && self.ranking_item.present? ? self.ranking_item.position : nil
7 9 end
... ...
lib/ext/entities.rb
... ... @@ -11,7 +11,7 @@ module Noosfero
11 11 end
12 12 end
13 13  
14   - class RankingItem < Entity
  14 + class RankingItem < Grape::Entity
15 15 root :proposals, :proposal
16 16 expose :id, :position, :abstract, :body, :votes_for, :votes_against
17 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 7 association_foreign_key: :category_id
8 8  
9 9 has_one :proposal_evaluation
  10 + belongs_to :article_obj, class_name: 'Article', foreign_key:'article_id'
10 11  
11 12 validates_presence_of :requestor_id, :target_id
12 13 validates_associated :article_object
... ... @@ -20,7 +21,7 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
20 21 settings_items :article, :type => Hash, :default => {}
21 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 27 scope :pending_evaluated, lambda { |profile, filter_type, filter_text|
... ... @@ -131,18 +132,26 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
131 132 end
132 133  
133 134 def self.undo_flags(tasks, user)
  135 + self.disable_article(tasks)
  136 +
134 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 140 result = self.where(conditions).update_all(fields, ["id IN (?)",tasks])
138   - result
  141 +
139 142 end
140 143  
141 144 def undo_flags(user)
142 145 if flagged?
  146 + if flagged_for_approval?
  147 + self.article_obj.published = false
  148 + self.article_obj.save!
  149 + end
  150 +
143 151 self.status = Task::Status::ACTIVE
144 152 self.end_date = nil
145 153 self.closed_by = user
  154 +
146 155 self.save!
147 156 end
148 157 end
... ... @@ -155,6 +164,13 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
155 164 requestor.name if requestor
156 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 174 def article_parent=(parent)
159 175 @article_parent = parent
160 176 end
... ... @@ -164,12 +180,15 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
164 180 end
165 181  
166 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 190 end
172   - @article_object
  191 + self.article_obj
173 192 end
174 193  
175 194 def article_type
... ... @@ -181,8 +200,9 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
181 200 end
182 201  
183 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 206 self.save!
187 207 end
188 208  
... ...
test/unit/proposal_task_test.rb
... ... @@ -89,17 +89,21 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase
89 89 task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal 1', :abstract => 'proposal 1'})
90 90 task_two = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal 2', :abstract => 'proposal 2'})
91 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 94 assert task.flagged?
95 95 assert task_two.flagged?
96 96  
  97 + task.perform
  98 + task_two.perform
  99 +
97 100 ProposalsDiscussionPlugin::ProposalTask.undo_flags([task.id,task_two.id], person)
98 101 task.reload
99 102 task_two.reload
100 103  
101 104 assert_equal [false,false], [task.flagged?,task_two.flagged?]
102 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 107 end
104 108  
105 109 should 'undo flags from a specific proposal task' do
... ... @@ -113,14 +117,47 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase
113 117  
114 118 task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal', :abstract => 'proposal'})
115 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 146 assert task.flagged?
118 147  
  148 + task.perform
  149 +
119 150 task.undo_flags(person)
120 151 task.reload
121 152  
122 153 assert_equal false, task.flagged?
123 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 161 end
125 162  
126 163 should 'do not fail on task information with integer as abstract' do
... ... @@ -132,9 +169,10 @@ class ProposalTaskTest &lt; ActiveSupport::TestCase
132 169 should 'create a proposal with category when accept a task' do
133 170 c1 = fast_create(Category)
134 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 174 task.perform
137   - assert_equal [c1], ProposalsDiscussionPlugin::Proposal.last.categories
  175 + assert_equal c1, ProposalsDiscussionPlugin::Proposal.last.categories.first
138 176 end
139 177  
140 178 end
... ...
test/unit/task_category_test.rb
... ... @@ -16,8 +16,7 @@ class TaskCategoryTest &lt; ActiveSupport::TestCase
16 16 task_data = {
17 17 article: {name: "test proposal", abstract: "teste adadd"},
18 18 requestor: person,
19   - target: profile,
20   - spam: false
  19 + target: profile
21 20 }
22 21  
23 22 task = ProposalsDiscussionPlugin::ProposalTask.new task_data
... ... @@ -35,8 +34,7 @@ class TaskCategoryTest &lt; ActiveSupport::TestCase
35 34 task_data = {
36 35 article: {name: "test proposal", abstract: "teste adadd"},
37 36 requestor: person,
38   - target: profile,
39   - spam: false
  37 + target: profile
40 38 }
41 39  
42 40 task = ProposalsDiscussionPlugin::ProposalTask.create! task_data
... ... @@ -51,8 +49,7 @@ class TaskCategoryTest &lt; ActiveSupport::TestCase
51 49 task_data = {
52 50 article: {name: "test proposal", abstract: "teste adadd"},
53 51 requestor: person,
54   - target: profile,
55   - spam: false
  52 + target: profile
56 53 }
57 54 task = ProposalsDiscussionPlugin::ProposalTask.create! task_data
58 55 evaluated_by = false
... ...