Commit d8b03d4d07bf139ac551e43969612481c64b677a

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

Refactor ProblemController

Extract some instance variable in DecentExposure
extract same usage of params in ProblemController and AppController
app/controllers/apps_controller.rb
1 1 class AppsController < InheritedResources::Base
  2 +
  3 + include ProblemsSearcher
  4 +
2 5 before_filter :require_admin!, :except => [:index, :show]
3 6 before_filter :parse_email_at_notices_or_set_default, :only => [:create, :update]
4 7 before_filter :parse_notice_at_notices_or_set_default, :only => [:create, :update]
... ... @@ -9,16 +12,10 @@ class AppsController &lt; InheritedResources::Base
9 12 format.html do
10 13 @all_errs = !!params[:all_errs]
11 14  
12   - @sort = params[:sort]
13   - @order = params[:order]
14   - @sort = "last_notice_at" unless %w{message app last_deploy_at last_notice_at count}.member?(@sort)
15   - @order = "desc" unless %w{asc desc}.member?(@order)
16   -
17 15 @problems = resource.problems
18 16 @problems = @problems.unresolved unless @all_errs
19   - @problems = @problems.in_env(params[:environment]).ordered_by(@sort, @order).page(params[:page]).per(current_user.per_page)
  17 + @problems = @problems.in_env(params[:environment]).ordered_by(params_sort, params_order).page(params[:page]).per(current_user.per_page)
20 18  
21   - @selected_problems = params[:problems] || []
22 19 @deploys = @app.deploys.order_by(:created_at.desc).limit(5)
23 20 end
24 21 format.atom do
... ...
app/controllers/problems_controller.rb
... ... @@ -6,8 +6,8 @@
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 :set_sorting_params, :only => [:index, :all, :search]
10   - before_filter :set_tracker_params, :only => [:create_issue]
  9 +
  10 + include ProblemsSearcher
