diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2007-02-03 12:57:33 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2007-02-03 12:57:33 +0000 |
commit | 8316e786672d019699ea472bf83eda5da32820f9 (patch) | |
tree | ecb7c81760c83f9c24da7d82a6198a16da4b37b3 | |
parent | 4a988b0f9223f76dee2252608986a6e3f1a634a6 (diff) | |
download | redmine-8316e786672d019699ea472bf83eda5da32820f9.tar.gz redmine-8316e786672d019699ea472bf83eda5da32820f9.zip |
custom_field.possible_values is now serialized (no longer pipe separated)
git-svn-id: http://redmine.rubyforge.org/svn/trunk@223 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r-- | app/helpers/custom_fields_helper.rb | 2 | ||||
-rw-r--r-- | app/models/custom_field.rb | 21 | ||||
-rw-r--r-- | app/models/custom_value.rb | 2 | ||||
-rw-r--r-- | app/views/custom_fields/_form.rhtml | 37 | ||||
-rw-r--r-- | db/migrate/022_serialize_possibles_values.rb | 13 |
5 files changed, 65 insertions, 10 deletions
diff --git a/app/helpers/custom_fields_helper.rb b/app/helpers/custom_fields_helper.rb index 75b8a45cd..e473ccdbe 100644 --- a/app/helpers/custom_fields_helper.rb +++ b/app/helpers/custom_fields_helper.rb @@ -34,7 +34,7 @@ module CustomFieldsHelper when "bool"
check_box 'custom_value', 'value', :name => field_name, :id => field_id
when "list"
- select 'custom_value', 'value', custom_field.possible_values.split('|'), { :include_blank => true }, :name => field_name, :id => field_id
+ select 'custom_value', 'value', custom_field.possible_values, { :include_blank => true }, :name => field_name, :id => field_id
end
end
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 3626165b5..ea11edc46 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -17,7 +17,8 @@ class CustomField < ActiveRecord::Base
has_many :custom_values, :dependent => :delete_all
-
+ serialize :possible_values
+
FIELD_FORMATS = { "string" => { :name => :label_string, :order => 1 },
"text" => { :name => :label_text, :order => 2 },
"int" => { :name => :label_integer, :order => 3 },
@@ -30,7 +31,23 @@ class CustomField < ActiveRecord::Base validates_uniqueness_of :name
validates_format_of :name, :with => /^[\w\s\'\-]*$/i
validates_inclusion_of :field_format, :in => FIELD_FORMATS.keys
- validates_presence_of :possible_values, :if => Proc.new { |field| field.field_format == "list" }
+
+ def initialize(attributes = nil)
+ super
+ self.possible_values ||= []
+ end
+
+ def before_validation
+ # remove empty values
+ self.possible_values = self.possible_values.collect{|v| v unless v.empty?}.compact
+ end
+
+ def validate
+ if self.field_format == "list"
+ errors.add(:possible_values, :activerecord_error_blank) if self.possible_values.nil? || self.possible_values.empty?
+ errors.add(:possible_values, :activerecord_error_invalid) unless self.possible_values.is_a? Array
+ end
+ end
# to move in project_custom_field
def self.for_all
diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb index 015ccd244..e93dfb4f3 100644 --- a/app/models/custom_value.rb +++ b/app/models/custom_value.rb @@ -31,7 +31,7 @@ protected when "date"
errors.add(:value, :activerecord_error_not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/ or value.empty?
when "list"
- errors.add(:value, :activerecord_error_inclusion) unless custom_field.possible_values.split('|').include? value or value.empty?
+ errors.add(:value, :activerecord_error_inclusion) unless custom_field.possible_values.include? value or value.empty?
end
end end
diff --git a/app/views/custom_fields/_form.rhtml b/app/views/custom_fields/_form.rhtml index cf658fb2a..36be6259a 100644 --- a/app/views/custom_fields/_form.rhtml +++ b/app/views/custom_fields/_form.rhtml @@ -1,6 +1,7 @@ <%= error_messages_for 'custom_field' %> -<script> +<script type="text/javascript"> +//<![CDATA[ function toggle_custom_field_format() { format = $("custom_field_field_format"); p_length = $("custom_field_min_length"); @@ -10,28 +11,47 @@ function toggle_custom_field_format() { case "list": Element.hide(p_length.parentNode); Element.hide(p_regexp.parentNode); - Element.show(p_values.parentNode); + Element.show(p_values); break; case "int": case "string": case "text": Element.show(p_length.parentNode); Element.show(p_regexp.parentNode); - Element.hide(p_values.parentNode); + Element.hide(p_values); break; case "date": case "bool": Element.hide(p_length.parentNode); Element.hide(p_regexp.parentNode); - Element.hide(p_values.parentNode); + Element.hide(p_values); break; default: Element.show(p_length.parentNode); Element.show(p_regexp.parentNode); - Element.show(p_values.parentNode); + Element.show(p_values); break; } } + +function addValueField() { + var f = $$('p#custom_field_possible_values span'); + p = document.getElementById("custom_field_possible_values"); + var v = f[0].cloneNode(true); + v.childNodes[0].value = ""; + p.appendChild(v); +} + +function deleteValueField(e) { + var f = $$('p#custom_field_possible_values span'); + if (f.length == 1) { + e.parentNode.childNodes[0].value = ""; + } else { + Element.remove(e.parentNode); + } +} + +//]]> </script> <!--[form:custom_field]--> @@ -42,7 +62,12 @@ function toggle_custom_field_format() { <%= 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> <p><%= f.text_field :regexp, :size => 50 %><br>(<%=l(:text_regexp_info)%>)</p>
-<p><%= f.text_area :possible_values, :rows => 5, :cols => 60 %><br>(<%=l(:text_possible_values_info)%>)</p> +<p id="custom_field_possible_values"><label><%= l(:field_possible_values) %> <%= image_to_function "add.png", "addValueField();return false" %></label> +<% (@custom_field.possible_values.to_a + [""]).each do |value| %> +<span><%= text_field_tag 'custom_field[possible_values][]', value, :size => 30 %> <%= image_to_function "delete.png", "deleteValueField(this);return false" %><br /></span> +<% end %> + +</p> </div>
<%= javascript_tag "toggle_custom_field_format();" %> <!--[eoform:custom_field]--> diff --git a/db/migrate/022_serialize_possibles_values.rb b/db/migrate/022_serialize_possibles_values.rb new file mode 100644 index 000000000..5158f37fd --- /dev/null +++ b/db/migrate/022_serialize_possibles_values.rb @@ -0,0 +1,13 @@ +class SerializePossiblesValues < ActiveRecord::Migration + def self.up + CustomField.find(:all).each do |field| + if field.possible_values and field.possible_values.is_a? String + field.possible_values = field.possible_values.split('|') + field.save + end + end + end + + def self.down + end +end |