GCF-3.2.patch

Frederico Camara, 2019-02-25 17:05

Download (21.8 KB)

View differences:

app/controllers/projects_controller.rb
162 162
    @member ||= @project.members.new
163 163
    @trackers = Tracker.sorted.to_a
164 164
    @wiki ||= @project.wiki || Wiki.new(:project => @project)
165
    @cfs=AttributeGroupField.joins(:attribute_group).joins(:custom_field).joins(:tracker).
166
       where(:attribute_groups => {project_id: @project}, :custom_fields => {id: @project.all_issue_custom_fields.pluck(:id)}).
167
       pluck("trackers.id", "attribute_groups.id", "attribute_groups.name", "attribute_groups.position",
168
             "id", "position", "custom_fields.id", "custom_fields.name", "custom_fields.position").sort_by{|x| [x[3], x[5]]}
165 169
  end
166 170

  
167 171
  def edit
......
194 198
    redirect_to settings_project_path(@project, :tab => 'modules')
195 199
  end
196 200

  
201
  def groupissuescustomfields
202
    # clean invalid values: invalid cfs, empty cf lists, empty groups
203
    group_issues_custom_fields = (JSON.parse params[:group_issues_custom_fields]).
204
      each{|tid,v| v.replace(v.select{|k,v| v["cfs"] ? v["cfs"].delete_if{|k,v| @project.all_issue_custom_fields.pluck(:id).include?(v)} : v})}.
205
      each{|tid,v| v.delete_if{|k,v| v["cfs"].blank?}}.
206
      delete_if{|k,v| v.blank?}
207

  
208
    groups = AttributeGroup.where(project_id: @project.id).collect(&:id)
209
    fields = AttributeGroupField.where(attribute_group_id: groups).collect(&:id) 
210
    group_issues_custom_fields.each do |tid,v|
211
      v.each do |gp, g|
212
        gid = groups.shift
213
        if gid.nil?
214
          gid=AttributeGroup.create(project_id: @project.id, tracker_id: tid, name: g["name"].nil? ? nil : g["name"], position: gp).id
215
        else
216
          AttributeGroup.update(gid, project_id: @project.id, tracker_id: tid, name: g["name"].nil? ? nil : g["name"], position: gp)
217
        end
218
        g['cfs'].each do |cfp, cf|
219
          cfid = fields.shift
220
          if cfid.nil?
221
            AttributeGroupField.create(attribute_group_id: gid, custom_field_id: cf, position: cfp)
222
          else
223
            AttributeGroupField.update(cfid, attribute_group_id: gid, custom_field_id: cf, position: cfp)
224
          end
225
        end
226
      end
227
    end
228
    fields.each do |i|
229
      AttributeGroupField.delete(i)
230
    end
231
    groups.each do |i|
232
      AttributeGroup.delete(i)
233
    end
234
    flash[:notice] = l(:notice_successful_update)
235
    redirect_to settings_project_path(@project, :tab => 'groupissuescustomfields')
236
  end
237

  
197 238
  def archive
198 239
    unless @project.archive
199 240
      flash[:error] = l(:error_can_not_archive_project)
app/helpers/issues_helper.rb
210 215
    r.to_html
211 216
  end
212 217

  
218
  def group_by_keys(project_id, tracker_id, custom_field_values)
219
    keys_grouped = AttributeGroupField.joins(:attribute_group).
220
      where(:attribute_groups => {project_id: project_id, tracker_id: tracker_id}).
221
      order("attribute_groups.position", :position).pluck(:name, :custom_field_id).group_by(&:shift)
222
    custom_fields_grouped = { nil => (keys_grouped[nil].nil? ? [] : keys_grouped[nil].map{|n| custom_field_values.select{|x| x.custom_field[:id] == n[0]}}.flatten) |
223
      custom_field_values.select{|y| ! keys_grouped.values.flatten.include?(y.custom_field[:id])}}
224
    keys_grouped.reject{|k,v| k == nil}.each{|k,v| custom_fields_grouped[k] = v.map{|n| custom_field_values.select{|x| x.custom_field[:id] == n[0]}}.flatten}
225
    custom_fields_grouped
226
  end
227

  
213 228
  def render_custom_fields_rows(issue)
214
    values = issue.visible_custom_field_values
215
    return if values.empty?
216
    half = (values.size / 2.0).ceil
217
    issue_fields_rows do |rows|
218
      values.each_with_index do |value, i|
219
        css = "cf_#{value.custom_field.id}"
