Project

General

Profile

Feature #44153 ยป 0001-Add-projects-to-issue-custom-fields-API-response.patch

Go MAEDA, 2026-06-07 10:25

View differences:

app/controllers/custom_fields_controller.rb
34 34
          IssueCustomField.where(is_for_all: false).joins(:projects).group(:custom_field_id).count
35 35
      end
36 36
      format.api do
37
        @custom_fields = CustomField.includes(:roles).all
37
        @custom_fields = CustomField.includes(:roles).all.to_a
38
        issue_custom_fields = @custom_fields.grep(IssueCustomField)
39
        ActiveRecord::Associations::Preloader.new(
40
          records: issue_custom_fields,
41
          associations: [:projects, :trackers]
42
        ).call
38 43
      end
39 44
    end
40 45
  end
app/views/custom_fields/index.api.rsb
10 10
      api.min_length        field.min_length
11 11
      api.max_length        field.max_length
12 12
      api.is_required       field.is_required?
13
      api.is_for_all        field.is_for_all?
13 14
      api.is_filter         field.is_filter?
14 15
      api.searchable        field.searchable
15 16
      api.multiple          field.multiple?
......
30 31
      end
31 32

  
32 33
      if field.is_a?(IssueCustomField)
34
        api.array :projects do
35
          field.projects.each do |project|
36
            api.project :id => project.id, :name => project.name
37
          end
38
        end
39

  
33 40
        api.array :trackers do
34 41
          field.trackers.each do |tracker|
35 42
            api.tracker :id => tracker.id, :name => tracker.name
test/integration/api_test/custom_fields_test.rb
31 31
        assert_select 'description', :text => 'Select one of the databases'
32 32
        assert_select 'id', :text => '2'
33 33
        assert_select 'customized_type', :text => 'issue'
34
        assert_select 'is_for_all', :text => 'true'
34 35
        assert_select 'possible_values[type=array]' do
35 36
          assert_select 'possible_value>value', :text => 'PostgreSQL'
36 37
          assert_select 'possible_value>label', :text => 'PostgreSQL'
37 38
        end
39
        assert_select 'projects[type=array]'
38 40
        assert_select 'trackers[type=array]'
39 41
        assert_select 'roles[type=array]'
40 42
        assert_select 'visible', :text => 'true'
......
57 59
    end
58 60
  end
59 61

  
62
  test "GET /custom_fields.xml should include projects for issue custom fields limited to projects" do
63
    get '/custom_fields.xml', :headers => credentials('admin')
64
    assert_response :success
65

  
66
    xml = Hash.from_xml(response.body)
67
    field = xml['custom_fields'].detect {|f| f['id'] == '9'}
68
    assert_kind_of Hash, field
69
    assert_equal 'false', field['is_for_all']
70
    assert_kind_of Array, field['projects']
71
    assert_equal [{'id' => '1', 'name' => 'eCookbook'}], field['projects']
72
  end
73

  
74
  test "GET /custom_fields.xml should include empty projects for issue custom fields for all projects" do
75
    get '/custom_fields.xml', :headers => credentials('admin')
76
    assert_response :success
77

  
78
    xml = Hash.from_xml(response.body)
79
    field = xml['custom_fields'].detect {|f| f['id'] == '1'}
80
    assert_kind_of Hash, field
81
    assert_equal 'true', field['is_for_all']
82
    assert_equal [], field['projects']
83
  end
84

  
85
  test "GET /custom_fields.xml should not include projects for custom fields that are not issue custom fields" do
86
    get '/custom_fields.xml', :headers => credentials('admin')
87
    assert_response :success
88

  
89
    xml = Hash.from_xml(response.body)
90
    field = xml['custom_fields'].detect {|f| f['id'] == '3'}
91
    assert_kind_of Hash, field
92
    assert_not field.has_key?('projects')
93
  end
94

  
60 95
  test "GET /custom_fields.xml should include roles for custom fields visible by role" do
61 96
    custom_fields = [
62 97
      IssueCustomField.generate!(:visible => false, :role_ids => [1, 2]),
    (1-1/1)