Project

General

Profile

Feature #1189 » multi_values_for_r3739.diff

Minjie Zhu, 2010-05-17 14:52

View differences:

app/controllers/issues_controller.rb (working copy)
@edit_allowed = User.current.allowed_to?(:edit_issues, @project)
@priorities = IssuePriority.all
@time_entry = TimeEntry.new
@custom_values = @issue.custom_field_values
respond_to do |format|
format.html { render :template => 'issues/show.rhtml' }
format.xml { render :layout => false }
......
@edit_allowed = User.current.allowed_to?(:edit_issues, @project)
@time_entry = TimeEntry.new
@custom_values = @issue.custom_field_values
@notes = params[:notes]
@issue.init_journal(User.current, @notes)
# User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
app/helpers/custom_fields_helper.rb (working copy)
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 + options_for_select(custom_field.possible_values, custom_value.value), :id => field_id)
multi_image = custom_field.allow_multi ?
link_to_function(image_tag('bullet_toggle_plus.png'), "toggle_multi_custom('#{custom_field.id}');", :style => "vertical-align: bottom;") :
''
multiple = custom_field.allow_multi && custom_value.value.is_a?(Array) && custom_value.value.length > 1
select_name = custom_field.allow_multi ? "#{name}[custom_multi_values][#{custom_field.id}][]" : field_name
select_tag(select_name, blank_option + options_for_select(custom_field.possible_values, custom_value.value), :id => field_id, :multiple => multiple) + multi_image
else
text_field_tag(field_name, custom_value.value, :id => field_id)
end
......
# Return a string used to display a custom value
def show_value(custom_value)
return "" unless custom_value
format_value(custom_value.value, custom_value.custom_field.field_format)
format_value((custom_value.value.is_a?(Array) ? custom_value.value.join("\n") : custom_value.value), custom_value.custom_field.field_format)
end
# Return a string used to display a custom value
app/models/custom_field.rb (working copy)
def before_validation
# make sure these fields are not searchable
self.searchable = false if %w(int float date bool).include?(field_format)
# make sure only list field_format have allow_multi option
self.allow_multi = false unless field_format == 'list'
true
end
app/models/custom_value.rb (working copy)
end
end
end
class CustomValuesCollection < Array
attr_accessor :custom_field
def initialize(custom_field, custom_values=[])
@custom_field = custom_field if custom_field.is_a?(CustomField)
custom_values.map{ |x| self << x }
self
end
def value
self.uniq.map(&:value).delete_if {|x| x.blank?}
end
def value=(new_value)
self.delete_if{ |x| true }
new_value.map{ |x| self << x }
end
def save
self.compact.each(&:save)
end
def valid?
self.inject(true){ |bool,v| bool && v.valid? }
end
def validate
self.uniq.map(&:validate)
end
def custom_field_id
@custom_field.id
end
def method_missing(symbol, *args)
if @custom_field.respond_to?(symbol)
@custom_field.send(symbol, *args)
elsif self.first && self.first.respond_to?(symbol)
self.first.send(symbol, *args)
else
super
end
end
end
app/models/issue.rb (working copy)
due_date
done_ratio
estimated_hours
custom_multi_values
custom_field_values
lock_version
) unless const_defined?(:SAFE_ATTRIBUTES)
......
@issue_before_change = self.clone
@issue_before_change.status = self.status
@custom_values_before_change = {}
self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
custom_field_values.each {|c| @custom_values_before_change.store c.custom_field.id, c.value }
# Make sure updated_on is updated when adding a note.
updated_on_will_change!
@current_journal
......
:value => send(c)) unless send(c)==@issue_before_change.send(c)
}
# custom fields changes
custom_values.each {|c|
custom_field_values.each {|c|
next if (@custom_values_before_change[c.custom_field_id]==c.value ||
(@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
@current_journal.details << JournalDetail.new(:property => 'cf',
app/models/journal_detail.rb (working copy)
belongs_to :journal
def before_save
self.value = value.join(", ") if value && value.is_a?(Array)
self.old_value = old_value.join(", ") if old_value && old_value.is_a?(Array)
self.value = value[0..254] if value && value.is_a?(String)
self.old_value = old_value[0..254] if old_value && old_value.is_a?(String)
end
app/views/custom_fields/_form.rhtml (working copy)
function toggle_custom_field_format() {
format = $("custom_field_field_format");
p_length = $("custom_field_min_length");
p_multi = $("custom_field_allow_multi");
p_regexp = $("custom_field_regexp");
p_values = $("custom_field_possible_values");
p_searchable = $("custom_field_searchable");
......
Element.hide(p_regexp.parentNode);
if (p_searchable) Element.show(p_searchable.parentNode);
Element.show(p_values);
Element.show(p_multi.parentNode);
break;
case "bool":
p_default.setAttribute('type','checkbox');
......
Element.hide(p_regexp.parentNode);
if (p_searchable) Element.hide(p_searchable.parentNode);
Element.hide(p_values);
Element.hide(p_multi.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);
Element.hide(p_multi.parentNode);
break;
case "float":
case "int":
......
Element.show(p_regexp.parentNode);
if (p_searchable) Element.hide(p_searchable.parentNode);
Element.hide(p_values);
Element.hide(p_multi.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);
Element.hide(p_multi.parentNode);
break;
}
}
......
<p><%= f.check_box :is_for_all %></p>
<p><%= f.check_box :is_filter %></p>
<p><%= f.check_box :searchable %></p>
<p><%= f.check_box :allow_multi %></p>
<% when "UserCustomField" %>
<p><%= f.check_box :is_required %></p>
app/views/issues/_form_custom_fields.rhtml (working copy)
<script type="text/javascript">
//<![CDATA[
function toggle_multi_custom(field) {
select = $('issue_custom_field_values_' + field);
if (select.multiple == true) {
select.multiple = false;
} else {
select.multiple = true;
}
}
//]]>
</script>
<div class="splitcontentleft">
<% i = 0 %>
<% split_on = (@issue.custom_field_values.size / 2.0).ceil - 1 %>
config/locales/en.yml (working copy)
field_column_names: Columns
field_time_zone: Time zone
field_searchable: Searchable
field_allow_multi: Allow multiple choices
field_default_value: Default value
field_comments_sorting: Display comments
field_parent_title: Parent page
config/locales/fr.yml (working copy)
field_column_names: Colonnes
field_time_zone: Fuseau horaire
field_searchable: Utilisé pour les recherches
field_allow_multi: Permettre les choix multiples
field_default_value: Valeur par défaut
field_comments_sorting: Afficher les commentaires
field_parent_title: Page parent
config/locales/ja.yml (working copy)
field_column_names: 項目
field_time_zone: タイムゾーン
field_searchable: 検索条件に設定可能とする
field_allow_multi: 複数選択可能
field_default_value: デフォルト値
field_comments_sorting: コメントを表示
field_parent_title: 親ページ
config/locales/zh-TW.yml (working copy)
field_column_names: 欄位
field_time_zone: 時區
field_searchable: 可用做搜尋條件
field_allow_multi: 允許多重選擇
field_default_value: 預設值
field_comments_sorting: 註解排序
field_parent_title: 父頁面
config/locales/zh.yml (working copy)
field_column_names: 列
field_time_zone: 时区
field_searchable: 可用作搜索条件
field_allow_multi: 允许多选
field_default_value: 默认值
field_comments_sorting: 显示注释
field_parent_title: 上级页面
db/migrate/20100512172200_add_custom_fields_multi.rb (revision 0)
class AddCustomFieldsMulti < ActiveRecord::Migration
def self.up
add_column :custom_fields, :allow_multi, :boolean, :default => false
end
def self.down
remove_column :custom_fields, :allow_multi
end
end
test/unit/custom_value_test.rb (working copy)
assert v.valid?
end
def test_multi_list_field_validation
f = CustomField.new(:field_format => 'list', :allow_multi => true, :possible_values => ['value1', 'value2'])
v = CustomValuesCollection.new(:custom_field => f, :value => [])
assert v.valid?
v << CustomValue.new(:custom_field => f, :value => 'value1')
assert v.valid?
v << CustomValue.new(:custom_field => f, :value => 'value2')
assert v.valid?
v << CustomValue.new(:custom_field => f, :value => 'abc')
assert !v.valid?
end
def test_multi_list_field_value
f = CustomField.new(:field_format => 'list', :allow_multi => true, :possible_values => ['value1', 'value2'])
v = CustomValuesCollection.new(:custom_field => f, :value => [])
v << CustomValue.new(:custom_field => f, :value => 'value1')
c = CustomValue.new(:custom_field => f, :value => 'value2')
v << c
assert_equal ['value1', 'value2'], v.value
v << c
assert ['value1', 'value2'], v.value
end
def test_int_field_validation
f = CustomField.new(:field_format => 'int')
v = CustomValue.new(:custom_field => f, :value => '')
vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb (working copy)
@custom_field_values_changed = true
values = values.stringify_keys
custom_field_values.each do |custom_value|
custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s)
if custom_value.is_a? CustomValuesCollection
if custom_value.empty?
values.each do |key, value|
if (custom_value.custom_field_id == key.to_i && value.is_a?(Array))
if value.empty?
custom_value << custom_values.build(:custom_field => custom_value.custom_field, :value => nil)
else
value.each do |v|
custom_value << custom_values.build(:custom_field => custom_value.custom_field, :value => v)
end
end
end
end
end
CustomValue # otherwise Rails doesn't know the CustomValuesCollection class
custom_value = CustomValuesCollection.new custom_value.custom_field, custom_value
#end
else
custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s)
end
end if values.is_a?(Hash)
end
def custom_field_values
@custom_field_values ||= available_custom_fields.collect { |x| custom_values.detect { |v| v.custom_field == x } || custom_values.build(:custom_field => x, :value => nil) }
@custom_field_values ||= available_custom_fields.collect do |x|
if x.allow_multi
CustomValue # otherwise Rails doesn't know the CustomValuesCollection class
CustomValuesCollection.new x, custom_values.select{ |v| v.custom_field == x }
else
custom_values.detect { |v| v.custom_field == x } || custom_values.build(:custom_field => x, :value => nil)
end
end
end
def custom_multi_values=(values)
values.each do |key, value|
value.delete_if {|v| v.to_s == ""} if value.length > 1
end
@old_custom_values ||= custom_values.select{ |x| x.custom_field.allow_multi }
@custom_field_values_changed = true
values = values.stringify_keys
values.each do |key, value|
custom_value = custom_field_values.detect{ |c| c.custom_field.id == key.to_i && c.allow_multi }
value.each do |v|
old = @old_custom_values.detect{ |u| u.custom_field == custom_value.custom_field && u.value == v }
if old.blank?
custom_value << custom_values.build(:custom_field => custom_value.custom_field, :value => v)
else
custom_value << old unless custom_value.include?(old)
@old_custom_values.delete old
end
end if values.is_a?(Hash) && custom_value != nil && values.has_key?(custom_value.custom_field.id.to_s)
end
#delete old normal values
@custom_field_values.each { |c| c.delete_if{ |x| @old_custom_values.include?(x) } if c.is_a?(CustomValuesCollection) }
@custom_values.delete_if { |c| @old_custom_values.include?(c) }
end
def custom_field_values_changed?
......
def save_custom_field_values
custom_field_values.each(&:save)
@old_custom_values.each(&:destroy) unless @old_custom_values.blank?
@custom_field_values_changed = false
@custom_field_values = nil
end
(1-1/8)