220
        m = (i < half ? :left : :right)
221
        rows.send m, custom_field_name_tag(value.custom_field), show_value(value), :class => css
229
    s=''
230
    group_by_keys(issue.project_id, issue.tracker_id, issue.visible_custom_field_values).each do |title, values|
231
      if values.present?
232
        s << content_tag('h4', title, :style => 'background: #0001; padding: 0.3em;') unless title.nil?
233
        half = (values.size / 2.0).ceil
234
        s << issue_fields_rows do |rows|
235
          values.each_with_index do |value, i|
236
            css = "cf_#{value.custom_field.id}"
237
            m = (i < half ? :left : :right)
238
            rows.send m, custom_field_name_tag(value.custom_field), show_value(value), :class => css
239
          end
240
        end
222 241
      end
223 242
    end
243
    s.html_safe
224 244
  end
225 245

  
226 246
  # Returns the path for updating the issue form
app/helpers/projects_helper.rb
27 27
            {:name => 'wiki', :action => :manage_wiki, :partial => 'projects/settings/wiki', :label => :label_wiki},
28 28
            {:name => 'repositories', :action => :manage_repository, :partial => 'projects/settings/repositories', :label => :label_repository_plural},
29 29
            {:name => 'boards', :action => :manage_boards, :partial => 'projects/settings/boards', :label => :label_board_plural},
30
            {:name => 'activities', :action => :manage_project_activities, :partial => 'projects/settings/activities', :label => :enumeration_activities}
30
            {:name => 'activities', :action => :manage_project_activities, :partial => 'projects/settings/activities', :label => :enumeration_activities},
31
            {:name => 'groupissuescustomfields', :action => :edit_project, :partial => 'projects/settings/groupissuescustomfields', :label => :grouped_cf}
31 32
            ]
32 33
    tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)}
33 34
  end
app/models/attribute_group.rb
1
class AttributeGroup < ActiveRecord::Base
2
  belongs_to :project
3
  belongs_to :tracker
4
  has_many :attribute_group_fields
5
  has_many :custom_fields, :through => :attribute_group_fields
6
  acts_as_list
7

  
8
  scope :sorted, lambda { order(:position) }
9
end
app/models/attribute_group_field.rb
1
class AttributeGroupField < ActiveRecord::Base
2
  belongs_to :attribute_group
3
  belongs_to :custom_field
4
  has_one :tracker, :through => :attribute_group
5
  acts_as_list
6

  
7
  scope :sorted, lambda { order(:position) }
8
end
app/models/project.rb
50 51
  has_many :changesets, :through => :repository
51 52
  has_one :wiki, :dependent => :destroy
52 53
  # Custom field for the project issues
54
  has_many :attribute_groups
55
  has_many :attribute_group_fields, :through => :attribute_groups
53 56
  has_and_belongs_to_many :issue_custom_fields,
54 57
                          lambda {order("#{CustomField.table_name}.position")},
55 58
                          :class_name => 'IssueCustomField',
app/models/tracker.rb
34 34

  
35 35
  has_and_belongs_to_many :projects
36 36
  has_and_belongs_to_many :custom_fields, :class_name => 'IssueCustomField', :join_table => "#{table_name_prefix}custom_fields_trackers#{table_name_suffix}", :association_foreign_key => 'custom_field_id'
37
  has_many :attribute_groups
38
  has_many :attribute_group_fields, :through => :attribute_groups
37 39
  acts_as_list
38 40

  
39 41
  attr_protected :fields_bits
app/views/issues/_form_custom_fields.html.erb
1
<% custom_field_values = @issue.editable_custom_field_values %>
2
<% if custom_field_values.present? %>
1
<% group_by_keys(@issue.project_id, @issue.tracker_id, @issue.editable_custom_field_values).each do |title,values| %>
2
<% if values.present? %>
3
<%= content_tag('h4', title, :style => 'background: #0001; padding: 0.3em;') unless title.nil? %>
3 4
<div class="splitcontent">
4 5
<div class="splitcontentleft">
5 6
<% i = 0 %>
6
<% split_on = (custom_field_values.size / 2.0).ceil - 1 %>
7
<% custom_field_values.each do |value| %>
7
<% split_on = (values.size / 2.0).ceil - 1 %>
8
<% values.each do |value| %>
8 9
  <p><%= custom_field_tag_with_label :issue, value, :required => @issue.required_attribute?(value.custom_field_id) %></p>
