Skip to content

Commit

Permalink
custom field tags
Browse files Browse the repository at this point in the history
git-svn-id: http://redmine.rubyforge.org/svn/trunk@20 e93f8b46-1217-0410-a6f0-8f06a7374b81
  • Loading branch information
jplang committed Jul 31, 2006
1 parent d2b89f5 commit 54aeec7
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion redmine/app/controllers/projects_controller.rb
Expand Up @@ -80,7 +80,7 @@ def settings
@member ||= @project.members.new
@roles = Role.find_all
@users = User.find_all - @project.members.find(:all, :include => :user).collect{|m| m.user }
@custom_values = ProjectCustomField.find(:all).collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
@custom_values ||= ProjectCustomField.find(:all).collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
end

# Edit @project
Expand Down
20 changes: 15 additions & 5 deletions redmine/app/helpers/application_helper.rb
Expand Up @@ -17,16 +17,17 @@

module ApplicationHelper

# return current logged in user or nil
# Return current logged in user or nil
def loggedin?
@logged_in_user
end

# return true if user is loggend in and is admin, otherwise false
# Return true if user is logged in and is admin, otherwise false
def admin_loggedin?
@logged_in_user and @logged_in_user.admin?
end

# Return true if user is authorized for controller/action, otherwise false
def authorize_for(controller, action)
# check if action is allowed on public projects
if @project.is_public? and Permission.allowed_to_public "%s/%s" % [ controller, action ]
Expand Down Expand Up @@ -76,12 +77,21 @@ def error_messages_for(object_name, options = {})
if attr == "base"
full_messages << l(msg)
else
full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg)
full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg) unless attr == "custom_values"
end
end
end
# retrieve custom values error messages
if object.errors[:custom_values]
object.custom_values.each do |v|
v.errors.each do |attr, msg|
next if msg.nil?
full_messages << "&#171; " + v.custom_field.name + " &#187; " + l(msg)
end
end
end
content_tag("div",
content_tag(
options[:header_tag] || "h2", lwr(:gui_validation_error, object.errors.count) + " :"
options[:header_tag] || "h2", lwr(:gui_validation_error, full_messages.length) + " :"
) +
content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
"id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
Expand Down
17 changes: 10 additions & 7 deletions redmine/app/helpers/custom_fields_helper.rb
Expand Up @@ -17,41 +17,43 @@

module CustomFieldsHelper

# Return custom field html tag corresponding to its format
def custom_field_tag(custom_value)
custom_field = custom_value.custom_field
field_name = "custom_fields[#{custom_field.id}]"
field_id = "custom_fields_#{custom_field.id}"

case custom_field.field_format
when "string", "int", "date"
text_field_tag field_name, custom_value.value, :id => field_id
text_field 'custom_value', 'value', :name => field_name, :id => field_id
when "text"
text_area_tag field_name, custom_value.value, :id => field_id, :cols => 60, :rows => 3
text_area 'custom_value', 'value', :name => field_name, :id => field_id, :cols => 60, :rows => 3
when "bool"
check_box_tag(field_name, "1", custom_value.value == "1", :id => field_id) +
hidden_field_tag(field_name, "0")
check_box 'custom_value', 'value', :name => field_name, :id => field_id
when "list"
select_tag field_name,
"<option></option>" + options_for_select(custom_field.possible_values.split('|'),
custom_value.value), :id => field_id
select 'custom_value', 'value', custom_field.possible_values.split('|'), { :include_blank => true }, :name => field_name, :id => field_id
end
end

# Return custom field label tag
def custom_field_label_tag(custom_value)
content_tag "label", custom_value.custom_field.name +
(custom_value.custom_field.is_required? ? " <span class=\"required\">*</span>" : ""),
:for => "custom_fields_#{custom_value.custom_field.id}"
end

# Return custom field tag with its label tag
def custom_field_tag_with_label(custom_value)
case custom_value.custom_field.field_format
when "bool"
# label is displayed inline after the checkbox
custom_field_tag(custom_value) + " " + custom_field_label_tag(custom_value)
else
custom_field_label_tag(custom_value) + "<br />" + custom_field_tag(custom_value)
end
end

# Return a string used to display a custom value
def show_value(custom_value)
case custom_value.custom_field.field_format
when "bool"
Expand All @@ -61,6 +63,7 @@ def show_value(custom_value)
end
end

# Return an array of custom field formats which can be used in select_tag
def custom_field_formats_for_select
CustomField::FIELD_FORMATS.keys.collect { |k| [ l(CustomField::FIELD_FORMATS[k]), k ] }
end
Expand Down
19 changes: 7 additions & 12 deletions redmine/app/models/custom_value.rb
Expand Up @@ -21,22 +21,17 @@ class CustomValue < ActiveRecord::Base

protected
def validate
# errors are added to customized object unless it's nil
object = customized || self

object.errors.add(custom_field.name, :activerecord_error_blank) if custom_field.is_required? and value.empty?
object.errors.add(custom_field.name, :activerecord_error_invalid) unless custom_field.regexp.empty? or value =~ Regexp.new(custom_field.regexp)

object.errors.add(custom_field.name, :activerecord_error_too_short) if custom_field.min_length > 0 and value.length < custom_field.min_length and value.length > 0
object.errors.add(custom_field.name, :activerecord_error_too_long) if custom_field.max_length > 0 and value.length > custom_field.max_length

errors.add(:value, :activerecord_error_blank) and return if custom_field.is_required? and value.empty?
errors.add(:value, :activerecord_error_invalid) unless custom_field.regexp.empty? or value =~ Regexp.new(custom_field.regexp)
errors.add(:value, :activerecord_error_too_short) if custom_field.min_length > 0 and value.length < custom_field.min_length and value.length > 0
errors.add(:value, :activerecord_error_too_long) if custom_field.max_length > 0 and value.length > custom_field.max_length
case custom_field.field_format
when "int"
object.errors.add(custom_field.name, :activerecord_error_not_a_number) unless value =~ /^[0-9]*$/
errors.add(:value, :activerecord_error_not_a_number) unless value =~ /^[0-9]*$/
when "date"
object.errors.add(custom_field.name, :activerecord_error_invalid) unless value =~ /^(\d+)\/(\d+)\/(\d+)$/ or value.empty?
errors.add(:value, :activerecord_error_invalid) unless value =~ /^(\d+)\/(\d+)\/(\d+)$/ or value.empty?
when "list"
object.errors.add(custom_field.name, :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.split('|').include? value or value.empty?
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions redmine/app/views/account/register.rhtml
Expand Up @@ -26,8 +26,8 @@
<p><label for="user_language"><%=l(:field_language)%></label><br/>
<%= select("user", "language", lang_options_for_select) %></p>

<% for custom_value in @custom_values %>
<p><%= custom_field_tag_with_label custom_value %></p>
<% for @custom_value in @custom_values %>
<p><%= custom_field_tag_with_label @custom_value %></p>
<% end %>

<p><%= check_box 'user', 'mail_notification' %> <label for="user_mail_notification"><%=l(:field_mail_notification)%></label></p>
Expand Down
4 changes: 2 additions & 2 deletions redmine/app/views/issues/edit.rhtml
Expand Up @@ -39,8 +39,8 @@
<p><label for="issue_due_date"><%=l(:field_due_date)%></label><br />
<%= date_select 'issue', 'due_date', :start_year => Date.today.year, :include_blank => true %></p>

<% for custom_value in @custom_values %>
<p><%= custom_field_tag_with_label custom_value %></p>
<% for @custom_value in @custom_values %>
<p><%= custom_field_tag_with_label @custom_value %></p>
<% end %>

<p><label for="issue_fixed_version"><%=l(:field_fixed_version)%></label><br/>
Expand Down
4 changes: 2 additions & 2 deletions redmine/app/views/projects/_form.rhtml
Expand Up @@ -22,8 +22,8 @@
<p><%= check_box 'project', 'is_public' %>
<label for="project_is_public"><%=l(:field_is_public)%></label></p>

<% for custom_value in @custom_values %>
<p><%= custom_field_tag_with_label custom_value %></p>
<% for @custom_value in @custom_values %>
<p><%= custom_field_tag_with_label @custom_value %></p>
<% end %>

<fieldset><legend><%=l(:label_custom_field_plural)%></legend>
Expand Down
4 changes: 2 additions & 2 deletions redmine/app/views/projects/add_issue.rhtml
Expand Up @@ -39,8 +39,8 @@
<p><label for="issue_due_date"><%=l(:field_due_date)%></label><br />
<%= date_select 'issue', 'due_date', :start_year => Date.today.year, :include_blank => true %></p>

<% for custom_value in @custom_values %>
<p><%= custom_field_tag_with_label custom_value %></p>
<% for @custom_value in @custom_values %>
<p><%= custom_field_tag_with_label @custom_value %></p>
<% end %>

<p><label for="attachment_file"><%=l(:label_attachment)%></label><br />
Expand Down
4 changes: 2 additions & 2 deletions redmine/app/views/users/_form.rhtml
Expand Up @@ -23,8 +23,8 @@
<p><label for="user_language"><%=l(:field_language)%></label><br/>
<%= select("user", "language", lang_options_for_select) %></p>

<% for custom_value in @custom_values %>
<p><%= custom_field_tag_with_label custom_value %></p>
<% for @custom_value in @custom_values %>
<p><%= custom_field_tag_with_label @custom_value %></p>
<% end %>

<div style="clear: both;"></div>
Expand Down

0 comments on commit 54aeec7

Please sign in to comment.