Commit 11b082045552d616dad01df7f7ee5a387b409863

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

Little refactoring of action in ProblemController

app/controllers/problems_controller.rb
... ... @@ -6,10 +6,13 @@
6 6 # COLLECTION => :index, :all, :destroy_several, :resolve_several, :unresolve_several, :merge_several, :unmerge_several, :search
7 7 class ProblemsController < ApplicationController
8 8  
9   - before_filter :find_selected_problems, :only => [:destroy_several, :resolve_several, :unresolve_several, :merge_several, :unmerge_several]
10 9 before_filter :set_sorting_params, :only => [:index, :all, :search]
11 10 before_filter :set_tracker_params, :only => [:create_issue]
12 11  
  12 + before_filter :need_selected_problem, :only => [
  13 + :resolve_several, :unresolve_several, :unmerge_several
  14 + ]
  15 +
13 16 expose(:app) {
14 17 if current_user.admin?
15 18 App.find(params[:app_id])
... ... @@ -17,15 +20,23 @@ class ProblemsController &lt; ApplicationController
17 20 current_user.apps.find(params[:app_id])
18 21 end
19 22 }
  23 +
20 24 expose(:problem) {
21 25 app.problems.find(params[:id])
22 26 }
23 27  
  28 + expose(:err_ids) {
  29 + (params[:problems] || []).compact
  30 + }
  31 +
  32 + expose(:selected_problems) {
  33 + Array(Problem.find(err_ids))
  34 + }
  35 +
24 36 def index
25 37 app_scope = current_user.admin? ? App.all : current_user.apps
26 38 @all_errs = params[:all_errs]
27 39 @problems = Problem.for_apps(app_scope).in_env(params[:environment]).all_else_unresolved(@all_errs).ordered_by(@sort, @order)
28   - @selected_problems = params[:problems] || []
29 40 respond_to do |format|
30 41 format.html do
31 42 @problems = @problems.page(params[:page]).per(current_user.per_page)
... ... @@ -64,35 +75,40 @@ class ProblemsController &lt; ApplicationController
64 75 end
65 76  
66 77 def resolve_several
67   - @selected_problems.each(&:resolve!)
68   - flash[:success] = "Great news everyone! #{I18n.t(:n_errs_have, :count => @selected_problems.count)} been resolved."
  78 + selected_problems.each(&:resolve!)
  79 + flash[:success] = "Great news everyone! #{I18n.t(:n_errs_have, :count => selected_problems.count)} been resolved."
69 80 redirect_to :back
70 81 end
71 82  
72 83 def unresolve_several
73   - @selected_problems.each(&:unresolve!)
74   - flash[:success] = "#{I18n.t(:n_errs_have, :count => @selected_problems.count)} been unresolved."
  84 + selected_problems.each(&:unresolve!)
  85 + flash[:success] = "#{I18n.t(:n_errs_have, :count => selected_problems.count)} been unresolved."
75 86 redirect_to :back
76 87 end
77 88  
  89 + ##
  90 + # Action to merge several Problem in One problem
  91 + #
  92 + # @param [ Array<String> ] :problems the list of problem ids
  93 + #
78 94 def merge_several
79   - if @selected_problems.length < 2
80   - flash[:notice] = "You must select at least two errors to merge"
  95 + if selected_problems.length < 2
  96 + flash[:notice] = I18n.t('controllers.problems.flash.need_two_errors_merge')
81 97 else
82   - @merged_problem = Problem.merge!(@selected_problems)
83   - flash[:notice] = "#{@selected_problems.count} errors have been merged."
  98 + ProblemMerge.new(selected_problems).merge
  99 + flash[:notice] = I18n.t('controllers.problems.flash.merge_several.success', :nb => selected_problems.count)
84 100 end
85 101 redirect_to :back
86 102 end
87 103  
88 104 def unmerge_several
89   - all = @selected_problems.map(&:unmerge!).flatten
  105 + all = selected_problems.map(&:unmerge!).flatten
90 106 flash[:success] = "#{I18n.t(:n_errs_have, :count => all.length)} been unmerged."
91 107 redirect_to :back
92 108 end
93 109  
94 110 def destroy_several
95   - nb_problem_destroy = ProblemDestroy.execute(@selected_problems)
  111 + nb_problem_destroy = ProblemDestroy.execute(selected_problems)
96 112 flash[:notice] = "#{I18n.t(:n_errs_have, :count => nb_problem_destroy)} been deleted."
97 113 redirect_to :back
98 114 end
... ... @@ -117,20 +133,20 @@ class ProblemsController &lt; ApplicationController
117 133 IssueTracker.default_url_options[:protocol] = request.scheme
118 134 end
119 135  
120   - def find_selected_problems
121   - err_ids = (params[:problems] || []).compact
122   - if err_ids.empty?
123   - flash[:notice] = "You have not selected any errors"
124   - redirect_to :back
125   - else
126   - @selected_problems = Array(Problem.find(err_ids))
127   - end
128   - end
129   -
130 136 def set_sorting_params
131 137 @sort = params[:sort]
132 138 @sort = "last_notice_at" unless %w{app message last_notice_at last_deploy_at count}.member?(@sort)
133 139 @order = params[:order] || "desc"
134 140 end
  141 +
  142 + ##
  143 + # Redirect :back if no errors selected
  144 + #
  145 + def need_selected_problem
  146 + if err_ids.empty?
  147 + flash[:notice] = I18n.t('controllers.problems.flash.no_select_problem')
  148 + redirect_to :back
  149 + end
  150 + end