9
<% if i == split_on -%>
10
<% if i == split_on %>
10 11
</div><div class="splitcontentright">
11
<% end -%>
12
<% i += 1 -%>
13
<% end -%>
12
<% end %>
13
<% i += 1 %>
14
<% end %>
14 15
</div>
15 16
</div>
16 17
<% end %>
18
<% end %>
app/views/projects/settings/_groupissuescustomfields.html.erb
1
<p><%= select_tag "tracker_id", options_from_collection_for_select(@project.trackers.collect, "id", "name"), {:required => true, :onchange => "refresh_trackers(this);"} %><p>
2

  
3
<div id='custom_fields_form'>
4
  <% @project.trackers.each do |t| %>
5
  <div id='tracker_<%= t.id %>' class='tracker'>
6
    <span style="color: #888;"><%= l(:changed_cf_position) %></span>
7
    <ul class='sortable_items sortable_tracker-<%= t.id %> nil not_a_group'>
8
      <% @cfs.select{|x| x[0]==t.id && x[2]==nil}.each do |x| %>
9
      <li id="<%= x[6] %>" pos="<%= x[8] %>"><%= x[7] %></li>
10
      <% end %>
11
    </ul>
12
    <span style="color: #888;"><%= l(:global_cf_position) %></span>
13
    <ul class="sortable_items sortable_tracker-<%= t.id %> unsorted not_a_group">
14
      <% Issue.new(project_id: @project.id, tracker_id: t.id).custom_field_values.
15
           select{|x| ! @cfs.select{|c| c[0]==t.id}.map{|c| c[6]}.include?(x.custom_field_id)}.
16
           collect{|x| [x.custom_field.id, x.custom_field.name, x.custom_field.position]}.each do |x| %>
17
      <li id="<%= x[0] %>" pos="<%= x[2] %>"><%= x[1] %></li>
18
      <% end %>
19
    </ul>
20
    <span style="color: #888;"><%= l(:grouped_cf) %></span>
21
    <div class='sortable_groups groups_tracker-<%= t.id %>'>
22
      <% @cfs.select{|x| x[0]==t.id && x[2]!=nil}.map{|x| x[2]}.uniq.each do |g| %>
23
      <div class="sortable_group">
24
        <input type="text" value="<%= g %>" style="background: #ddf; padding: 5px; width: 360px"/>
25
        <img src="/images/delete.png" onclick="remove_label(this, 'tracker-<%= t.id %>');"/>
26
        <ul class='sortable_items sortable_tracker-<%= t.id %>'>
27
          <% @cfs.select{|x| x[0]==t.id && x[2]==g}.each do |x| %>
28
          <li id="<%= x[6] %>" pos="<%= x[8] %>"><%= x[7] %></li>
29
          <% end %>
30
        </ul>
31
      </div>
32
      <% end %>
33
      <div>
34
        <input type="text" value="" style="background: #ddf; padding: 5px; width: 370px"/>
35
        <img src="/images/add.png" style="" onclick="add_label(this, 'tracker-<%= t.id %>');"/>
36
      </div>
37
    </div>
38
  </div>
39
  <% end %>
40
<%= form_for @project, :url => { :action => 'groupissuescustomfields', :id => @project },
41
            :html => {:id => 'groupissuescustomfields-form',
42
                      :method => :post} do |f| %>
