Commit bb88fa4d252fa60652efaa7e70e483dd17fff3a9

Authored by Michel Felipe
1 parent e6e26d29

Added migration and fix test units to task categories db structure

db/migrate/20150702195735_create_tasks_categories.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +class CreateTasksCategories < ActiveRecord::Migration
  2 + def up
  3 + create_table :proposals_discussion_plugin_task_categories, id: false do |t|
  4 + t.belongs_to :task, index: true
  5 + t.belongs_to :category, index: true
  6 +
  7 + end
  8 + end
  9 +
  10 + def down
  11 + drop_table :proposals_discussion_plugin_task_categories
  12 + end
  13 +end
... ...
lib/proposals_discussion_plugin/proposal_task.rb
1 1 class ProposalsDiscussionPlugin::ProposalTask < Task
2 2  
3   - has_and_belongs_to_many :proposals_discussion_plugin_task_categories
  3 + has_and_belongs_to_many :categories,
  4 + class_name: "ProposalsDiscussionPlugin::TaskCategory",
  5 + join_table: :proposals_discussion_plugin_task_categories,
  6 + foreign_key: :task_id,
  7 + association_foreign_key: :category_id
4 8  
5 9 validates_presence_of :requestor_id, :target_id
6 10 validates_associated :article_object
7 11  
  12 + validate :require_category
  13 +
8 14 settings_items :name, :type => String
9 15 settings_items :ip_address, :type => String
10 16 settings_items :user_agent, :type => String
... ... @@ -278,4 +284,12 @@ class ProposalsDiscussionPlugin::ProposalTask &lt; Task
278 284 def after_ham!
279 285 self.delay.marked_as_ham
280 286 end
  287 +
  288 + protected
  289 +
  290 + def require_category
  291 + if categories.count == 0 && flagged?
  292 + errors.add :categories, _( 'Please, select at least one')
  293 + end
  294 + end
281 295 end
... ...
lib/proposals_discussion_plugin/task_category.rb
1 1 class ProposalsDiscussionPlugin::TaskCategory < Category
2 2  
3   - has_and_belongs_to_many :tasks
  3 + has_and_belongs_to_many :tasks, join_table: :proposals_discussion_plugin_task_categories, foreign_key: :category_id
4 4 end
... ...
test/unit/task_category_test.rb
1 1 require_relative '../test_helper'
2 2  
3   -class TaskCategory < ActiveSupport::TestCase
  3 +class TaskCategoryTest < ActiveSupport::TestCase
4 4  
5 5 def setup
6 6 @person = fast_create(Person)
... ... @@ -12,18 +12,65 @@ class TaskCategory &lt; ActiveSupport::TestCase
12 12 attr_reader :person, :profile, :discussion, :proposal
13 13  
14 14 should 'add a category to a task' do
15   - requestor = fast_create(Person)
  15 +
16 16 task_data = {
17   - :requestor => requestor,
18   - :target => person,
19   - :spam => false
  17 + article: {name: "test proposal", abstract: "teste adadd"},
  18 + requestor: person,
  19 + target: profile,
  20 + spam: false
20 21 }
21 22  
22   - task = Task.create!(task_data)
  23 + task = ProposalsDiscussionPlugin::ProposalTask.new task_data
  24 +
  25 + category = ProposalsDiscussionPlugin::TaskCategory.create!(name: 'ProposalTest', environment: Environment.default)
23 26  
24   - category = ProposalsDiscussionPlugin::TaskCategory.create!(name: 'ProposalTest', tasks: [task])
  27 + category.tasks << task;
  28 + category.save!
25 29  
26 30 assert_equal task.id, category.tasks[0].id
27 31 end
28 32  
  33 + should 'approve a proposal task without category' do
  34 +
  35 + task_data = {
  36 + article: {name: "test proposal", abstract: "teste adadd"},
  37 + requestor: person,
  38 + target: profile,
  39 + spam: false
  40 + }
  41 +
  42 + task = ProposalsDiscussionPlugin::ProposalTask.create! task_data
  43 + evaluated_by = false
  44 +
  45 + assert_raise(ActiveRecord::RecordInvalid) { task.flag_accept_proposal evaluated_by }
  46 +
  47 + end
  48 +
  49 + should 'approve a proposal task with categories' do
  50 +
  51 + task_data = {
  52 + article: {name: "test proposal", abstract: "teste adadd"},
  53 + requestor: person,
  54 + target: profile,
  55 + spam: false
  56 + }
  57 + task = ProposalsDiscussionPlugin::ProposalTask.create! task_data
  58 + evaluated_by = false
  59 +
  60 + categories = [
  61 + { name: 'ProposalTest', environment: Environment.default },
  62 + { name: 'ProposalTestTwo', environment: Environment.default }
  63 + ]
  64 + categories.each do |category|
  65 + task.categories << ProposalsDiscussionPlugin::TaskCategory.new(category)
  66 + end
  67 +
  68 + assert_nothing_raised do
  69 + task.flag_accept_proposal evaluated_by
  70 + end
  71 +
  72 + assert_equal 2, task.categories.count
  73 +
  74 + end
  75 +
29 76 end
... ...