Commit 6523e5b903d609d72406395b5fb47b97b13ba02c

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

[WIP] refactor the problems_controller

Extract the filter `find_problem` in decent_exposure to get this problem
app/controllers/problems_controller.rb
... ... @@ -6,7 +6,6 @@
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_problem, :except => [:index, :all, :destroy_several, :resolve_several, :unresolve_several, :merge_several, :unmerge_several, :search]
10 9 before_filter :find_selected_problems, :only => [:destroy_several, :resolve_several, :unresolve_several, :merge_several, :unmerge_several]
11 10 before_filter :set_sorting_params, :only => [:index, :all, :search]
12 11 before_filter :set_tracker_params, :only => [:create_issue]
... ... @@ -18,6 +17,9 @@ class ProblemsController &lt; ApplicationController
18 17 current_user.apps.find(params[:app_id])
19 18 end
20 19 }
  20 + expose(:problem) {
  21 + app.problems.find(params[:id])
  22 + }
21 23  
22 24 def index
23 25 app_scope = current_user.admin? ? App.all : current_user.apps
... ... @@ -33,28 +35,28 @@ class ProblemsController &lt; ApplicationController
33 35 end
34 36  
35 37 def show
36   - @notices = @problem.notices.reverse_ordered.page(params[:notice]).per(1)
  38 + @notices = problem.notices.reverse_ordered.page(params[:notice]).per(1)
37 39 @notice = @notices.first
38 40 @comment = Comment.new
39 41 end
40 42  
41 43 def create_issue
42   - issue_creation = IssueCreation.new(@problem, current_user, params[:tracker])
  44 + issue_creation = IssueCreation.new(problem, current_user, params[:tracker])
43 45  
44 46 unless issue_creation.execute
45 47 flash[:error] = issue_creation.errors[:base].first
46 48 end
47 49  
48   - redirect_to app_problem_path(app, @problem)
  50 + redirect_to app_problem_path(app, problem)
49 51 end
50 52  
51 53 def unlink_issue
52   - @problem.update_attribute :issue_link, nil
53   - redirect_to app_problem_path(app, @problem)
  54 + problem.update_attribute :issue_link, nil
  55 + redirect_to app_problem_path(app, problem)
54 56 end
55 57  
56 58 def resolve
57   - @problem.resolve!
  59 + problem.resolve!
58 60 flash[:success] = 'Great news everyone! The err has been resolved.'
59 61 redirect_to :back
60 62 rescue ActionController::RedirectBackError
... ... @@ -109,10 +111,6 @@ class ProblemsController &lt; ApplicationController
109 111  
110 112 protected
111 113  
112   - def find_problem
113   - @problem = app.problems.find(params[:id])
114   - end
115   -
116 114 def set_tracker_params
117 115 IssueTracker.default_url_options[:host] = request.host
118 116 IssueTracker.default_url_options[:port] = request.port
... ...
app/views/problems/_issue_tracker_links.html.haml
1 1 - if app.issue_tracker_configured? || current_user.github_account?
2   - - if @problem.issue_link.present?
3   - %span= link_to 'go to issue', @problem.issue_link, :class => "#{@problem.issue_type}_goto goto-issue"
4   - = link_to 'unlink issue', unlink_issue_app_problem_path(app, @problem), :method => :delete, :data => { :confirm => "Unlink err issues?" }, :class => "unlink-issue"
5   - - elsif @problem.issue_link == "pending"
6   - %span.disabled= link_to 'creating...', '#', :class => "#{@problem.issue_type}_inactive create-issue"
7   - = link_to 'retry', create_issue_app_problem_path(app, @problem), :method => :post
  2 + - if problem.issue_link.present?
  3 + %span= link_to 'go to issue', problem.issue_link, :class => "#{problem.issue_type}_goto goto-issue"
  4 + = link_to 'unlink issue', unlink_issue_app_problem_path(app, problem), :method => :delete, :data => { :confirm => "Unlink err issues?" }, :class => "unlink-issue"
  5 + - elsif problem.issue_link == "pending"
  6 + %span.disabled= link_to 'creating...', '#', :class => "#{problem.issue_type}_inactive create-issue"
  7 + = link_to 'retry', create_issue_app_problem_path(app, problem), :method => :post
8 8 - else
9 9 - if app.github_repo?
10 10 - if current_user.can_create_github_issues?
11   - %span= link_to 'create issue', create_issue_app_problem_path(app, @problem, :tracker => 'user_github'), :method => :post, :class => "github_create create-issue"
  11 + %span= link_to 'create issue', create_issue_app_problem_path(app, problem, :tracker => 'user_github'), :method => :post, :class => "github_create create-issue"
12 12 - elsif app.issue_tracker_configured? && app.issue_tracker.label.eql?('github')
13   - %span= link_to 'create issue', create_issue_app_problem_path(app, @problem), :method => :post, :class => "github_create create-issue"
  13 + %span= link_to 'create issue', create_issue_app_problem_path(app, problem), :method => :post, :class => "github_create create-issue"
