diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 60af371..7b0c880 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -162,6 +162,10 @@ class ProjectsController < ApplicationController @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 @@ -194,6 +198,43 @@ class ProjectsController < ApplicationController 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) diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index d9b3b87..a98bac0 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -210,17 +215,32 @@ module IssuesHelper 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 diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 22f7cc3..4a76979 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -27,7 +27,8 @@ module ProjectsHelper {: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 diff --git a/app/models/attribute_group.rb b/app/models/attribute_group.rb new file mode 100644 index 0000000..ca8dc19 --- /dev/null +++ b/app/models/attribute_group.rb @@ -0,0 +1,9 @@ +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 diff --git a/app/models/attribute_group_field.rb b/app/models/attribute_group_field.rb new file mode 100644 index 0000000..f2592cf --- /dev/null +++ b/app/models/attribute_group_field.rb @@ -0,0 +1,8 @@ +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 diff --git a/app/models/project.rb b/app/models/project.rb index c7fc547..26a588e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -50,6 +51,8 @@ class Project < ActiveRecord::Base 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', diff --git a/app/models/tracker.rb b/app/models/tracker.rb index 45c96f9..f9a7372 100644 --- a/app/models/tracker.rb +++ b/app/models/tracker.rb @@ -34,6 +34,8 @@ class Tracker < ActiveRecord::Base 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 diff --git a/app/views/issues/_form_custom_fields.html.erb b/app/views/issues/_form_custom_fields.html.erb index 2e12c00..ad5900d 100644 --- a/app/views/issues/_form_custom_fields.html.erb +++ b/app/views/issues/_form_custom_fields.html.erb @@ -1,16 +1,18 @@ -<% 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? %>
<%= custom_field_tag_with_label :issue, value, :required => @issue.required_attribute?(value.custom_field_id) %>
-<% if i == split_on -%> +<% if i == split_on %><%= select_tag "tracker_id", options_from_collection_for_select(@project.trackers.collect, "id", "name"), {:required => true, :onchange => "refresh_trackers(this);"} %>
+ +
<%= submit_tag l(:button_save), :onclick => "fill_json_data();" %>
+<% end %> +