11 11  
12 12 before_filter :need_selected_problem, :only => [
13 13 :resolve_several, :unresolve_several, :unmerge_several
... ... @@ -25,25 +25,35 @@ class ProblemsController &lt; ApplicationController
25 25 app.problems.find(params[:id])
26 26 }
27 27  
28   - expose(:err_ids) {
29   - (params[:problems] || []).compact
  28 +
  29 + expose(:all_errs) {
  30 + params[:all_errs]
  31 + }
  32 +
  33 + expose(:app_scope) {
  34 + apps = current_user.admin? ? App.all : current_user.apps
  35 + params[:app_id] ? apps.where(:_id => params[:app_id]) : apps
30 36 }
31 37  
32   - expose(:selected_problems) {
33   - Array(Problem.find(err_ids))
  38 + expose(:params_environement) {
  39 + params[:environment]
34 40 }
35 41  
36   - def index
37   - app_scope = current_user.admin? ? App.all : current_user.apps
38   - @all_errs = params[:all_errs]
39   - @problems = Problem.for_apps(app_scope).in_env(params[:environment]).all_else_unresolved(@all_errs).ordered_by(@sort, @order)
40   - respond_to do |format|
41   - format.html do
42   - @problems = @problems.page(params[:page]).per(current_user.per_page)
43   - end
44   - format.atom
  42 + expose(:problems) {
  43 + pro = Problem.for_apps(
  44 + app_scope
  45 + ).in_env(
  46 + params_environement
  47 + ).all_else_unresolved(all_errs).ordered_by(params_sort, params_order)
  48 +
  49 + if request.format == :html
  50 + pro.page(params[:page]).per(current_user.per_page)
  51 + else
  52 + pro
45 53 end
46   - end
  54 + }
  55 +
  56 + def index; end
47 57  
48 58 def show
49 59 @notices = problem.notices.reverse_ordered.page(params[:notice]).per(1)
... ... @@ -52,10 +62,11 @@ class ProblemsController &lt; ApplicationController
52 62 end
53 63  
54 64 def create_issue
  65 + IssueTracker.update_url_options(request)
55 66 issue_creation = IssueCreation.new(problem, current_user, params[:tracker])
56 67  
57 68 unless issue_creation.execute
58   - flash[:error] = issue_creation.errors[:base].first
  69 + flash[:error] = issue_creation.errors.full_messages.join(', ')
59 70 end
60 71  
61 72 redirect_to app_problem_path(app, problem)
... ... @@ -114,12 +125,7 @@ class ProblemsController &lt; ApplicationController
114 125 end
115 126  
116 127 def search
117   - if params[:app_id]
118   - app_scope = App.where(:_id => params[:app_id])
119   - else
120   - app_scope = current_user.admin? ? App.all : current_user.apps
121   - end
122   - @problems = Problem.search(params[:search]).for_apps(app_scope).in_env(params[:environment]).all_else_unresolved(params[:all_errs]).ordered_by(@sort, @order)
  128 + @problems = Problem.search(params[:search]).for_apps(app_scope).in_env(params[:environment]).all_else_unresolved(params[:all_errs]).ordered_by(params_sort, params_order)
123 129 @selected_problems = params[:problems] || []
124 130 @problems = @problems.page(params[:page]).per(current_user.per_page)
125 131 render :content_type => 'text/javascript'
... ... @@ -127,18 +133,6 @@ class ProblemsController &lt; ApplicationController
127 133  
128 134 protected
129 135  
130   - def set_tracker_params
131   - IssueTracker.default_url_options[:host] = request.host
132   - IssueTracker.default_url_options[:port] = request.port
133   - IssueTracker.default_url_options[:protocol] = request.scheme
134   - end
135   -
136   - def set_sorting_params
137   - @sort = params[:sort]
138   - @sort = "last_notice_at" unless %w{app message last_notice_at last_deploy_at count}.member?(@sort)
139   - @order = params[:order] || "desc"
140   - end
141   -
142 136 ##
143 137 # Redirect :back if no errors selected
144 138 #
... ...
app/controllers/problems_searcher.rb 0 → 100644
... ... @@ -0,0 +1,33 @@
  1 +# Include to do a Search
  2 +# TODO: Need to be in a Dedicated Object ProblemsSearch with params like input
  3 +#
  4 +module ProblemsSearcher
  5 + extend ActiveSupport::Concern
  6 +
  7 + included do
  8 +
  9 + expose(:params_sort) {
  10 + unless %w{app message last_notice_at last_deploy_at count}.member?(params[:sort])
  11 + "last_notice_at"
  12 + else
  13 + params[:sort]
  14 + end
  15 + }
  16 +
  17 + expose(:params_order){
  18 + unless %w{asc desc}.member?(params[:order])
  19 + 'desc'
  20 + else
  21 + params[:order]
  22 + end
  23 + }
  24 +
  25 + expose(:selected_problems) {
  26 + Array(Problem.find(err_ids))
  27 + }
  28 +
  29 + expose(:err_ids) {
  30 + (params[:problems] || []).compact
  31 + }
  32 + end
  33 +end
... ...
app/helpers/sort_helper.rb
1 1 # encoding: utf-8
2 2 module SortHelper
3   -
  3 +
4 4 def link_for_sort(name, field=nil)
5 5 field ||= name.underscore
6   - current = (@sort == field)
7   - order = (current && (@order == "asc")) ? "desc" : "asc"
  6 + current = (params_sort == field)
  7 + order = (current && (params_order == "asc")) ? "desc" : "asc"
8 8 url = request.path + "?sort=#{field}&order=#{order}"
9 9 options = {}
10 10 options.merge!(:class => "current #{order}") if current
11 11 link_to(name, url, options)
12 12 end
13   -
  13 +
14 14 end
... ...
app/interactors/issue_creation.rb
... ... @@ -41,15 +41,11 @@ class IssueCreation
41 41 end
42 42  
43 43 def execute
44   - if tracker
45   - begin
46   - tracker.create_issue problem, user
47   - rescue => ex
48   - Rails.logger.error "Error during issue creation: " << ex.message
49   - errors.add :base, "There was an error during issue creation: #{ex.message}"
50   - end
51   - end
52   -
  44 + tracker.create_issue problem, user if tracker
53 45 errors.empty?
  46 + rescue => ex
  47 + Rails.logger.error "Error during issue creation: " << ex.message
  48 + errors.add :base, "There was an error during issue creation: #{ex.message}"
  49 + false
54 50 end
55 51 end
... ...
app/models/issue_tracker.rb
... ... @@ -40,4 +40,15 @@ class IssueTracker
40 40 def configured?
41 41 project_id.present?
42 42 end
  43 +
  44 + ##
  45 + # Update default_url_option with valid data from the request information
  46 + #
  47 + # @param [ Request ] a request with host, port and protocol
  48 + #
  49 + def self.update_url_options(request)
  50 + IssueTracker.default_url_options[:host] = request.host
  51 + IssueTracker.default_url_options[:port] = request.port
  52 + IssueTracker.default_url_options[:protocol] = request.scheme
  53 + end
43 54 end
... ...
app/views/problems/_table.html.haml
... ... @@ -16,7 +16,7 @@
16 16 - problems.each do |problem|
17 17 %tr{:class => problem.resolved? ? 'resolved' : 'unresolved'}
18 18 %td.select
19   - = check_box_tag "problems[]", problem.id, @selected_problems.member?(problem.id.to_s)
  19 + = check_box_tag "problems[]", problem.id, selected_problems.member?(problem.id.to_s)
20 20 %td.app
21 21 = link_to problem.app.name, app_path(problem.app)
22 22 - if current_page?(:controller => 'problems')
... ...
app/views/problems/index.html.haml
1 1 - content_for :title, @all_errs ? 'All Errors' : 'Unresolved Errors'
2 2 - content_for :head do
3 3 = auto_discovery_link_tag :atom, problems_path(User.token_authentication_key => current_user.authentication_token, :format => "atom"), :title => "Errbit notices at #{request.host}"
  4 +
4 5 - content_for :action_bar do
5 6 - if @all_errs
6 7 = link_to 'hide resolved', problems_path, :class => 'button'
7 8 - else
8 9 = link_to 'show resolved', problems_path(:all_errs => true), :class => 'button'
  10 +
9 11 %section
10 12 = form_tag search_problems_path(:all_errs => @all_errs), :method => :get, :remote => true do
11 13 = text_field_tag :search, params[:search], :placeholder => 'Search for issues'
12 14 %br
13 15 %section
14   - .problem_table{:id => 'problem_table'}
15   - = render 'problems/table', :problems => @problems
  16 + #problem_table.problem_table
  17 + = render 'problems/table'
... ...
spec/controllers/problems_controller_spec.rb
... ... @@ -27,13 +27,13 @@ describe ProblemsController do
27 27  
28 28 it "should have default per_page value for user" do
29 29 get :index
30   - assigns(:problems).to_a.size.should == User::PER_PAGE
  30 + controller.problems.to_a.size.should == User::PER_PAGE
31 31 end
32 32  
33 33 it "should be able to override default per_page value" do
34 34 @user.update_attribute :per_page, 10
35 35 get :index
36   - assigns(:problems).to_a.size.should == 10
  36 + controller.problems.to_a.size.should == 10
37 37 end
38 38 end
39 39  
... ... @@ -48,35 +48,35 @@ describe ProblemsController do
48 48 context 'no params' do
49 49 it 'shows problems for all environments' do
50 50 get :index
51   - assigns(:problems).size.should == 21
  51 + controller.problems.size.should == 21
52 52 end
53 53 end
54 54  
55 55 context 'environment production' do
56 56 it 'shows problems for just production' do
57 57 get :index, :environment => 'production'
58   - assigns(:problems).size.should == 6
  58 + controller.problems.size.should == 6
59 59 end
60 60 end
61 61  
62 62 context 'environment staging' do
63 63 it 'shows problems for just staging' do
64 64 get :index, :environment => 'staging'
65   - assigns(:problems).size.should == 5
  65 + controller.problems.size.should == 5
66 66 end
67 67 end
68 68  
69 69 context 'environment development' do
70 70 it 'shows problems for just development' do
71 71 get :index, :environment => 'development'
72   - assigns(:problems).size.should == 5
  72 + controller.problems.size.should == 5
73 73 end
74 74 end
75 75  
76 76 context 'environment test' do
77 77 it 'shows problems for just test' do
78 78 get :index, :environment => 'test'
79   - assigns(:problems).size.should == 5
  79 + controller.problems.size.should == 5
80 80 end
81 81 end
82 82 end
... ... @@ -89,8 +89,8 @@ describe ProblemsController do
89 89 watched_unresolved_err = Fabricate(:err, :problem => Fabricate(:problem, :app => Fabricate(:user_watcher, :user => user).app, :resolved => false))
90 90 watched_resolved_err = Fabricate(:err, :problem => Fabricate(:problem, :app => Fabricate(:user_watcher, :user => user).app, :resolved => true))
91 91 get :index
92   - assigns(:problems).should include(watched_unresolved_err.problem)
93   - assigns(:problems).should_not include(unwatched_err.problem, watched_resolved_err.problem)
  92 + controller.problems.should include(watched_unresolved_err.problem)
  93 + controller.problems.should_not include(unwatched_err.problem, watched_resolved_err.problem)
94 94 end
95 95 end
96 96 end
... ... @@ -106,7 +106,7 @@ describe ProblemsController do
106 106 mock('proxy', :page => mock('other_proxy', :per => problems))
107 107 )
108 108 get :index, :all_errs => true
109   - assigns(:problems).should == problems
  109 + controller.problems.should == problems
110 110 end
111 111 end
112 112  
... ... @@ -117,8 +117,8 @@ describe ProblemsController do
117 117 watched_unresolved_problem = Fabricate(:problem, :app => Fabricate(:user_watcher, :user => user).app, :resolved => false)
118 118 watched_resolved_problem = Fabricate(:problem, :app => Fabricate(:user_watcher, :user => user).app, :resolved => true)
119 119 get :index, :all_errs => true
120   - assigns(:problems).should include(watched_resolved_problem, watched_unresolved_problem)
121   - assigns(:problems).should_not include(unwatched_problem)
  120 + controller.problems.should include(watched_resolved_problem, watched_unresolved_problem)
  121 + controller.problems.should_not include(unwatched_problem)
122 122 end
123 123 end
124 124 end
... ...
spec/views/problems/index.html.haml_spec.rb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +require 'spec_helper'
  2 +
  3 +describe "problems/index.html.haml" do
  4 + let(:problem_1) { Fabricate(:problem) }
  5 + let(:problem_2) { Fabricate(:problem, :app => problem_1.app) }
  6 +
  7 + before do
  8 + # view.stub(:app).and_return(problem.app)
  9 + view.stub(:selected_problems).and_return([])
  10 + view.stub(:problems).and_return(Kaminari.paginate_array([problem_1, problem_2]).page(1).per(10))
  11 + view.stub(:params_sort).and_return('asc')
  12 + controller.stub(:current_user) { Fabricate(:user) }
  13 + end
  14 +
  15 + describe "with problem" do
  16 + before { problem_1 && problem_2 }
  17 +
  18 + it 'should works' do
  19 + render
  20 + rendered.should have_selector('div#problem_table.problem_table')
  21 + end
  22 + end
  23 +
  24 +end
  25 +
... ...