14 14 - if app.issue_tracker_configured? && !app.issue_tracker.label.eql?('github')
15   - %span= link_to 'create issue', create_issue_app_problem_path(app, @problem), :method => :post, :class => "#{app.issue_tracker.label}_create create-issue"
  15 + %span= link_to 'create issue', create_issue_app_problem_path(app, problem), :method => :post, :class => "#{app.issue_tracker.label}_create create-issue"
... ...
app/views/problems/show.html.haml
1   -- content_for :page_title, @problem.message
  1 +- content_for :page_title, problem.message
2 2 - content_for :title_css_class, 'err_show'
3   -- content_for :title, @problem.error_class || truncate(@problem.message, :length => 32)
  3 +- content_for :title, problem.error_class || truncate(problem.message, :length => 32)
4 4 - content_for :meta do
5 5 %strong App:
6 6 = link_to app.name, app
7 7 %strong Where:
8   - = @problem.where
  8 + = problem.where
9 9 %br
10 10 %strong Environment:
11   - = @problem.environment
  11 + = problem.environment
12 12 %strong Last Notice:
13   - = @problem.last_notice_at.to_s(:precise)
  13 + = problem.last_notice_at.to_s(:precise)
14 14 - content_for :action_bar do
15   - - if @problem.unresolved?
16   - %span= link_to 'resolve', [:resolve, app, @problem], :method => :put, :data => { :confirm => problem_confirm }, :class => 'resolve'
  15 + - if problem.unresolved?
  16 + %span= link_to 'resolve', [:resolve, app, problem], :method => :put, :data => { :confirm => problem_confirm }, :class => 'resolve'
17 17 - if current_user.authentication_token
18   - %span= link_to 'iCal', polymorphic_path([app, @problem], :format => "ics", :auth_token => current_user.authentication_token), :class => "calendar_link"
  18 + %span= link_to 'iCal', polymorphic_path([app, problem], :format => "ics", :auth_token => current_user.authentication_token), :class => "calendar_link"
19 19 %span>= link_to 'up', (request.env['HTTP_REFERER'] ? :back : app_problems_path(app)), :class => 'up'
20 20 %br
21 21 = render "issue_tracker_links"
22 22  
23   -- if @problem.comments_allowed? || @problem.comments.any?
  23 +- if problem.comments_allowed? || problem.comments.any?
24 24 - content_for :comments do
25 25 %h3 Comments
26   - - @problem.comments.each do |comment|
  26 + - problem.comments.each do |comment|
27 27 .window
28 28 %table.comment
29 29 %tr
... ... @@ -37,11 +37,11 @@
37 37 - else
38 38 %span.comment-info
39 39 = time_ago_in_words(comment.created_at, true) << " ago by [Unknown User]"
40   - %span.delete= link_to '&#10008;'.html_safe, [app, @problem, comment], :method => :delete, :data => { :confirm => "Are you sure you don't need this comment?" }, :class => "destroy-comment"
  40 + %span.delete= link_to '&#10008;'.html_safe, [app, problem, comment], :method => :delete, :data => { :confirm => "Are you sure you don't need this comment?" }, :class => "destroy-comment"
41 41 %tr
42 42 %td= simple_format comment.body
43   - - if @problem.comments_allowed?
44   - = form_for [app, @problem, @comment] do |comment_form|
  43 + - if problem.comments_allowed?
  44 + = form_for [app, problem, @comment] do |comment_form|
45 45 %p Add a comment
46 46 = comment_form.text_area :body
47 47 = comment_form.submit "Save Comment"
... ... @@ -63,7 +63,7 @@
63 63 - if @notice
64 64 #summary
65 65 %h3 Summary
66   - = render 'notices/summary', :notice => @notice, :problem => @problem
  66 + = render 'notices/summary', :notice => @notice
67 67  
68 68 #backtrace
69 69 %h3 Backtrace
... ...
app/views/problems/show.ics.haml
1   -= generate_problem_ical(@problem.notices.order_by(:created_at.asc))
  1 += generate_problem_ical(problem.notices.order_by(:created_at.asc))
... ...
spec/controllers/problems_controller_spec.rb
... ... @@ -138,7 +138,7 @@ describe ProblemsController do
138 138  
139 139 it "finds the problem" do
140 140 get :show, :app_id => app.id, :id => err.problem.id
141   - assigns(:problem).should == err.problem
  141 + controller.problem.should == err.problem
142 142 end
143 143  
144 144 it "successfully render page" do
... ... @@ -179,7 +179,7 @@ describe ProblemsController do
179 179  
180 180 it 'finds the problem if the user is watching the app' do
181 181 get :show, :app_id => @watched_app.to_param, :id => @watched_err.problem.id
182   - assigns(:problem).should == @watched_err.problem
  182 + controller.problem.should == @watched_err.problem
183 183 end
184 184  
185 185 it 'raises a DocumentNotFound error if the user is not watching the app' do
... ... @@ -205,7 +205,7 @@ describe ProblemsController do
205 205 @problem.app.problems.should_receive(:find).and_return(@problem.problem)
206 206 put :resolve, :app_id => @problem.app.id, :id => @problem.problem.id
207 207 controller.app.should == @problem.app
208   - assigns(:problem).should == @problem.problem
  208 + controller.problem.should == @problem.problem
