Commit fe4e38a11c8f6fd42225c9e71586d67eed5386cc

Authored by Marcin Ciunelis
2 parents f2611648 4624c4a8
Exists in master and in 1 other branch production

Merge pull request #209 from shingara/feature/improve_destroy_problem

Feature/improve destroy problem
app/controllers/errs_controller.rb
... ... @@ -114,8 +114,8 @@ class ErrsController < ApplicationController
114 114 end
115 115  
116 116 def destroy_several
117   - @selected_problems.each(&:destroy)
118   - flash[:notice] = "#{pluralize(@selected_problems.count, 'err has', 'errs have')} been deleted."
  117 + nb_problem_destroy = ProblemDestroy.execute(@selected_problems)
  118 + flash[:notice] = "#{pluralize(nb_problem_destroy, 'err has', 'errs have')} been deleted."
119 119 redirect_to :back
120 120 end
121 121  
... ...
app/interactors/problem_destroy.rb 0 → 100644
... ... @@ -0,0 +1,48 @@
  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 + ##
  16 + # Destroy all problem pass in args
  17 + #
  18 + # @params [ Array[Problem] ] problems the list of problem need to be delete
  19 + # can be a single Problem
  20 + # @return [ Integer ]
  21 + # the number of problem destroy
  22 + #
  23 + def self.execute(problems)
  24 + Array(problems).each{ |problem|
  25 + ProblemDestroy.new(problem).execute
  26 + }.count
  27 + end
  28 +
  29 + private
  30 +
  31 + def errs_id
  32 + problem.errs.only(:id).map(&:id)
  33 + end
  34 +
  35 + def comments_id
  36 + problem.comments.only(:id).map(&:id)
  37 + end
  38 +
  39 + def delete_errs
  40 + Err.collection.remove(:_id => { '$in' => errs_id })
  41 + Notice.collection.remove(:err_id => { '$in' => errs_id })
  42 + end
  43 +
  44 + def delete_comments
  45 + Comment.collection.remove(:_id => { '$in' => comments_id })
  46 + end
  47 +
  48 +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
... ...