43

  
44
<%= hidden_field :group_issues_custom_fields, '', :id => 'group_issues_custom_fields', :name => 'group_issues_custom_fields' %>
45
<p><%= submit_tag l(:button_save), :onclick => "fill_json_data();" %></p>
46
<% end %>
47
</div>
48

  
49
<script>
50
function init_sortables(labelclass) {
51
  $( '.groups_' + labelclass ).sortable({
52
    items: 'div.sortable_group',
53
    connectWith: '.groups_' + labelclass,
54
    start: function(event, ui) {
55
      ui.placeholder.height(ui.item.height());
56
    },
57
    axis: 'y'
58
  });
59
  $( '.sortable_' + labelclass ).sortable({
60
    connectWith: '.sortable_' + labelclass,
61
    update: function(event, ui) {
62
      if (ui.item.parent().hasClass('unsorted')) {
63
        ui.item.parent().prepend(ui.item);
64
        ui.item.parent().children('li').each(function () {
65
          if (parseInt($(this).attr('pos')) < parseInt(ui.item.attr('pos'))) {
66
            ui.item.insertAfter(this);
67
          }
68
        });
69
      }
70
    },
71
    axis: 'y'
72
  }).disableSelection();
73
}
74
function refresh_trackers(tracker) {
75
  $('.tracker').hide();
76
  $('#tracker_' + tracker.value).show();
77
}
78
function add_label(label, labelclass) {
79
  var old_label = $(label), new_label = $(label).parent().clone();
80
  new_label.children().first().val('').trigger('change');
81
  old_label.attr('src','/images/delete.png').attr('onclick','remove_label(this, "' + labelclass + '");');
82
  old_label.prev().width('360px');
83
  old_label.parent().addClass('ui-sortable-handle sortable_group');
84
  old_label.parent().sortable({
85
    connectWith: '.groups_' + labelclass
86
  }).disableSelection();
87
  old_label.parent().append('<ul id="' + labelclass + '_" class="sortable_items sortable_' + labelclass + '"/>');
88
  old_label.next().sortable({
89
    connectWith: '.sortable_' + labelclass
90
  }).disableSelection();
91
  old_label.parent().parent().append(new_label);
92
}
93
function remove_label(label, labelclass) {
94
  $(label).next().children().each(function () {
95
    var item = this;
96
    $('.unsorted.sortable_' + labelclass).prepend(this);
97
    $('.unsorted.sortable_' + labelclass).children('li').each(function () {
98
      if (parseInt($(this).attr('pos')) < parseInt($(item).attr('pos'))) {
99
        $(item).insertAfter(this);
100
      }
101
    });
102
  });
103
  $(label).parent().hide();
104
}
105
function fill_json_data() {
106
  var r = {}, gp = 0, cp = 0;
107
  $('#custom_fields_form').children('div').each(function () {
108
    var tracker_id = this.id.split('_').pop();
109
    gp = 0, cp = 0;
110

  
111
    // Group 'nil'
112
    r[tracker_id] = {};
113
    r[tracker_id][++gp] = {'cfs': {}};
114
    $(this).children('.nil').children().each(function () {
115
      cp++;
116
      r[tracker_id][gp]['cfs'][cp] = this.id;
117
    });
118
    if (! r[tracker_id][gp]['cfs'][1]) { delete r[tracker_id][gp]; }
119
    $(this).children('.sortable_groups').children().each(function () {
120
      if ($(this).children('ul').length) {
121
        r[tracker_id][++gp] = {
122
          'name': $(this).children('input').val(),
123
          'cfs': {}
124
        };
125
        cp = 0;
126
        $(this).children('ul').children().each(function () {
127
          r[tracker_id][gp]['cfs'][++cp] = this.id;
128
        });
129
        if (! r[tracker_id][gp]['cfs'][1]) { r[tracker_id][gp] = ''; }
130
      }
131
    });
132
    if ( ! r[tracker_id] ) { r[tracker_id] = ''; }
133
  });
134
  console.log(r);
135
  $('#group_issues_custom_fields').val(JSON.stringify(r)); }
136
$('#tracker_id').change();
137
<% @project.trackers.each do |t| %>
138
init_sortables('tracker-<%= t.id %>');
139
<% end %>
140
</script>
config/locales/en.yml
1170 1189
  description_date_from: Enter start date
1171 1190
  description_date_to: Enter end date
1172 1191
  text_repository_identifier_info: 'Only lower case letters (a-z), numbers, dashes and underscores are allowed.<br />Once saved, the identifier cannot be changed.'
1192

  
1193
  grouped_cf: Grouped Custom Fields
1194
  global_cf_position: Global Custom Fields Position
1195
  changed_cf_position: Changed Custom Fields Position
config/locales/pt-BR.yml
1203 1218
  label_default_values_for_new_users: Valor padrão para novos usuários
1204 1219
  error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted: Spent time cannot
1205 1220
    be reassigned to an issue that is about to be deleted
1221

  
1222
  grouped_cf: Agrupar campos personalizados
1223
  global_cf_position: Posição global de campos personalizados
1224
  changed_cf_position: Alterar posição de campos personalizados
config/routes.rb
101 102
    member do
102 103
      get 'settings(/:tab)', :action => 'settings', :as => 'settings'
103 104
      post 'modules'
105
      post 'groupissuescustomfields'
104 106
      post 'archive'
105 107
      post 'unarchive'
106 108
      post 'close'
db/migrate/20180913211420_create_attribute_groups.rb
1
class CreateAttributeGroups < ActiveRecord::Migration
2
  def change
3
    create_table :attribute_groups do |t|
4
      t.references :project, index: true, foreign_key: true
