diff --git a/db/migrate/20160204200628_add_article_ref_to_tasks.rb b/db/migrate/20160204200628_add_article_ref_to_tasks.rb new file mode 100644 index 0000000..a946ff0 --- /dev/null +++ b/db/migrate/20160204200628_add_article_ref_to_tasks.rb @@ -0,0 +1,28 @@ +class AddArticleRefToTasks < ActiveRecord::Migration + + def up + add_reference :tasks, :article, index: true, foreign_key: true, on_delete: :nullify + ProposalsDiscussionPlugin::ProposalTask.find_each do |task| + + if task.data[:article] + field = {name: task.data[:article][:name]} + if task.data[:article][:id] + field = {id: task.data[:article][:id]} + end + article = Article.find_by field + + if article.nil? + data = task.data[:article].merge({profile: task.target}).except(:type) + article = Article.create!(data) + end + task.update_column(:article_id, article.id) + end + + end + end + + def down + remove_reference :tasks, :article, index: true, foreign_key: true + end + +end diff --git a/lib/ext/article.rb b/lib/ext/article.rb index 42af826..3c03245 100644 --- a/lib/ext/article.rb +++ b/lib/ext/article.rb @@ -2,6 +2,8 @@ require_dependency 'article' class Article + has_one :task + def ranking_position self.kind_of?(ProposalsDiscussionPlugin::Proposal) && self.ranking_item.present? ? self.ranking_item.position : nil end diff --git a/lib/ext/entities.rb b/lib/ext/entities.rb index 9d430a1..c3fe3b7 100644 --- a/lib/ext/entities.rb +++ b/lib/ext/entities.rb @@ -11,7 +11,7 @@ module Noosfero end end - class RankingItem < Entity + class RankingItem < Grape::Entity root :proposals, :proposal expose :id, :position, :abstract, :body, :votes_for, :votes_against expose :hits, :effective_support, :proposal_id, :created_at diff --git a/lib/proposals_discussion_plugin/proposal_task.rb b/lib/proposals_discussion_plugin/proposal_task.rb index 28bb9a9..10bd63b 100644 --- a/lib/proposals_discussion_plugin/proposal_task.rb +++ b/lib/proposals_discussion_plugin/proposal_task.rb @@ -7,6 +7,7 @@ class ProposalsDiscussionPlugin::ProposalTask < Task association_foreign_key: :category_id has_one :proposal_evaluation + belongs_to :article_obj, class_name: 'Article', foreign_key:'article_id' validates_presence_of :requestor_id, :target_id validates_associated :article_object @@ -20,7 +21,7 @@ class ProposalsDiscussionPlugin::ProposalTask < Task settings_items :article, :type => Hash, :default => {} settings_items :closing_statment, :article_parent_id - attr_accessible :requestor, :target, :article, :status, :end_date, :closed_by + attr_accessible :requestor, :target, :article, :article_obj, :article_parent_id, :status, :end_date, :closed_by scope :pending_evaluated, lambda { |profile, filter_type, filter_text| @@ -131,18 +132,26 @@ class ProposalsDiscussionPlugin::ProposalTask < Task end def self.undo_flags(tasks, user) + self.disable_article(tasks) + fields = "status = #{Task::Status::ACTIVE}, end_date = NULL, closed_by_id = #{user.id}" - conditions = "status = #{Status::FLAGGED_FOR_REPROVAL} OR status = #{Status::FLAGGED_FOR_APPROVAL}" + conditions = "status != #{Task::Status::ACTIVE}" result = self.where(conditions).update_all(fields, ["id IN (?)",tasks]) - result + end def undo_flags(user) if flagged? + if flagged_for_approval? + self.article_obj.published = false + self.article_obj.save! + end + self.status = Task::Status::ACTIVE self.end_date = nil self.closed_by = user + self.save! end end @@ -155,6 +164,13 @@ class ProposalsDiscussionPlugin::ProposalTask < Task requestor.name if requestor end + def self.disable_article(tasks,id = nil) + + conditions = "tasks.status = #{Status::FLAGGED_FOR_APPROVAL} OR tasks.status = #{Task::Status::FINISHED}" + Article.joins(:task).where(conditions).update_all('published = false, published_at = NULL', ["tasks.id IN (?)",tasks]) + + end + def article_parent=(parent) @article_parent = parent end @@ -164,12 +180,15 @@ class ProposalsDiscussionPlugin::ProposalTask < Task 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? + if self.article_obj.nil? + self.article_obj = article_type.new(article.merge(target.present? ? {:profile => target} : {}).except(:type)) + self.article_obj.parent = article_parent + self.article_obj.author = requestor if requestor.present? + else + self.article_obj.published = true + self.article_obj.published_at = Time.now end - @article_object + self.article_obj end def article_type @@ -181,8 +200,9 @@ class ProposalsDiscussionPlugin::ProposalTask < Task end def perform - article_object.save! - self.data[:article][:id] = article_object[:id] + self.article_obj = article_object + self.article_obj.save! + self.data[:article][:id] = self.article_obj.id self.save! end diff --git a/test/unit/proposal_task_test.rb b/test/unit/proposal_task_test.rb index 480b3d3..ab4822f 100644 --- a/test/unit/proposal_task_test.rb +++ b/test/unit/proposal_task_test.rb @@ -89,17 +89,21 @@ class ProposalTaskTest < ActiveSupport::TestCase task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal 1', :abstract => 'proposal 1'}) task_two = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal 2', :abstract => 'proposal 2'}) task.categories = task_two.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)] - task.flag_reject_proposal(person2) - task_two.flag_reject_proposal(person2) + task.flag_accept_proposal(person2) + task_two.flag_accept_proposal(person2) assert task.flagged? assert task_two.flagged? + task.perform + task_two.perform + ProposalsDiscussionPlugin::ProposalTask.undo_flags([task.id,task_two.id], person) task.reload task_two.reload assert_equal [false,false], [task.flagged?,task_two.flagged?] assert_equal [Task::Status::ACTIVE,Task::Status::ACTIVE], [task.status, task_two.status] + assert_equal [false,false], [task.article_obj.published,task_two.article_obj.published] end should 'undo flags from a specific proposal task' do @@ -113,14 +117,47 @@ class ProposalTaskTest < ActiveSupport::TestCase task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal', :abstract => 'proposal'}) task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)] - task.flag_reject_proposal(person1) + task.flag_accept_proposal(person1) + assert task.flagged? + + task.perform + + task.undo_flags(person) + task.reload + + assert_equal false, task.flagged? + assert_equal Task::Status::ACTIVE, task.status + assert_equal false, task.article_obj.published + + end + + should 'redo flags from a specific proposal task' do + role1 = Role.create!(:name => 'profile_role2', :permissions => ['perform_task'], :environment => Environment.default) + role2 = Role.create!(:name => 'profile_role', :permissions => ['view_tasks'], :environment => Environment.default) + + person1 = fast_create(Person) + person1.define_roles([role1], profile) + person2 = fast_create(Person) + person2.define_roles([role2], profile) + + task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article => {:name => 'proposal', :abstract => 'proposal'}) + task.categories = [fast_create(ProposalsDiscussionPlugin::TaskCategory)] + task.flag_accept_proposal(person1) assert task.flagged? + task.perform + task.undo_flags(person) task.reload assert_equal false, task.flagged? assert_equal Task::Status::ACTIVE, task.status + assert_equal false, task.article_obj.published + + task.flag_accept_proposal(person1) + task.perform + + assert_equal true, task.article_obj.published end should 'do not fail on task information with integer as abstract' do @@ -132,9 +169,10 @@ class ProposalTaskTest < ActiveSupport::TestCase should 'create a proposal with category when accept a task' do c1 = fast_create(Category) discussion.categories << c1 - task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article_parent_id => @discussion.id, :article => {:name => 'proposal 1', :abstract => 'proposal 1', :type => "ProposalsDiscussionPlugin::Proposal"}) + + task = ProposalsDiscussionPlugin::ProposalTask.create!(:requestor => person, :target => profile, :article_parent_id => @discussion.id, :article => {:name => 'proposal 1', :abstract => 'proposal 1', :type => 'ProposalsDiscussionPlugin::Proposal'}) task.perform - assert_equal [c1], ProposalsDiscussionPlugin::Proposal.last.categories + assert_equal c1, ProposalsDiscussionPlugin::Proposal.last.categories.first end end diff --git a/test/unit/task_category_test.rb b/test/unit/task_category_test.rb index 3cf81d4..0cd94c2 100644 --- a/test/unit/task_category_test.rb +++ b/test/unit/task_category_test.rb @@ -16,8 +16,7 @@ class TaskCategoryTest < ActiveSupport::TestCase task_data = { article: {name: "test proposal", abstract: "teste adadd"}, requestor: person, - target: profile, - spam: false + target: profile } task = ProposalsDiscussionPlugin::ProposalTask.new task_data @@ -35,8 +34,7 @@ class TaskCategoryTest < ActiveSupport::TestCase task_data = { article: {name: "test proposal", abstract: "teste adadd"}, requestor: person, - target: profile, - spam: false + target: profile } task = ProposalsDiscussionPlugin::ProposalTask.create! task_data @@ -51,8 +49,7 @@ class TaskCategoryTest < ActiveSupport::TestCase task_data = { article: {name: "test proposal", abstract: "teste adadd"}, requestor: person, - target: profile, - spam: false + target: profile } task = ProposalsDiscussionPlugin::ProposalTask.create! task_data evaluated_by = false -- libgit2 0.21.2