Commit 7b7b83f2c5f8e4f7e6cf6407b67b5467bd6f98e3

Authored by Cyril Mougel
1 parent cb4eff56
Exists in master and in 1 other branch production

create an interactor to destroy a problem

app/interactors/problem_destroy.rb 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +class ProblemDestroy
  2 +
  3 + attr_reader :problem
  4 +
  5 + def initialize(problem)
  6 + @problem = problem
  7 + end
  8 +
  9 + def execute
  10 + delete_errs
  11 + delete_comments
  12 + problem.delete
  13 + end
  14 +
  15 + private
  16 +
  17 + def errs_id
  18 + problem.errs.only(:id).map(&:id)
  19 + end
  20 +
  21 + def comments_id
  22 + problem.comments.only(:id).map(&:id)
  23 + end
  24 +
  25 + def delete_errs
  26 + Err.collection.remove(:_id => { '$in' => errs_id })
  27 + Notice.collection.remove(:err_id => { '$in' => errs_id })
  28 + end
  29 +
  30 + def delete_comments
  31 + Comment.collection.remove(:_id => { '$in' => comments_id })
  32 + end
  33 +
  34 +end
... ...
spec/interactors/problem_destroy_spec.rb 0 → 100644
... ... @@ -0,0 +1,77 @@
  1 +require 'spec_helper'
  2 +
  3 +describe ProblemDestroy do
  4 + let(:problem_destroy) {
  5 + ProblemDestroy.new(problem)
  6 + }
  7 +
  8 + context "in unit way" do
  9 + let(:problem) {
  10 + problem = Problem.new()
  11 + problem.stub(:errs).and_return(mock(:criteria, :only => [err_1, err_2]))
  12 + problem.stub(:comments).and_return(mock(:criteria, :only => [comment_1, comment_2]))
  13 + problem.stub(:delete)
  14 + problem
  15 + }
  16 + let(:err_1) { Err.new }
  17 + let(:err_2) { Err.new }
  18 +
  19 + let(:comment_1) { Comment.new }
  20 + let(:comment_2) { Comment.new }
  21 +
  22 + describe "#initialize" do
  23 + it 'take a problem like args' do
  24 + problem_destroy.problem.should == problem
  25 + end
  26 + end
  27 +
  28 + describe "#execute" do
  29 + it 'destroy the problem himself' do
  30 + problem.should_receive(:delete).and_return(true)
  31 + problem_destroy.execute
  32 + end
  33 +
  34 + it 'delete all errs associate' do
  35 + Err.collection.should_receive(:remove).with(:_id => { '$in' => [err_1.id, err_2.id] })
  36 + problem_destroy.execute
  37 + end
  38 +
  39 + it 'delete all comments associate' do
  40 + Comment.collection.should_receive(:remove).with(:_id => { '$in' => [comment_1.id, comment_2.id] })
  41 + problem_destroy.execute
  42 + end
  43 +
  44 + it 'delete all notice of associate to this errs' do
  45 + Notice.collection.should_receive(:remove).with({:err_id => { '$in' => [err_1.id, err_2.id] }})
  46 + problem_destroy.execute
  47 + end
  48 + end
  49 +
  50 + end
  51 +
  52 + context "in integration way" do
  53 + let!(:problem) { Fabricate(:problem) }
  54 + let!(:comment_1) { Fabricate(:comment, :err => problem) }
  55 + let!(:comment_2) { Fabricate(:comment, :err => problem) }
  56 + let!(:err_1) { Fabricate(:err, :problem => problem) }
  57 + let!(:err_2) { Fabricate(:err, :problem => problem) }
  58 + let!(:notice_1_1) { Fabricate(:notice, :err => err_1) }
  59 + let!(:notice_1_2) { Fabricate(:notice, :err => err_1) }
  60 + let!(:notice_2_1) { Fabricate(:notice, :err => err_2) }
  61 + let!(:notice_2_2) { Fabricate(:notice, :err => err_2) }
  62 +
  63 + it 'should all destroy' do
  64 + problem_destroy.execute
  65 + Problem.where(:_id => problem.id).entries.should be_empty
  66 + Err.where(:_id => err_1.id).entries.should be_empty
  67 + Err.where(:_id => err_2.id).entries.should be_empty
  68 + Comment.where(:_id => comment_1.id).entries.should be_empty
  69 + Comment.where(:_id => comment_2.id).entries.should be_empty
  70 + Notice.where(:_id => notice_1_1.id).entries.should be_empty
  71 + Notice.where(:_id => notice_1_2.id).entries.should be_empty
  72 + Notice.where(:_id => notice_2_1.id).entries.should be_empty
  73 + Notice.where(:_id => notice_2_2.id).entries.should be_empty
  74 + end
  75 + end
  76 +
  77 +end
... ...