5
      t.references :tracker, index: true, foreign_key: true
6
      t.string :name
7
      t.integer :position, :default => 1, :null => false
8

  
9
      t.timestamps null: false
10
    end
11
  end
12
end
db/migrate/20180913212008_create_attribute_group_fields.rb
1
class CreateAttributeGroupFields < ActiveRecord::Migration
2
  def change
3
    create_table :attribute_group_fields do |t|
4
      t.references :attribute_group, index: true, foreign_key: true
5
      t.references :custom_field, index: true, foreign_key: true
6
      t.integer :position
7

  
8
      t.timestamps null: false
9
    end
10
  end
11
end
lib/redmine.rb
77 77
  map.permission :view_project, {:projects => [:show], :activities => [:index]}, :public => true, :read => true
78 78
  map.permission :search_project, {:search => :index}, :public => true, :read => true
79 79
  map.permission :add_project, {:projects => [:new, :create]}, :require => :loggedin
80
  map.permission :edit_project, {:projects => [:settings, :edit, :update]}, :require => :member
80
  map.permission :edit_project, {:projects => [:settings, :edit, :update, :groupissuescustomfields]}, :require => :member
81 81
  map.permission :close_project, {:projects => [:close, :reopen]}, :require => :member, :read => true
82 82
  map.permission :select_project_modules, {:projects => :modules}, :require => :member
83 83
  map.permission :view_members, {:members => [:index, :show]}, :public => true, :read => true
public/stylesheets/application.css
361 363
div.issue span.private, div.journal span.private { position:relative; bottom: 2px; text-transform: uppercase; background: #d22; color: #fff; font-weight:bold; padding: 0px 2px 0px 2px; font-size: 60%; margin-right: 2px; border-radius: 2px;}
362 364
div.issue .next-prev-links {color:#999;}
363 365
div.issue .attributes {margin-top: 2em;}
364
div.issue .attribute {padding-left:180px; clear:left; min-height: 1.8em;}
365
div.issue .attribute .label {width: 170px; margin-left:-180px; font-weight:bold; float:left;}
366
div.issue .attribute {padding-left:225px; clear:left; min-height: 1.8em;}
367
div.issue .attribute .label {width: 215px; margin-left:-225px; font-weight:bold; float:left;}
368
div.issue .attribute .value p {margin: 0 0 0.8em 0;}
366 369
div.issue.overdue .due-date .value { color: #c22; }
367 370

  
368 371
#issue_tree table.issues, #relations table.issues { border: 0; }
......
1313 1322
  height:1px;
1314 1323
  overflow:hidden;
1315 1324
}
1325

  
1326
/* Custom Field Groups */
1327
.sortable_groups {
1328
  background: #eee;
1329
  border: 1px solid #888;
1330
  width: 445px;
1331
  min-height: 30px;
1332
  margin: 5px;
1333
  padding: 5px;
1334
}
1335
.sortable_groups div {
1336
  background: #ddf;
1337
  border: 1px solid #888;
1338
  min-height: 30px;
1339
  margin: 5px;
1340
  padding: 5px;
1341
}
1342
.sortable_items {
1343
  background: #eee;
1344
  border: 1px solid #888;
1345
  width: 400px;
1346
  min-height: 30px;
1347
  margin: 5px;
1348
  padding: 5px;
1349
}
1350
.sortable_items li {
1351
  background: #ffffff;
1352
  border: 1px solid #888;
1353
  margin: 5px;
1354
  padding: 5px;
1355
  list-style-type: none;
1356
  font-size: 1.2em;
1357
}
1358
.not_a_group {
1359
  padding: 5px 27px;
1360
}
1361
.ui-sortable-handle:before {
1362
  content:url('/images/reorder.png');
1363
  margin-right: 5px;
1364
}
1365
.ui-sortable-placeholder {
1366
  background: #8888ff;
1367
  border: 1px solid #888;
1368
  visibility: visible;
1369
}
test/fixtures/attribute_group_fields.yml
1
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2

  
3
one:
4
  attribute_group_id: 
5
  custom_field_id: 
6
  position: 1
7

  
8
two:
9
  attribute_group_id: 
10
  custom_field_id: 
11
  position: 1
test/fixtures/attribute_groups.yml
1
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2

  
3
one:
4
  project_id: 
5
  tracker_id: 
6
  name: MyString
7
  position: 1
8

  
9
two:
10
  project_id: 
11
  tracker_id: 
12
  name: MyString
13
  position: 1