135 151 end
136 152  
... ...
config/locales/en.yml
... ... @@ -2,7 +2,7 @@
2 2 # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3 3  
4 4 en:
5   - hello: "Hello world"
  5 +
6 6 flash:
7 7 apps:
8 8 create:
... ... @@ -11,6 +11,7 @@ en:
11 11 success: "Good news everyone! '%{app_name}' was successfully updated."
12 12 destroy:
13 13 success: "'%{app_name}' was successfully destroyed."
  14 +
14 15 n_errs_have:
15 16 one: "%{count} err has"
16 17 other: "%{count} errs have"
... ... @@ -38,6 +39,12 @@ en:
38 39 error: "You can't delete yourself"
39 40 update:
40 41 success: "%{name}'s information was successfully updated."
  42 + problems:
  43 + flash:
  44 + no_select_problem: "You have not selected any errors"
  45 + need_two_errors_merge: "You must select at least two errors to merge"
  46 + merge_several:
  47 + success: "%{nb} errors have been merged."
41 48  
42 49 devise:
43 50 registrations:
... ...
spec/controllers/problems_controller_spec.rb
... ... @@ -341,31 +341,25 @@ describe ProblemsController do
341 341 @problem2 = Fabricate(:err, :problem => Fabricate(:problem, :resolved => false)).problem
342 342 end
343 343  
344   - it "should apply to multiple problems" do
345   - post :resolve_several, :problems => [@problem1.id.to_s, @problem2.id.to_s]
346   - assigns(:selected_problems).should == [@problem1, @problem2]
347   - end
348   -
349   - it "should require at least one problem" do
350   - post :resolve_several, :problems => []
351   - request.flash[:notice].should match(/You have not selected any/)
352   - end
353   -
354 344 context "POST /problems/merge_several" do
355 345 it "should require at least two problems" do
356 346 post :merge_several, :problems => [@problem1.id.to_s]
357   - request.flash[:notice].should match(/You must select at least two/)
  347 + request.flash[:notice].should eql I18n.t('controllers.problems.flash.need_two_errors_merge')
358 348 end
359 349  
360 350 it "should merge the problems" do
361   - lambda {
362   - post :merge_several, :problems => [@problem1.id.to_s, @problem2.id.to_s]
363   - assigns(:merged_problem).reload.errs.length.should == 2
364   - }.should change(Problem, :count).by(-1)
  351 + ProblemMerge.should_receive(:new).and_return(double(:merge => true))
  352 + post :merge_several, :problems => [@problem1.id.to_s, @problem2.id.to_s]
365 353 end
366 354 end
367 355  
368 356 context "POST /problems/unmerge_several" do
  357 +
  358 + it "should require at least one problem" do
  359 + post :unmerge_several, :problems => []
  360 + request.flash[:notice].should eql I18n.t('controllers.problems.flash.no_select_problem')
  361 + end
  362 +
369 363 it "should unmerge a merged problem" do
370 364 merged_problem = Problem.merge!(@problem1, @problem2)
371 365 merged_problem.errs.length.should == 2
... ... @@ -374,9 +368,16 @@ describe ProblemsController do
374 368 merged_problem.reload.errs.length.should == 1
375 369 }.should change(Problem, :count).by(1)
376 370 end
  371 +
377 372 end
378 373  
379 374 context "POST /problems/resolve_several" do
  375 +
  376 + it "should require at least one problem" do
  377 + post :resolve_several, :problems => []
  378 + request.flash[:notice].should eql I18n.t('controllers.problems.flash.no_select_problem')
  379 + end
  380 +
380 381 it "should resolve the issue" do
381 382 post :resolve_several, :problems => [@problem2.id.to_s]
382 383 @problem2.reload.resolved?.should == true
... ... @@ -390,10 +391,17 @@ describe ProblemsController do
390 391 it "should display a message about 2 errs" do
391 392 post :resolve_several, :problems => [@problem1.id.to_s, @problem2.id.to_s]
392 393 flash[:success].should match(/2 errs have been resolved/)
  394 + controller.selected_problems.should == [@problem1, @problem2]
393 395 end
394 396 end
395 397  
396 398 context "POST /problems/unresolve_several" do
  399 +
  400 + it "should require at least one problem" do
  401 + post :unresolve_several, :problems => []
  402 + request.flash[:notice].should eql I18n.t('controllers.problems.flash.no_select_problem')
  403 + end
  404 +
397 405 it "should unresolve the issue" do
398 406 post :unresolve_several, :problems => [@problem1.id.to_s]
399 407 @problem1.reload.resolved?.should == false
... ...
spec/models/problem_spec.rb
... ... @@ -152,7 +152,6 @@ describe Problem do
152 152 end
153 153 end
154 154  
155   -
156 155 context "Scopes" do
157 156 context "resolved" do
158 157 it 'only finds resolved Problems' do
... ...