209 209 end
210 210  
211 211 it "should resolve the issue" do
... ...
spec/views/problems/show.html.haml_spec.rb
1 1 require 'spec_helper'
2 2  
3 3 describe "problems/show.html.haml" do
  4 + let(:problem) { Fabricate(:problem) }
  5 + let(:comment) { Fabricate(:comment) }
  6 +
4 7 before do
5   - problem = Fabricate(:problem)
6   - comment = Fabricate(:comment)
7   - assign :problem, problem
8   - assign :comment, comment
9 8 view.stub(:app).and_return(problem.app)
  9 + view.stub(:problem).and_return(problem)
  10 +
  11 + assign :comment, comment
10 12 assign :notices, problem.notices.page(1).per(1)
11 13 assign :notice, problem.notices.first
  14 +
12 15 controller.stub(:current_user) { Fabricate(:user) }
13 16 end
14 17  
15 18 def with_issue_tracker(tracker, problem)
16 19 problem.app.issue_tracker = tracker.new :api_token => "token token token", :project_id => "1234"
17   - assign :problem, problem
  20 + view.stub(:problem).and_return(problem)
18 21 view.stub(:app).and_return(problem.app)
19 22 end
20 23  
... ... @@ -54,7 +57,7 @@ describe &quot;problems/show.html.haml&quot; do
54 57 it "should link 'up' to app_problems_path if HTTP_REFERER isn't set'" do
55 58 controller.request.env['HTTP_REFERER'] = nil
56 59 problem = Fabricate(:problem_with_comments)
57   - assign :problem, problem
  60 + view.stub(:problem).and_return(problem)
58 61 view.stub(:app).and_return(problem.app)
59 62 render
60 63  
... ... @@ -67,7 +70,7 @@ describe &quot;problems/show.html.haml&quot; do
67 70 controller.stub(:current_user) { user }
68 71  
69 72 problem = Fabricate(:problem_with_comments, :app => Fabricate(:app, :github_repo => "test_user/test_repo"))
70   - assign :problem, problem
  73 + view.stub(:problem).and_return(problem)
71 74 view.stub(:app).and_return(problem.app)
72 75 render
73 76  
... ... @@ -77,7 +80,7 @@ describe &quot;problems/show.html.haml&quot; do
77 80 it 'should allow creating issue for github if application has a github tracker' do
78 81 problem = Fabricate(:problem_with_comments, :app => Fabricate(:app, :github_repo => "test_user/test_repo"))
79 82 with_issue_tracker(GithubIssuesTracker, problem)
80   - assign :problem, problem
  83 + view.stub(:problem).and_return(problem)
81 84 view.stub(:app).and_return(problem.app)
82 85 render
83 86  
... ... @@ -89,7 +92,7 @@ describe &quot;problems/show.html.haml&quot; do
89 92 let(:app) { App.new(:new_record => false) }
90 93  
91 94 it 'not see link to create issue' do
92   - assign :problem, problem
  95 + view.stub(:problem).and_return(problem)
93 96 view.stub(:app).and_return(problem.app)
94 97 render
95 98 expect(view.content_for(:action_bar)).to_not match(/create issue/)
... ... @@ -105,7 +108,7 @@ describe &quot;problems/show.html.haml&quot; do
105 108 context "with problem without issue link" do
106 109 let(:problem){ Problem.new(:new_record => false, :app => app) }
107 110 it 'not see link if no issue tracker' do
108   - assign :problem, problem
  111 + view.stub(:problem).and_return(problem)
109 112 view.stub(:app).and_return(problem.app)
110 113 render
111 114 expect(view.content_for(:action_bar)).to match(/create issue/)
... ... @@ -117,7 +120,7 @@ describe &quot;problems/show.html.haml&quot; do
117 120 let(:problem){ Problem.new(:new_record => false, :app => app, :issue_link => 'http://foo') }
118 121  
119 122 it 'not see link if no issue tracker' do
120   - assign :problem, problem
  123 + view.stub(:problem).and_return(problem)
121 124 view.stub(:app).and_return(problem.app)
122 125 render
123 126 expect(view.content_for(:action_bar)).to_not match(/create issue/)
... ... @@ -136,7 +139,7 @@ describe &quot;problems/show.html.haml&quot; do
136 139  
137 140 it 'should display comments and new comment form when no issue tracker' do
138 141 problem = Fabricate(:problem_with_comments)
139   - assign :problem, problem
  142 + view.stub(:problem).and_return(problem)
140 143 view.stub(:app).and_return(problem.app)
141 144 render
142 145  
... ...
spec/views/problems/show.ics.haml_spec.rb 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +require 'spec_helper'
  2 +
  3 +describe "problems/show.html.ics" do
  4 + let(:problem) { Fabricate(:problem) }
  5 + before do
  6 + view.stub(:problem).and_return(problem)
  7 + end
  8 +
  9 + it 'should work' do
  10 + render :template => 'problems/show.ics.haml'
  11 + end
  12 +
  13 +
  14 +end
... ...