Adds support for multiselect custom fields (#1189).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8721 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
@@ -36,6 +36,7 @@ module CustomFieldsHelper
|
||||
def custom_field_tag(name, custom_value)
|
||||
custom_field = custom_value.custom_field
|
||||
field_name = "#{name}[custom_field_values][#{custom_field.id}]"
|
||||
field_name << "[]" if custom_field.multiple?
|
||||
field_id = "#{name}_custom_field_values_#{custom_field.id}"
|
||||
|
||||
field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
|
||||
@@ -48,10 +49,22 @@ module CustomFieldsHelper
|
||||
when "bool"
|
||||
hidden_field_tag(field_name, '0') + check_box_tag(field_name, '1', custom_value.true?, :id => field_id)
|
||||
when "list"
|
||||
blank_option = custom_field.is_required? ?
|
||||
(custom_field.default_value.blank? ? "<option value=\"\">--- #{l(:actionview_instancetag_blank_option)} ---</option>" : '') :
|
||||
'<option></option>'
|
||||
select_tag(field_name, blank_option.html_safe + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value), :id => field_id)
|
||||
blank_option = ''
|
||||
unless custom_field.multiple?
|
||||
if custom_field.is_required?
|
||||
unless custom_field.default_value.present?
|
||||
blank_option = "<option value=\"\">--- #{l(:actionview_instancetag_blank_option)} ---</option>"
|
||||
end
|
||||
else
|
||||
blank_option = '<option></option>'
|
||||
end
|
||||
end
|
||||
s = select_tag(field_name, blank_option.html_safe + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value),
|
||||
:id => field_id, :multiple => custom_field.multiple?)
|
||||
if custom_field.multiple?
|
||||
s << hidden_field_tag(field_name, '')
|
||||
end
|
||||
s
|
||||
else
|
||||
text_field_tag(field_name, custom_value.value, :id => field_id)
|
||||
end
|
||||
@@ -71,6 +84,7 @@ module CustomFieldsHelper
|
||||
|
||||
def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil)
|
||||
field_name = "#{name}[custom_field_values][#{custom_field.id}]"
|
||||
field_name << "[]" if custom_field.multiple?
|
||||
field_id = "#{name}_custom_field_values_#{custom_field.id}"
|
||||
field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
|
||||
case field_format.try(:edit_as)
|
||||
@@ -84,7 +98,11 @@ module CustomFieldsHelper
|
||||
[l(:general_text_yes), '1'],
|
||||
[l(:general_text_no), '0']]), :id => field_id)
|
||||
when "list"
|
||||
select_tag(field_name, options_for_select([[l(:label_no_change_option), '']] + custom_field.possible_values_options(projects)), :id => field_id)
|
||||
options = []
|
||||
options << [l(:label_no_change_option), ''] unless custom_field.multiple?
|
||||
options += custom_field.possible_values_options(projects)
|
||||
select_tag(field_name, options_for_select(options),
|
||||
:id => field_id, :multiple => custom_field.multiple?)
|
||||
else
|
||||
text_field_tag(field_name, '', :id => field_id)
|
||||
end
|
||||
@@ -98,7 +116,11 @@ module CustomFieldsHelper
|
||||
|
||||
# Return a string used to display a custom value
|
||||
def format_value(value, field_format)
|
||||
Redmine::CustomFieldFormat.format_value(value, field_format) # Proxy
|
||||
if value.is_a?(Array)
|
||||
value.collect {|v| format_value(v, field_format)}.join(', ')
|
||||
else
|
||||
Redmine::CustomFieldFormat.format_value(value, field_format)
|
||||
end
|
||||
end
|
||||
|
||||
# Return an array of custom field formats which can be used in select_tag
|
||||
@@ -110,8 +132,18 @@ module CustomFieldsHelper
|
||||
def render_api_custom_values(custom_values, api)
|
||||
api.array :custom_fields do
|
||||
custom_values.each do |custom_value|
|
||||
api.custom_field :id => custom_value.custom_field_id, :name => custom_value.custom_field.name do
|
||||
api.value custom_value.value
|
||||
attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
|
||||
attrs.merge!(:multiple => true) if custom_value.custom_field.multiple?
|
||||
api.custom_field attrs do
|
||||
if custom_value.value.is_a?(Array)
|
||||
api.array :value do
|
||||
custom_value.value.each do |value|
|
||||
api.value value unless value.blank?
|
||||
end
|
||||
end
|
||||
else
|
||||
api.value custom_value.value
|
||||
end
|
||||
end
|
||||
end
|
||||
end unless custom_values.empty?
|
||||
|
||||
@@ -161,7 +161,44 @@ module IssuesHelper
|
||||
out
|
||||
end
|
||||
|
||||
# Returns the textual representation of a journal details
|
||||
# as an array of strings
|
||||
def details_to_strings(details, no_html=false)
|
||||
strings = []
|
||||
values_by_field = {}
|
||||
details.each do |detail|
|
||||
if detail.property == 'cf'
|
||||
field_id = detail.prop_key
|
||||
field = CustomField.find_by_id(field_id)
|
||||
if field && field.multiple?
|
||||
values_by_field[field_id] ||= {:added => [], :deleted => []}
|
||||
if detail.old_value
|
||||
values_by_field[field_id][:deleted] << detail.old_value
|
||||
end
|
||||
if detail.value
|
||||
values_by_field[field_id][:added] << detail.value
|
||||
end
|
||||
next
|
||||
end
|
||||
end
|
||||
strings << show_detail(detail, no_html)
|
||||
end
|
||||
values_by_field.each do |field_id, changes|
|
||||
detail = JournalDetail.new(:property => 'cf', :prop_key => field_id)
|
||||
if changes[:added].any?
|
||||
detail.value = changes[:added]
|
||||
strings << show_detail(detail, no_html)
|
||||
elsif changes[:deleted].any?
|
||||
detail.old_value = changes[:deleted]
|
||||
strings << show_detail(detail, no_html)
|
||||
end
|
||||
end
|
||||
strings
|
||||
end
|
||||
|
||||
# Returns the textual representation of a single journal detail
|
||||
def show_detail(detail, no_html=false)
|
||||
multiple = false
|
||||
case detail.property
|
||||
when 'attr'
|
||||
field = detail.prop_key.to_s.gsub(/\_id$/, "")
|
||||
@@ -192,6 +229,7 @@ module IssuesHelper
|
||||
when 'cf'
|
||||
custom_field = CustomField.find_by_id(detail.prop_key)
|
||||
if custom_field
|
||||
multiple = custom_field.multiple?
|
||||
label = custom_field.name
|
||||
value = format_value(detail.value, custom_field.field_format) if detail.value
|
||||
old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
|
||||
@@ -232,6 +270,8 @@ module IssuesHelper
|
||||
when 'attr', 'cf'
|
||||
if !detail.old_value.blank?
|
||||
l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
|
||||
elsif multiple
|
||||
l(:text_journal_added, :label => label, :value => value).html_safe
|
||||
else
|
||||
l(:text_journal_set_to, :label => label, :value => value).html_safe
|
||||
end
|
||||
|
||||
@@ -31,7 +31,14 @@ module QueriesHelper
|
||||
|
||||
def column_content(column, issue)
|
||||
value = column.value(issue)
|
||||
|
||||
if value.is_a?(Array)
|
||||
value.collect {|v| column_value(column, issue, v)}.compact.sort.join(', ')
|
||||
else
|
||||
column_value(column, issue, value)
|
||||
end
|
||||
end
|
||||
|
||||
def column_value(column, issue, value)
|
||||
case value.class.name
|
||||
when 'String'
|
||||
if column.name == :subject
|
||||
|
||||
@@ -38,6 +38,8 @@ class CustomField < ActiveRecord::Base
|
||||
def set_searchable
|
||||
# make sure these fields are not searchable
|
||||
self.searchable = false if %w(int float date bool).include?(field_format)
|
||||
# make sure only these fields can have multiple values
|
||||
self.multiple = false unless %w(list user version).include?(field_format)
|
||||
true
|
||||
end
|
||||
|
||||
@@ -123,6 +125,7 @@ class CustomField < ActiveRecord::Base
|
||||
# objects by their value of the custom field.
|
||||
# Returns false, if the custom field can not be used for sorting.
|
||||
def order_statement
|
||||
return nil if multiple?
|
||||
case field_format
|
||||
when 'string', 'text', 'list', 'date', 'bool'
|
||||
# COALESCE is here to make sure that blank and NULL values are sorted equally
|
||||
@@ -161,14 +164,24 @@ class CustomField < ActiveRecord::Base
|
||||
nil
|
||||
end
|
||||
|
||||
# Returns the error message for the given value
|
||||
# Returns the error messages for the given value
|
||||
# or an empty array if value is a valid value for the custom field
|
||||
def validate_field_value(value)
|
||||
errs = []
|
||||
if is_required? && value.blank?
|
||||
errs << ::I18n.t('activerecord.errors.messages.blank')
|
||||
if value.is_a?(Array)
|
||||
if !multiple?
|
||||
errs << ::I18n.t('activerecord.errors.messages.invalid')
|
||||
end
|
||||
if is_required? && value.detect(&:present?).nil?
|
||||
errs << ::I18n.t('activerecord.errors.messages.blank')
|
||||
end
|
||||
value.each {|v| errs += validate_field_value_format(v)}
|
||||
else
|
||||
if is_required? && value.blank?
|
||||
errs << ::I18n.t('activerecord.errors.messages.blank')
|
||||
end
|
||||
errs += validate_field_value_format(value)
|
||||
end
|
||||
errs += validate_field_value_format(value)
|
||||
errs
|
||||
end
|
||||
|
||||
|
||||
+27
-6
@@ -429,7 +429,7 @@ class Issue < ActiveRecord::Base
|
||||
else
|
||||
@attributes_before_change = attributes.dup
|
||||
@custom_values_before_change = {}
|
||||
self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
|
||||
self.custom_field_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
|
||||
end
|
||||
# Make sure updated_on is updated when adding a note.
|
||||
updated_on_will_change!
|
||||
@@ -1006,14 +1006,35 @@ class Issue < ActiveRecord::Base
|
||||
end
|
||||
if @custom_values_before_change
|
||||
# custom fields changes
|
||||
custom_values.each {|c|
|
||||
custom_field_values.each {|c|
|
||||
before = @custom_values_before_change[c.custom_field_id]
|
||||
after = c.value
|
||||
next if before == after || (before.blank? && after.blank?)
|
||||
@current_journal.details << JournalDetail.new(:property => 'cf',
|
||||
:prop_key => c.custom_field_id,
|
||||
:old_value => before,
|
||||
:value => after)
|
||||
|
||||
if before.is_a?(Array) || after.is_a?(Array)
|
||||
before = [before] unless before.is_a?(Array)
|
||||
after = [after] unless after.is_a?(Array)
|
||||
|
||||
# values removed
|
||||
(before - after).reject(&:blank?).each do |value|
|
||||
@current_journal.details << JournalDetail.new(:property => 'cf',
|
||||
:prop_key => c.custom_field_id,
|
||||
:old_value => value,
|
||||
:value => nil)
|
||||
end
|
||||
# values added
|
||||
(after - before).reject(&:blank?).each do |value|
|
||||
@current_journal.details << JournalDetail.new(:property => 'cf',
|
||||
:prop_key => c.custom_field_id,
|
||||
:old_value => nil,
|
||||
:value => value)
|
||||
end
|
||||
else
|
||||
@current_journal.details << JournalDetail.new(:property => 'cf',
|
||||
:prop_key => c.custom_field_id,
|
||||
:old_value => before,
|
||||
:value => after)
|
||||
end
|
||||
}
|
||||
end
|
||||
@current_journal.save
|
||||
|
||||
+10
-4
@@ -57,7 +57,7 @@ class QueryCustomFieldColumn < QueryColumn
|
||||
def initialize(custom_field)
|
||||
self.name = "cf_#{custom_field.id}".to_sym
|
||||
self.sortable = custom_field.order_statement || false
|
||||
if %w(list date bool int).include?(custom_field.field_format)
|
||||
if %w(list date bool int).include?(custom_field.field_format) && !custom_field.multiple?
|
||||
self.groupable = custom_field.order_statement
|
||||
end
|
||||
self.groupable ||= false
|
||||
@@ -73,8 +73,8 @@ class QueryCustomFieldColumn < QueryColumn
|
||||
end
|
||||
|
||||
def value(issue)
|
||||
cv = issue.custom_values.detect {|v| v.custom_field_id == @cf.id}
|
||||
cv && @cf.cast_value(cv.value)
|
||||
cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)}
|
||||
cv.size > 1 ? cv : cv.first
|
||||
end
|
||||
|
||||
def css_classes
|
||||
@@ -694,7 +694,13 @@ class Query < ActiveRecord::Base
|
||||
value.push User.current.id.to_s
|
||||
end
|
||||
end
|
||||
"#{Issue.table_name}.id IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " +
|
||||
not_in = nil
|
||||
if operator == '!'
|
||||
# Makes ! operator work for custom fields with multiple values
|
||||
operator = '='
|
||||
not_in = 'NOT'
|
||||
end
|
||||
"#{Issue.table_name}.id #{not_in} IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " +
|
||||
sql_for_field(field, operator, value, db_table, db_field, true) + ')'
|
||||
end
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ function toggle_custom_field_format() {
|
||||
p_values = $("custom_field_possible_values");
|
||||
p_searchable = $("custom_field_searchable");
|
||||
p_default = $("custom_field_default_value");
|
||||
p_multiple = $("custom_field_multiple");
|
||||
|
||||
p_default.setAttribute('type','text');
|
||||
Element.show(p_default.parentNode);
|
||||
@@ -19,6 +20,7 @@ function toggle_custom_field_format() {
|
||||
Element.hide(p_regexp.parentNode);
|
||||
if (p_searchable) Element.show(p_searchable.parentNode);
|
||||
Element.show(p_values.parentNode);
|
||||
Element.show(p_multiple.parentNode);
|
||||
break;
|
||||
case "bool":
|
||||
p_default.setAttribute('type','checkbox');
|
||||
@@ -26,12 +28,14 @@ function toggle_custom_field_format() {
|
||||
Element.hide(p_regexp.parentNode);
|
||||
if (p_searchable) Element.hide(p_searchable.parentNode);
|
||||
Element.hide(p_values.parentNode);
|
||||
Element.hide(p_multiple.parentNode);
|
||||
break;
|
||||
case "date":
|
||||
Element.hide(p_length.parentNode);
|
||||
Element.hide(p_regexp.parentNode);
|
||||
if (p_searchable) Element.hide(p_searchable.parentNode);
|
||||
Element.hide(p_values.parentNode);
|
||||
Element.hide(p_multiple.parentNode);
|
||||
break;
|
||||
case "float":
|
||||
case "int":
|
||||
@@ -39,6 +43,7 @@ function toggle_custom_field_format() {
|
||||
Element.show(p_regexp.parentNode);
|
||||
if (p_searchable) Element.hide(p_searchable.parentNode);
|
||||
Element.hide(p_values.parentNode);
|
||||
Element.hide(p_multiple.parentNode);
|
||||
break;
|
||||
case "user":
|
||||
case "version":
|
||||
@@ -47,12 +52,14 @@ function toggle_custom_field_format() {
|
||||
if (p_searchable) Element.hide(p_searchable.parentNode);
|
||||
Element.hide(p_values.parentNode);
|
||||
Element.hide(p_default.parentNode);
|
||||
Element.show(p_multiple.parentNode);
|
||||
break;
|
||||
default:
|
||||
Element.show(p_length.parentNode);
|
||||
Element.show(p_regexp.parentNode);
|
||||
if (p_searchable) Element.show(p_searchable.parentNode);
|
||||
Element.hide(p_values.parentNode);
|
||||
Element.hide(p_multiple.parentNode);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -64,6 +71,7 @@ function toggle_custom_field_format() {
|
||||
<p><%= f.text_field :name, :required => true %></p>
|
||||
<p><%= f.select :field_format, custom_field_formats_for_select(@custom_field), {}, :onchange => "toggle_custom_field_format();",
|
||||
:disabled => !@custom_field.new_record? %></p>
|
||||
<p><%= f.check_box :multiple, :disabled => !@custom_field.new_record? %></p>
|
||||
<p><label for="custom_field_min_length"><%=l(:label_min_max_length)%></label>
|
||||
<%= f.text_field :min_length, :size => 5, :no_label => true %> -
|
||||
<%= f.text_field :max_length, :size => 5, :no_label => true %><br />(<%=l(:text_min_max_length_info)%>)</p>
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
|
||||
<% if journal.details.any? %>
|
||||
<ul class="details">
|
||||
<% for detail in journal.details %>
|
||||
<li><%= show_detail(detail) %></li>
|
||||
<% details_to_strings(journal.details).each do |string| %>
|
||||
<li><%= string %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
|
||||
@@ -19,8 +19,8 @@ xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
|
||||
end
|
||||
xml.content "type" => "html" do
|
||||
xml.text! '<ul>'
|
||||
change.details.each do |detail|
|
||||
xml.text! '<li>' + show_detail(detail, false) + '</li>'
|
||||
details_to_strings(change.details, false).each do |string|
|
||||
xml.text! '<li>' + string + '</li>'
|
||||
end
|
||||
xml.text! '</ul>'
|
||||
xml.text! textilizable(change, :notes, :only_path => false) unless change.notes.blank?
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<%= l(:text_issue_updated, :id => "##{@issue.id}", :author => h(@journal.user)) %>
|
||||
|
||||
<ul>
|
||||
<% for detail in @journal.details %>
|
||||
<li><%= show_detail(detail, true) %></li>
|
||||
<% details_to_strings(@journal.details).each do |string| %>
|
||||
<li><%= string %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<%= l(:text_issue_updated, :id => "##{@issue.id}", :author => @journal.user) %>
|
||||
|
||||
<% for detail in @journal.details -%>
|
||||
<%= show_detail(detail, true) %>
|
||||
<% details_to_strings(@journal.details, true).each do |string| -%>
|
||||
<%= string %>
|
||||
<% end -%>
|
||||
|
||||
<%= @journal.notes if @journal.notes? %>
|
||||
|
||||
Reference in New Issue
Block a user