diff --git a/db/migrate/20150702195735_create_tasks_categories.rb b/db/migrate/20150702195735_create_tasks_categories.rb new file mode 100644 index 0000000..1e6ae9c --- /dev/null +++ b/db/migrate/20150702195735_create_tasks_categories.rb @@ -0,0 +1,13 @@ +class CreateTasksCategories < ActiveRecord::Migration + def up + create_table :proposals_discussion_plugin_task_categories, id: false do |t| + t.belongs_to :task, index: true + t.belongs_to :category, index: true + + end + end + + def down + drop_table :proposals_discussion_plugin_task_categories + end +end diff --git a/lib/proposals_discussion_plugin/proposal_task.rb b/lib/proposals_discussion_plugin/proposal_task.rb index 2a620fa..76f5b9e 100644 --- a/lib/proposals_discussion_plugin/proposal_task.rb +++ b/lib/proposals_discussion_plugin/proposal_task.rb @@ -1,10 +1,16 @@ class ProposalsDiscussionPlugin::ProposalTask < Task - has_and_belongs_to_many :proposals_discussion_plugin_task_categories + 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 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 @@ -278,4 +284,12 @@ class ProposalsDiscussionPlugin::ProposalTask < Task def after_ham! self.delay.marked_as_ham end + + protected + + def require_category + if categories.count == 0 && flagged? + errors.add :categories, _( 'Please, select at least one') + end + end end diff --git a/lib/proposals_discussion_plugin/task_category.rb b/lib/proposals_discussion_plugin/task_category.rb index b3a0880..d57faaf 100644 --- a/lib/proposals_discussion_plugin/task_category.rb +++ b/lib/proposals_discussion_plugin/task_category.rb @@ -1,4 +1,4 @@ class ProposalsDiscussionPlugin::TaskCategory < Category - has_and_belongs_to_many :tasks + has_and_belongs_to_many :tasks, join_table: :proposals_discussion_plugin_task_categories, foreign_key: :category_id end diff --git a/test/unit/task_category_test.rb b/test/unit/task_category_test.rb index 7ab16b3..3cf81d4 100644 --- a/test/unit/task_category_test.rb +++ b/test/unit/task_category_test.rb @@ -1,6 +1,6 @@ require_relative '../test_helper' -class TaskCategory < ActiveSupport::TestCase +class TaskCategoryTest < ActiveSupport::TestCase def setup @person = fast_create(Person) @@ -12,18 +12,65 @@ class TaskCategory < ActiveSupport::TestCase attr_reader :person, :profile, :discussion, :proposal should 'add a category to a task' do - requestor = fast_create(Person) + task_data = { - :requestor => requestor, - :target => person, - :spam => false + article: {name: "test proposal", abstract: "teste adadd"}, + requestor: person, + target: profile, + spam: false } - task = Task.create!(task_data) + task = ProposalsDiscussionPlugin::ProposalTask.new task_data + + category = ProposalsDiscussionPlugin::TaskCategory.create!(name: 'ProposalTest', environment: Environment.default) - category = ProposalsDiscussionPlugin::TaskCategory.create!(name: 'ProposalTest', tasks: [task]) + category.tasks << task; + category.save! assert_equal task.id, category.tasks[0].id end + should 'approve a proposal task without category' do + + task_data = { + article: {name: "test proposal", abstract: "teste adadd"}, + requestor: person, + target: profile, + spam: false + } + + task = ProposalsDiscussionPlugin::ProposalTask.create! task_data + evaluated_by = false + + assert_raise(ActiveRecord::RecordInvalid) { task.flag_accept_proposal evaluated_by } + + end + + should 'approve a proposal task with categories' do + + task_data = { + article: {name: "test proposal", abstract: "teste adadd"}, + requestor: person, + target: profile, + spam: false + } + task = ProposalsDiscussionPlugin::ProposalTask.create! task_data + evaluated_by = false + + categories = [ + { name: 'ProposalTest', environment: Environment.default }, + { name: 'ProposalTestTwo', environment: Environment.default } + ] + categories.each do |category| + task.categories << ProposalsDiscussionPlugin::TaskCategory.new(category) + end + + assert_nothing_raised do + task.flag_accept_proposal evaluated_by + end + + assert_equal 2, task.categories.count + + end + end -- libgit2 0.21.2