diff --git a/app/helpers/custom_fields_helper.rb b/app/helpers/custom_fields_helper.rb index 75b8a45c..e473ccdb 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 3626165b..ea11edc4 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 015ccd24..e93dfb4f 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 cf658fb2..36be6259 100644 --- a/app/views/custom_fields/_form.rhtml +++ b/app/views/custom_fields/_form.rhtml @@ -1,6 +1,7 @@ <%= error_messages_for '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 %>
(<%=l(:text_min_max_length_info)%>)

<%= f.text_field :regexp, :size => 50 %>
(<%=l(:text_regexp_info)%>)

-

<%= f.text_area :possible_values, :rows => 5, :cols => 60 %>
(<%=l(:text_possible_values_info)%>)

+

+<% (@custom_field.possible_values.to_a + [""]).each do |value| %> +<%= text_field_tag 'custom_field[possible_values][]', value, :size => 30 %> <%= image_to_function "delete.png", "deleteValueField(this);return false" %>
+<% end %> + +

<%= javascript_tag "toggle_custom_field_format();" %> diff --git a/db/migrate/022_serialize_possibles_values.rb b/db/migrate/022_serialize_possibles_values.rb new file mode 100644 index 00000000..5158f37f --- /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