0001-Issues-autocomplete-should-response-with-content-typ.patch

Marius BALTEANU, 2019-02-17 15:29

Download (3.13 KB)

View differences:

app/controllers/auto_completes_controller.rb
19 19
  before_action :find_project
20 20

  
21 21
  def issues
22
    @issues = []
22
    issues = []
23 23
    q = (params[:q] || params[:term]).to_s.strip
24 24
    status = params[:status].to_s
25 25
    issue_id = params[:issue_id].to_s
......
32 32
        scope = scope.where.not(:id => issue_id.to_i)
33 33
      end
34 34
      if q.match(/\A#?(\d+)\z/)
35
        @issues << scope.find_by_id($1.to_i)
35
        issues << scope.find_by_id($1.to_i)
36 36
      end
37 37

  
38
      @issues += scope.like(q).order(:id => :desc).limit(10).to_a
39
      @issues.compact!
38
      issues += scope.like(q).order(:id => :desc).limit(10).to_a
39
      issues.compact!
40 40
    end
41
    render :layout => false
41

  
42
    render :json => format_issues_json(issues)
42 43
  end
43 44

  
44 45
  private
......
50 51
  rescue ActiveRecord::RecordNotFound
51 52
    render_404
52 53
  end
54

  
55
  def format_issues_json(issues)
56
    issues.map {|issue| {
57
      'id' => issue.id,
58
      'label' => "#{issue.tracker} ##{issue.id}: #{issue.subject.to_s.truncate(60)}",
59
      'value' => issue.id
60
      }
61
    }
62
  end
53 63
end
app/views/auto_completes/issues.html.erb
1
<%= raw @issues.map {|issue| {
2
      'id' => issue.id,
3
      'label' => "#{issue.tracker} ##{issue.id}: #{issue.subject.to_s.truncate(60)}",
4
      'value' => issue.id
5
      }
6
    }.to_json
7
%>
test/functional/auto_completes_controller_test.rb
138 138
    assert_include "issue", response.body
139 139
    assert_not_include "Bug #12: Closed issue on a locked version", response.body
140 140
  end
141

  
142
  def test_auto_complete_should_return_json_content_type_response
143
    get :issues, :params => {
144
        :project_id => 'subproject1',
145
        :q => '#13'
146
      }
147

  
148
    assert_response :success
149
    assert_include 'application/json', response.headers['Content-Type']
150
  end
141 151
end
142
-