Commit 11b082045552d616dad01df7f7ee5a387b409863
1 parent
51c1d1bb
Exists in
master
and in
1 other branch
Little refactoring of action in ProblemController
Showing
4 changed files
with
69 additions
and
39 deletions
Show diff stats
app/controllers/problems_controller.rb
@@ -6,10 +6,13 @@ | @@ -6,10 +6,13 @@ | ||
6 | # COLLECTION => :index, :all, :destroy_several, :resolve_several, :unresolve_several, :merge_several, :unmerge_several, :search | 6 | # COLLECTION => :index, :all, :destroy_several, :resolve_several, :unresolve_several, :merge_several, :unmerge_several, :search |
7 | class ProblemsController < ApplicationController | 7 | class ProblemsController < ApplicationController |
8 | 8 | ||
9 | - before_filter :find_selected_problems, :only => [:destroy_several, :resolve_several, :unresolve_several, :merge_several, :unmerge_several] | ||
10 | before_filter :set_sorting_params, :only => [:index, :all, :search] | 9 | before_filter :set_sorting_params, :only => [:index, :all, :search] |
11 | before_filter :set_tracker_params, :only => [:create_issue] | 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 | expose(:app) { | 16 | expose(:app) { |
14 | if current_user.admin? | 17 | if current_user.admin? |
15 | App.find(params[:app_id]) | 18 | App.find(params[:app_id]) |
@@ -17,15 +20,23 @@ class ProblemsController < ApplicationController | @@ -17,15 +20,23 @@ class ProblemsController < ApplicationController | ||
17 | current_user.apps.find(params[:app_id]) | 20 | current_user.apps.find(params[:app_id]) |
18 | end | 21 | end |
19 | } | 22 | } |
23 | + | ||
20 | expose(:problem) { | 24 | expose(:problem) { |
21 | app.problems.find(params[:id]) | 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 | def index | 36 | def index |
25 | app_scope = current_user.admin? ? App.all : current_user.apps | 37 | app_scope = current_user.admin? ? App.all : current_user.apps |
26 | @all_errs = params[:all_errs] | 38 | @all_errs = params[:all_errs] |
27 | @problems = Problem.for_apps(app_scope).in_env(params[:environment]).all_else_unresolved(@all_errs).ordered_by(@sort, @order) | 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 | respond_to do |format| | 40 | respond_to do |format| |
30 | format.html do | 41 | format.html do |
31 | @problems = @problems.page(params[:page]).per(current_user.per_page) | 42 | @problems = @problems.page(params[:page]).per(current_user.per_page) |
@@ -64,35 +75,40 @@ class ProblemsController < ApplicationController | @@ -64,35 +75,40 @@ class ProblemsController < ApplicationController | ||
64 | end | 75 | end |
65 | 76 | ||
66 | def resolve_several | 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 | redirect_to :back | 80 | redirect_to :back |
70 | end | 81 | end |
71 | 82 | ||
72 | def unresolve_several | 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 | redirect_to :back | 86 | redirect_to :back |
76 | end | 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 | def merge_several | 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 | else | 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 | end | 100 | end |
85 | redirect_to :back | 101 | redirect_to :back |
86 | end | 102 | end |
87 | 103 | ||
88 | def unmerge_several | 104 | def unmerge_several |
89 | - all = @selected_problems.map(&:unmerge!).flatten | 105 | + all = selected_problems.map(&:unmerge!).flatten |
90 | flash[:success] = "#{I18n.t(:n_errs_have, :count => all.length)} been unmerged." | 106 | flash[:success] = "#{I18n.t(:n_errs_have, :count => all.length)} been unmerged." |
91 | redirect_to :back | 107 | redirect_to :back |
92 | end | 108 | end |
93 | 109 | ||
94 | def destroy_several | 110 | def destroy_several |
95 | - nb_problem_destroy = ProblemDestroy.execute(@selected_problems) | 111 | + nb_problem_destroy = ProblemDestroy.execute(selected_problems) |
96 | flash[:notice] = "#{I18n.t(:n_errs_have, :count => nb_problem_destroy)} been deleted." | 112 | flash[:notice] = "#{I18n.t(:n_errs_have, :count => nb_problem_destroy)} been deleted." |
97 | redirect_to :back | 113 | redirect_to :back |
98 | end | 114 | end |
@@ -117,20 +133,20 @@ class ProblemsController < ApplicationController | @@ -117,20 +133,20 @@ class ProblemsController < ApplicationController | ||
117 | IssueTracker.default_url_options[:protocol] = request.scheme | 133 | IssueTracker.default_url_options[:protocol] = request.scheme |
118 | end | 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 | def set_sorting_params | 136 | def set_sorting_params |
131 | @sort = params[:sort] | 137 | @sort = params[:sort] |
132 | @sort = "last_notice_at" unless %w{app message last_notice_at last_deploy_at count}.member?(@sort) | 138 | @sort = "last_notice_at" unless %w{app message last_notice_at last_deploy_at count}.member?(@sort) |
133 | @order = params[:order] || "desc" | 139 | @order = params[:order] || "desc" |
134 | end | 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 | end | 151 | end |
136 | 152 |
config/locales/en.yml
@@ -2,7 +2,7 @@ | @@ -2,7 +2,7 @@ | ||
2 | # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. | 2 | # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. |
3 | 3 | ||
4 | en: | 4 | en: |
5 | - hello: "Hello world" | 5 | + |
6 | flash: | 6 | flash: |
7 | apps: | 7 | apps: |
8 | create: | 8 | create: |
@@ -11,6 +11,7 @@ en: | @@ -11,6 +11,7 @@ en: | ||
11 | success: "Good news everyone! '%{app_name}' was successfully updated." | 11 | success: "Good news everyone! '%{app_name}' was successfully updated." |
12 | destroy: | 12 | destroy: |
13 | success: "'%{app_name}' was successfully destroyed." | 13 | success: "'%{app_name}' was successfully destroyed." |
14 | + | ||
14 | n_errs_have: | 15 | n_errs_have: |
15 | one: "%{count} err has" | 16 | one: "%{count} err has" |
16 | other: "%{count} errs have" | 17 | other: "%{count} errs have" |
@@ -38,6 +39,12 @@ en: | @@ -38,6 +39,12 @@ en: | ||
38 | error: "You can't delete yourself" | 39 | error: "You can't delete yourself" |
39 | update: | 40 | update: |
40 | success: "%{name}'s information was successfully updated." | 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 | devise: | 49 | devise: |
43 | registrations: | 50 | registrations: |
spec/controllers/problems_controller_spec.rb
@@ -341,31 +341,25 @@ describe ProblemsController do | @@ -341,31 +341,25 @@ describe ProblemsController do | ||
341 | @problem2 = Fabricate(:err, :problem => Fabricate(:problem, :resolved => false)).problem | 341 | @problem2 = Fabricate(:err, :problem => Fabricate(:problem, :resolved => false)).problem |
342 | end | 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 | context "POST /problems/merge_several" do | 344 | context "POST /problems/merge_several" do |
355 | it "should require at least two problems" do | 345 | it "should require at least two problems" do |
356 | post :merge_several, :problems => [@problem1.id.to_s] | 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 | end | 348 | end |
359 | 349 | ||
360 | it "should merge the problems" do | 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 | end | 353 | end |
366 | end | 354 | end |
367 | 355 | ||
368 | context "POST /problems/unmerge_several" do | 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 | it "should unmerge a merged problem" do | 363 | it "should unmerge a merged problem" do |
370 | merged_problem = Problem.merge!(@problem1, @problem2) | 364 | merged_problem = Problem.merge!(@problem1, @problem2) |
371 | merged_problem.errs.length.should == 2 | 365 | merged_problem.errs.length.should == 2 |
@@ -374,9 +368,16 @@ describe ProblemsController do | @@ -374,9 +368,16 @@ describe ProblemsController do | ||
374 | merged_problem.reload.errs.length.should == 1 | 368 | merged_problem.reload.errs.length.should == 1 |
375 | }.should change(Problem, :count).by(1) | 369 | }.should change(Problem, :count).by(1) |
376 | end | 370 | end |
371 | + | ||
377 | end | 372 | end |
378 | 373 | ||
379 | context "POST /problems/resolve_several" do | 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 | it "should resolve the issue" do | 381 | it "should resolve the issue" do |
381 | post :resolve_several, :problems => [@problem2.id.to_s] | 382 | post :resolve_several, :problems => [@problem2.id.to_s] |
382 | @problem2.reload.resolved?.should == true | 383 | @problem2.reload.resolved?.should == true |
@@ -390,10 +391,17 @@ describe ProblemsController do | @@ -390,10 +391,17 @@ describe ProblemsController do | ||
390 | it "should display a message about 2 errs" do | 391 | it "should display a message about 2 errs" do |
391 | post :resolve_several, :problems => [@problem1.id.to_s, @problem2.id.to_s] | 392 | post :resolve_several, :problems => [@problem1.id.to_s, @problem2.id.to_s] |
392 | flash[:success].should match(/2 errs have been resolved/) | 393 | flash[:success].should match(/2 errs have been resolved/) |
394 | + controller.selected_problems.should == [@problem1, @problem2] | ||
393 | end | 395 | end |
394 | end | 396 | end |
395 | 397 | ||
396 | context "POST /problems/unresolve_several" do | 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 | it "should unresolve the issue" do | 405 | it "should unresolve the issue" do |
398 | post :unresolve_several, :problems => [@problem1.id.to_s] | 406 | post :unresolve_several, :problems => [@problem1.id.to_s] |
399 | @problem1.reload.resolved?.should == false | 407 | @problem1.reload.resolved?.should == false |
spec/models/problem_spec.rb
@@ -152,7 +152,6 @@ describe Problem do | @@ -152,7 +152,6 @@ describe Problem do | ||
152 | end | 152 | end |
153 | end | 153 | end |
154 | 154 | ||
155 | - | ||
156 | context "Scopes" do | 155 | context "Scopes" do |
157 | context "resolved" do | 156 | context "resolved" do |
158 | it 'only finds resolved Problems' do | 157 | it 'only finds resolved Problems' do |