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
This commit is contained in:
Jean-Philippe Lang 2007-02-03 12:57:33 +00:00
parent 4a988b0f92
commit 8316e78667
5 changed files with 65 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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]-->

View File

@ -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