Project

General

Profile

Patch #30919 » GCF-3.2.patch

Frederico Camara, 2019-02-25 17:05

View differences:

app/controllers/projects_controller.rb
@member ||= @project.members.new
@trackers = Tracker.sorted.to_a
@wiki ||= @project.wiki || Wiki.new(:project => @project)
@cfs=AttributeGroupField.joins(:attribute_group).joins(:custom_field).joins(:tracker).
where(:attribute_groups => {project_id: @project}, :custom_fields => {id: @project.all_issue_custom_fields.pluck(:id)}).
pluck("trackers.id", "attribute_groups.id", "attribute_groups.name", "attribute_groups.position",
"id", "position", "custom_fields.id", "custom_fields.name", "custom_fields.position").sort_by{|x| [x[3], x[5]]}
end
def edit
......
redirect_to settings_project_path(@project, :tab => 'modules')
end
def groupissuescustomfields
# clean invalid values: invalid cfs, empty cf lists, empty groups
group_issues_custom_fields = (JSON.parse params[:group_issues_custom_fields]).
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})}.
each{|tid,v| v.delete_if{|k,v| v["cfs"].blank?}}.
delete_if{|k,v| v.blank?}
groups = AttributeGroup.where(project_id: @project.id).collect(&:id)
fields = AttributeGroupField.where(attribute_group_id: groups).collect(&:id)
group_issues_custom_fields.each do |tid,v|
v.each do |gp, g|
gid = groups.shift
if gid.nil?
gid=AttributeGroup.create(project_id: @project.id, tracker_id: tid, name: g["name"].nil? ? nil : g["name"], position: gp).id
else
AttributeGroup.update(gid, project_id: @project.id, tracker_id: tid, name: g["name"].nil? ? nil : g["name"], position: gp)
end
g['cfs'].each do |cfp, cf|
cfid = fields.shift
if cfid.nil?
AttributeGroupField.create(attribute_group_id: gid, custom_field_id: cf, position: cfp)
else
AttributeGroupField.update(cfid, attribute_group_id: gid, custom_field_id: cf, position: cfp)
end
end
end
end
fields.each do |i|
AttributeGroupField.delete(i)
end
groups.each do |i|
AttributeGroup.delete(i)
end
flash[:notice] = l(:notice_successful_update)
redirect_to settings_project_path(@project, :tab => 'groupissuescustomfields')
end
def archive
unless @project.archive
flash[:error] = l(:error_can_not_archive_project)
app/helpers/issues_helper.rb
r.to_html
end
def group_by_keys(project_id, tracker_id, custom_field_values)
keys_grouped = AttributeGroupField.joins(:attribute_group).
where(:attribute_groups => {project_id: project_id, tracker_id: tracker_id}).
order("attribute_groups.position", :position).pluck(:name, :custom_field_id).group_by(&:shift)
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) |
custom_field_values.select{|y| ! keys_grouped.values.flatten.include?(y.custom_field[:id])}}
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}
custom_fields_grouped
end
def render_custom_fields_rows(issue)
values = issue.visible_custom_field_values
return if values.empty?
half = (values.size / 2.0).ceil
issue_fields_rows do |rows|
values.each_with_index do |value, i|
css = "cf_#{value.custom_field.id}"
m = (i < half ? :left : :right)
rows.send m, custom_field_name_tag(value.custom_field), show_value(value), :class => css
s=''
group_by_keys(issue.project_id, issue.tracker_id, issue.visible_custom_field_values).each do |title, values|
if values.present?
s << content_tag('h4', title, :style => 'background: #0001; padding: 0.3em;') unless title.nil?
half = (values.size / 2.0).ceil
s << issue_fields_rows do |rows|
values.each_with_index do |value, i|
css = "cf_#{value.custom_field.id}"
m = (i < half ? :left : :right)
rows.send m, custom_field_name_tag(value.custom_field), show_value(value), :class => css
end
end
end
end
s.html_safe
end
# Returns the path for updating the issue form
app/helpers/projects_helper.rb
{:name => 'wiki', :action => :manage_wiki, :partial => 'projects/settings/wiki', :label => :label_wiki},
{:name => 'repositories', :action => :manage_repository, :partial => 'projects/settings/repositories', :label => :label_repository_plural},
{:name => 'boards', :action => :manage_boards, :partial => 'projects/settings/boards', :label => :label_board_plural},
{:name => 'activities', :action => :manage_project_activities, :partial => 'projects/settings/activities', :label => :enumeration_activities}
{:name => 'activities', :action => :manage_project_activities, :partial => 'projects/settings/activities', :label => :enumeration_activities},
{:name => 'groupissuescustomfields', :action => :edit_project, :partial => 'projects/settings/groupissuescustomfields', :label => :grouped_cf}
]
tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)}
end
app/models/attribute_group.rb
class AttributeGroup < ActiveRecord::Base
belongs_to :project
belongs_to :tracker
has_many :attribute_group_fields
has_many :custom_fields, :through => :attribute_group_fields
acts_as_list
scope :sorted, lambda { order(:position) }
end
app/models/attribute_group_field.rb
class AttributeGroupField < ActiveRecord::Base
belongs_to :attribute_group
belongs_to :custom_field
has_one :tracker, :through => :attribute_group
acts_as_list
scope :sorted, lambda { order(:position) }
end
app/models/project.rb
has_many :changesets, :through => :repository
has_one :wiki, :dependent => :destroy
# Custom field for the project issues
has_many :attribute_groups
has_many :attribute_group_fields, :through => :attribute_groups
has_and_belongs_to_many :issue_custom_fields,
lambda {order("#{CustomField.table_name}.position")},
:class_name => 'IssueCustomField',
app/models/tracker.rb
has_and_belongs_to_many :projects
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'
has_many :attribute_groups
has_many :attribute_group_fields, :through => :attribute_groups
acts_as_list
attr_protected :fields_bits
app/views/issues/_form_custom_fields.html.erb
<% custom_field_values = @issue.editable_custom_field_values %>
<% if custom_field_values.present? %>
<% group_by_keys(@issue.project_id, @issue.tracker_id, @issue.editable_custom_field_values).each do |title,values| %>
<% if values.present? %>
<%= content_tag('h4', title, :style => 'background: #0001; padding: 0.3em;') unless title.nil? %>
<div class="splitcontent">
<div class="splitcontentleft">
<% i = 0 %>
<% split_on = (custom_field_values.size / 2.0).ceil - 1 %>
<% custom_field_values.each do |value| %>
<% split_on = (values.size / 2.0).ceil - 1 %>
<% values.each do |value| %>
<p><%= custom_field_tag_with_label :issue, value, :required => @issue.required_attribute?(value.custom_field_id) %></p>
<% if i == split_on -%>
<% if i == split_on %>
</div><div class="splitcontentright">
<% end -%>
<% i += 1 -%>
<% end -%>
<% end %>
<% i += 1 %>
<% end %>
</div>
</div>
<% end %>
<% end %>
app/views/projects/settings/_groupissuescustomfields.html.erb
<p><%= select_tag "tracker_id", options_from_collection_for_select(@project.trackers.collect, "id", "name"), {:required => true, :onchange => "refresh_trackers(this);"} %><p>
<div id='custom_fields_form'>
<% @project.trackers.each do |t| %>
<div id='tracker_<%= t.id %>' class='tracker'>
<span style="color: #888;"><%= l(:changed_cf_position) %></span>
<ul class='sortable_items sortable_tracker-<%= t.id %> nil not_a_group'>
<% @cfs.select{|x| x[0]==t.id && x[2]==nil}.each do |x| %>
<li id="<%= x[6] %>" pos="<%= x[8] %>"><%= x[7] %></li>
<% end %>
</ul>
<span style="color: #888;"><%= l(:global_cf_position) %></span>
<ul class="sortable_items sortable_tracker-<%= t.id %> unsorted not_a_group">
<% Issue.new(project_id: @project.id, tracker_id: t.id).custom_field_values.
select{|x| ! @cfs.select{|c| c[0]==t.id}.map{|c| c[6]}.include?(x.custom_field_id)}.
collect{|x| [x.custom_field.id, x.custom_field.name, x.custom_field.position]}.each do |x| %>
<li id="<%= x[0] %>" pos="<%= x[2] %>"><%= x[1] %></li>
<% end %>
</ul>
<span style="color: #888;"><%= l(:grouped_cf) %></span>
<div class='sortable_groups groups_tracker-<%= t.id %>'>
<% @cfs.select{|x| x[0]==t.id && x[2]!=nil}.map{|x| x[2]}.uniq.each do |g| %>
<div class="sortable_group">
<input type="text" value="<%= g %>" style="background: #ddf; padding: 5px; width: 360px"/>
<img src="/images/delete.png" onclick="remove_label(this, 'tracker-<%= t.id %>');"/>
<ul class='sortable_items sortable_tracker-<%= t.id %>'>
<% @cfs.select{|x| x[0]==t.id && x[2]==g}.each do |x| %>
<li id="<%= x[6] %>" pos="<%= x[8] %>"><%= x[7] %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<input type="text" value="" style="background: #ddf; padding: 5px; width: 370px"/>
<img src="/images/add.png" style="" onclick="add_label(this, 'tracker-<%= t.id %>');"/>
</div>
</div>
</div>
<% end %>
<%= form_for @project, :url => { :action => 'groupissuescustomfields', :id => @project },
:html => {:id => 'groupissuescustomfields-form',
:method => :post} do |f| %>
<%= hidden_field :group_issues_custom_fields, '', :id => 'group_issues_custom_fields', :name => 'group_issues_custom_fields' %>
<p><%= submit_tag l(:button_save), :onclick => "fill_json_data();" %></p>
<% end %>
</div>
<script>
function init_sortables(labelclass) {
$( '.groups_' + labelclass ).sortable({
items: 'div.sortable_group',
connectWith: '.groups_' + labelclass,
start: function(event, ui) {
ui.placeholder.height(ui.item.height());
},
axis: 'y'
});
$( '.sortable_' + labelclass ).sortable({
connectWith: '.sortable_' + labelclass,
update: function(event, ui) {
if (ui.item.parent().hasClass('unsorted')) {
ui.item.parent().prepend(ui.item);
ui.item.parent().children('li').each(function () {
if (parseInt($(this).attr('pos')) < parseInt(ui.item.attr('pos'))) {
ui.item.insertAfter(this);
}
});
}
},
axis: 'y'
}).disableSelection();
}
function refresh_trackers(tracker) {
$('.tracker').hide();
$('#tracker_' + tracker.value).show();
}
function add_label(label, labelclass) {
var old_label = $(label), new_label = $(label).parent().clone();
new_label.children().first().val('').trigger('change');
old_label.attr('src','/images/delete.png').attr('onclick','remove_label(this, "' + labelclass + '");');
old_label.prev().width('360px');
old_label.parent().addClass('ui-sortable-handle sortable_group');
old_label.parent().sortable({
connectWith: '.groups_' + labelclass
}).disableSelection();
old_label.parent().append('<ul id="' + labelclass + '_" class="sortable_items sortable_' + labelclass + '"/>');
old_label.next().sortable({
connectWith: '.sortable_' + labelclass
}).disableSelection();
old_label.parent().parent().append(new_label);
}
function remove_label(label, labelclass) {
$(label).next().children().each(function () {
var item = this;
$('.unsorted.sortable_' + labelclass).prepend(this);
$('.unsorted.sortable_' + labelclass).children('li').each(function () {
if (parseInt($(this).attr('pos')) < parseInt($(item).attr('pos'))) {
$(item).insertAfter(this);
}
});
});
$(label).parent().hide();
}
function fill_json_data() {
var r = {}, gp = 0, cp = 0;
$('#custom_fields_form').children('div').each(function () {
var tracker_id = this.id.split('_').pop();
gp = 0, cp = 0;
// Group 'nil'
r[tracker_id] = {};
r[tracker_id][++gp] = {'cfs': {}};
$(this).children('.nil').children().each(function () {
cp++;
r[tracker_id][gp]['cfs'][cp] = this.id;
});
if (! r[tracker_id][gp]['cfs'][1]) { delete r[tracker_id][gp]; }
$(this).children('.sortable_groups').children().each(function () {
if ($(this).children('ul').length) {
r[tracker_id][++gp] = {
'name': $(this).children('input').val(),
'cfs': {}
};
cp = 0;
$(this).children('ul').children().each(function () {
r[tracker_id][gp]['cfs'][++cp] = this.id;
});
if (! r[tracker_id][gp]['cfs'][1]) { r[tracker_id][gp] = ''; }
}
});
if ( ! r[tracker_id] ) { r[tracker_id] = ''; }
});
console.log(r);
$('#group_issues_custom_fields').val(JSON.stringify(r)); }
$('#tracker_id').change();
<% @project.trackers.each do |t| %>
init_sortables('tracker-<%= t.id %>');
<% end %>
</script>
config/locales/en.yml
description_date_from: Enter start date
description_date_to: Enter end date
text_repository_identifier_info: 'Only lower case letters (a-z), numbers, dashes and underscores are allowed.<br />Once saved, the identifier cannot be changed.'
grouped_cf: Grouped Custom Fields
global_cf_position: Global Custom Fields Position
changed_cf_position: Changed Custom Fields Position
config/locales/pt-BR.yml
label_default_values_for_new_users: Valor padrão para novos usuários
error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted: Spent time cannot
be reassigned to an issue that is about to be deleted
grouped_cf: Agrupar campos personalizados
global_cf_position: Posição global de campos personalizados
changed_cf_position: Alterar posição de campos personalizados
config/routes.rb
member do
get 'settings(/:tab)', :action => 'settings', :as => 'settings'
post 'modules'
post 'groupissuescustomfields'
post 'archive'
post 'unarchive'
post 'close'
db/migrate/20180913211420_create_attribute_groups.rb
class CreateAttributeGroups < ActiveRecord::Migration
def change
create_table :attribute_groups do |t|
t.references :project, index: true, foreign_key: true
t.references :tracker, index: true, foreign_key: true
t.string :name
t.integer :position, :default => 1, :null => false
t.timestamps null: false
end
end
end
db/migrate/20180913212008_create_attribute_group_fields.rb
class CreateAttributeGroupFields < ActiveRecord::Migration
def change
create_table :attribute_group_fields do |t|
t.references :attribute_group, index: true, foreign_key: true
t.references :custom_field, index: true, foreign_key: true
t.integer :position
t.timestamps null: false
end
end
end
lib/redmine.rb
map.permission :view_project, {:projects => [:show], :activities => [:index]}, :public => true, :read => true
map.permission :search_project, {:search => :index}, :public => true, :read => true
map.permission :add_project, {:projects => [:new, :create]}, :require => :loggedin
map.permission :edit_project, {:projects => [:settings, :edit, :update]}, :require => :member
map.permission :edit_project, {:projects => [:settings, :edit, :update, :groupissuescustomfields]}, :require => :member
map.permission :close_project, {:projects => [:close, :reopen]}, :require => :member, :read => true
map.permission :select_project_modules, {:projects => :modules}, :require => :member
map.permission :view_members, {:members => [:index, :show]}, :public => true, :read => true
public/stylesheets/application.css
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;}
div.issue .next-prev-links {color:#999;}
div.issue .attributes {margin-top: 2em;}
div.issue .attribute {padding-left:180px; clear:left; min-height: 1.8em;}
div.issue .attribute .label {width: 170px; margin-left:-180px; font-weight:bold; float:left;}
div.issue .attribute {padding-left:225px; clear:left; min-height: 1.8em;}
div.issue .attribute .label {width: 215px; margin-left:-225px; font-weight:bold; float:left;}
div.issue .attribute .value p {margin: 0 0 0.8em 0;}
div.issue.overdue .due-date .value { color: #c22; }
#issue_tree table.issues, #relations table.issues { border: 0; }
......
height:1px;
overflow:hidden;
}
/* Custom Field Groups */
.sortable_groups {
background: #eee;
border: 1px solid #888;
width: 445px;
min-height: 30px;
margin: 5px;
padding: 5px;
}
.sortable_groups div {
background: #ddf;
border: 1px solid #888;
min-height: 30px;
margin: 5px;
padding: 5px;
}
.sortable_items {
background: #eee;
border: 1px solid #888;
width: 400px;
min-height: 30px;
margin: 5px;
padding: 5px;
}
.sortable_items li {
background: #ffffff;
border: 1px solid #888;
margin: 5px;
padding: 5px;
list-style-type: none;
font-size: 1.2em;
}
.not_a_group {
padding: 5px 27px;
}
.ui-sortable-handle:before {
content:url('/images/reorder.png');
margin-right: 5px;
}
.ui-sortable-placeholder {
background: #8888ff;
border: 1px solid #888;
visibility: visible;
}
test/fixtures/attribute_group_fields.yml
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
attribute_group_id:
custom_field_id:
position: 1
two:
attribute_group_id:
custom_field_id:
position: 1
test/fixtures/attribute_groups.yml
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
project_id:
tracker_id:
name: MyString
position: 1
two:
project_id:
tracker_id:
name: MyString
position: 1
(5-5/24)