2004-11-23 20:04:44 -05:00
|
|
|
require 'cgi'
|
2007-01-28 02:16:55 -05:00
|
|
|
require 'action_view/helpers/form_helper'
|
2009-05-14 20:42:20 -04:00
|
|
|
require 'active_support/core_ext/class/attribute_accessors'
|
2009-06-08 16:33:18 -04:00
|
|
|
require 'active_support/core_ext/enumerable'
|
|
|
|
require 'active_support/core_ext/kernel/reporting'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
module ActionView
|
|
|
|
class Base
|
2009-10-07 19:13:27 -04:00
|
|
|
@@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>".html_safe! }
|
2004-11-23 20:04:44 -05:00
|
|
|
cattr_accessor :field_error_proc
|
|
|
|
end
|
|
|
|
|
|
|
|
module Helpers
|
2008-05-02 09:45:23 -04:00
|
|
|
# The Active Record Helper makes it easier to create forms for records kept in instance variables. The most far-reaching is the +form+
|
2004-11-23 20:04:44 -05:00
|
|
|
# method that creates a complete form for all the basic content types of the record (not associations or aggregations, though). This
|
2007-10-25 23:34:48 -04:00
|
|
|
# is a great way of making the record quickly available for editing, but likely to prove lackluster for a complicated real-world form.
|
2008-05-02 09:45:23 -04:00
|
|
|
# In that case, it's better to use the +input+ method and the specialized +form+ methods in link:classes/ActionView/Helpers/FormHelper.html
|
2009-07-19 12:28:15 -04:00
|
|
|
module ActiveModelHelper
|
2008-05-02 09:45:23 -04:00
|
|
|
# Returns a default input tag for the type of object returned by the method. For example, if <tt>@post</tt>
|
|
|
|
# has an attribute +title+ mapped to a +VARCHAR+ column that holds "Hello World":
|
|
|
|
#
|
|
|
|
# input("post", "title")
|
|
|
|
# # => <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
|
2005-07-17 05:54:01 -04:00
|
|
|
def input(record_name, method, options = {})
|
|
|
|
InstanceTag.new(record_name, method, self).to_tag(options)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2008-05-02 09:45:23 -04:00
|
|
|
# Returns an entire form with all needed input tags for a specified Active Record object. For example, if <tt>@post</tt>
|
|
|
|
# has attributes named +title+ of type +VARCHAR+ and +body+ of type +TEXT+ then
|
|
|
|
#
|
2008-07-27 17:34:20 -04:00
|
|
|
# form("post")
|
2008-05-02 09:45:23 -04:00
|
|
|
#
|
|
|
|
# would yield a form like the following (modulus formatting):
|
|
|
|
#
|
|
|
|
# <form action='/posts/create' method='post'>
|
|
|
|
# <p>
|
|
|
|
# <label for="post_title">Title</label><br />
|
|
|
|
# <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
|
|
|
|
# </p>
|
|
|
|
# <p>
|
|
|
|
# <label for="post_body">Body</label><br />
|
|
|
|
# <textarea cols="40" id="post_body" name="post[body]" rows="20"></textarea>
|
|
|
|
# </p>
|
|
|
|
# <input name="commit" type="submit" value="Create" />
|
|
|
|
# </form>
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
|
|
|
# It's possible to specialize the form builder by using a different action name and by supplying another
|
2008-05-02 09:45:23 -04:00
|
|
|
# block renderer. For example, if <tt>@entry</tt> has an attribute +message+ of type +VARCHAR+ then
|
|
|
|
#
|
|
|
|
# form("entry",
|
|
|
|
# :action => "sign",
|
|
|
|
# :input_block => Proc.new { |record, column|
|
|
|
|
# "#{column.human_name}: #{input(record, column.name)}<br />"
|
|
|
|
# })
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2008-05-02 09:45:23 -04:00
|
|
|
# would yield a form like the following (modulus formatting):
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2008-05-02 09:45:23 -04:00
|
|
|
# <form action="/entries/sign" method="post">
|
|
|
|
# Message:
|
|
|
|
# <input id="entry_message" name="entry[message]" size="30" type="text" /><br />
|
|
|
|
# <input name="commit" type="submit" value="Sign" />
|
|
|
|
# </form>
|
2005-01-15 13:57:17 -05:00
|
|
|
#
|
|
|
|
# It's also possible to add additional content to the form by giving it a block, such as:
|
|
|
|
#
|
|
|
|
# form("entry", :action => "sign") do |form|
|
|
|
|
# form << content_tag("b", "Department")
|
|
|
|
# form << collection_select("department", "id", @departments, "id", "name")
|
|
|
|
# end
|
2008-01-11 11:25:23 -05:00
|
|
|
#
|
|
|
|
# The following options are available:
|
|
|
|
#
|
2008-05-02 09:45:23 -04:00
|
|
|
# * <tt>:action</tt> - The action used when submitting the form (default: +create+ if a new record, otherwise +update+).
|
|
|
|
# * <tt>:input_block</tt> - Specialize the output using a different block, see above.
|
|
|
|
# * <tt>:method</tt> - The method used when submitting the form (default: +post+).
|
|
|
|
# * <tt>:multipart</tt> - Whether to change the enctype of the form to "multipart/form-data", used when uploading a file (default: +false+).
|
|
|
|
# * <tt>:submit_value</tt> - The text of the submit button (default: "Create" if a new record, otherwise "Update").
|
2005-06-12 01:33:23 -04:00
|
|
|
def form(record_name, options = {})
|
|
|
|
record = instance_variable_get("@#{record_name}")
|
2009-07-19 11:58:59 -04:00
|
|
|
record = convert_to_model(record)
|
2004-12-12 08:48:58 -05:00
|
|
|
|
2005-06-12 01:33:23 -04:00
|
|
|
options = options.symbolize_keys
|
2004-12-12 08:48:58 -05:00
|
|
|
options[:action] ||= record.new_record? ? "create" : "update"
|
2005-06-12 01:33:23 -04:00
|
|
|
action = url_for(:action => options[:action], :id => record)
|
2004-12-12 08:48:58 -05:00
|
|
|
|
|
|
|
submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize
|
2005-03-06 06:50:41 -05:00
|
|
|
|
2008-01-11 11:25:23 -05:00
|
|
|
contents = form_tag({:action => action}, :method =>(options[:method] || 'post'), :enctype => options[:multipart] ? 'multipart/form-data': nil)
|
2005-06-12 01:33:23 -04:00
|
|
|
contents << hidden_field(record_name, :id) unless record.new_record?
|
|
|
|
contents << all_input_tags(record, record_name, options)
|
|
|
|
yield contents if block_given?
|
|
|
|
contents << submit_tag(submit_value)
|
2008-01-11 11:25:23 -05:00
|
|
|
contents << '</form>'
|
2009-10-07 16:31:20 -04:00
|
|
|
contents.html_safe!
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2007-03-27 10:04:06 -04:00
|
|
|
# Returns a string containing the error message attached to the +method+ on the +object+ if one exists.
|
2008-07-27 17:34:20 -04:00
|
|
|
# This error message is wrapped in a <tt>DIV</tt> tag, which can be extended to include a <tt>:prepend_text</tt>
|
|
|
|
# and/or <tt>:append_text</tt> (to properly explain the error), and a <tt>:css_class</tt> to style it
|
|
|
|
# accordingly. +object+ should either be the name of an instance variable or the actual object. The method can be
|
|
|
|
# passed in either as a string or a symbol.
|
|
|
|
# As an example, let's say you have a model <tt>@post</tt> that has an error message on the +title+ attribute:
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2008-05-02 09:45:23 -04:00
|
|
|
# <%= error_message_on "post", "title" %>
|
|
|
|
# # => <div class="formError">can't be empty</div>
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2008-07-27 17:34:20 -04:00
|
|
|
# <%= error_message_on @post, :title %>
|
2008-05-02 09:45:23 -04:00
|
|
|
# # => <div class="formError">can't be empty</div>
|
2007-10-07 15:50:30 -04:00
|
|
|
#
|
2008-07-27 17:34:20 -04:00
|
|
|
# <%= error_message_on "post", "title",
|
|
|
|
# :prepend_text => "Title simply ",
|
|
|
|
# :append_text => " (or it won't work).",
|
|
|
|
# :css_class => "inputError" %>
|
|
|
|
def error_message_on(object, method, *args)
|
|
|
|
options = args.extract_options!
|
|
|
|
unless args.empty?
|
2008-07-27 17:49:19 -04:00
|
|
|
ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate ' +
|
2008-07-27 17:34:20 -04:00
|
|
|
'prepend_text, append_text, and css_class arguments', caller)
|
|
|
|
|
|
|
|
options[:prepend_text] = args[0] || ''
|
|
|
|
options[:append_text] = args[1] || ''
|
|
|
|
options[:css_class] = args[2] || 'formError'
|
|
|
|
end
|
|
|
|
options.reverse_merge!(:prepend_text => '', :append_text => '', :css_class => 'formError')
|
|
|
|
|
2009-07-19 11:58:59 -04:00
|
|
|
object = convert_to_model(object)
|
2009-07-19 11:27:04 -04:00
|
|
|
|
2007-10-07 15:50:30 -04:00
|
|
|
if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
|
2009-06-17 22:37:08 -04:00
|
|
|
(errors = obj.errors[method])
|
2008-07-27 17:34:20 -04:00
|
|
|
content_tag("div",
|
2009-06-17 22:37:08 -04:00
|
|
|
"#{options[:prepend_text]}#{ERB::Util.html_escape(errors.first)}#{options[:append_text]}",
|
2008-07-27 17:34:20 -04:00
|
|
|
:class => options[:css_class]
|
|
|
|
)
|
|
|
|
else
|
2006-12-27 15:46:12 -05:00
|
|
|
''
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
2005-03-06 06:50:41 -05:00
|
|
|
|
2007-03-27 10:04:06 -04:00
|
|
|
# Returns a string with a <tt>DIV</tt> containing all of the error messages for the objects located as instance variables by the names
|
2006-04-26 19:09:08 -04:00
|
|
|
# given. If more than one object is specified, the errors for the objects are displayed in the order that the object names are
|
|
|
|
# provided.
|
|
|
|
#
|
2007-03-27 10:04:06 -04:00
|
|
|
# This <tt>DIV</tt> can be tailored by the following options:
|
2004-12-01 08:59:16 -05:00
|
|
|
#
|
2008-05-02 09:45:23 -04:00
|
|
|
# * <tt>:header_tag</tt> - Used for the header of the error div (default: "h2").
|
|
|
|
# * <tt>:id</tt> - The id of the error div (default: "errorExplanation").
|
|
|
|
# * <tt>:class</tt> - The class of the error div (default: "errorExplanation").
|
|
|
|
# * <tt>:object</tt> - The object (or array of objects) for which to display errors,
|
|
|
|
# if you need to escape the instance variable convention.
|
|
|
|
# * <tt>:object_name</tt> - The object name to use in the header, or any text that you prefer.
|
|
|
|
# If <tt>:object_name</tt> is not set, the name of the first object will be used.
|
|
|
|
# * <tt>:header_message</tt> - The message in the header of the error div. Pass +nil+
|
|
|
|
# or an empty string to avoid the header message altogether. (Default: "X errors
|
|
|
|
# prohibited this object from being saved").
|
|
|
|
# * <tt>:message</tt> - The explanation message after the header message and before
|
|
|
|
# the error list. Pass +nil+ or an empty string to avoid the explanation message
|
|
|
|
# altogether. (Default: "There were problems with the following fields:").
|
|
|
|
#
|
|
|
|
# To specify the display for one object, you simply provide its name as a parameter.
|
|
|
|
# For example, for the <tt>@user</tt> model:
|
2008-07-27 17:34:20 -04:00
|
|
|
#
|
2006-04-26 19:09:08 -04:00
|
|
|
# error_messages_for 'user'
|
|
|
|
#
|
2009-07-28 22:04:34 -04:00
|
|
|
# You can also supply an object:
|
|
|
|
#
|
|
|
|
# error_messages_for @user
|
|
|
|
#
|
|
|
|
# This will use the last part of the model name in the presentation. For instance, if
|
|
|
|
# this is a MyKlass::User object, this will use "user" as the name in the String. This
|
|
|
|
# is taken from MyKlass::User.model_name.human, which can be overridden.
|
|
|
|
#
|
2008-05-02 09:45:23 -04:00
|
|
|
# To specify more than one object, you simply list them; optionally, you can add an extra <tt>:object_name</tt> parameter, which
|
|
|
|
# will be the name used in the header message:
|
2006-04-26 19:09:08 -04:00
|
|
|
#
|
|
|
|
# error_messages_for 'user_common', 'user', :object_name => 'user'
|
2005-09-03 03:43:15 -04:00
|
|
|
#
|
2009-07-28 22:04:34 -04:00
|
|
|
# You can also use a number of objects, which will have the same naming semantics
|
|
|
|
# as a single object.
|
|
|
|
#
|
|
|
|
# error_messages_for @user, @post
|
|
|
|
#
|
2008-07-16 08:00:36 -04:00
|
|
|
# If the objects cannot be located as instance variables, you can add an extra <tt>:object</tt> parameter which gives the actual
|
2008-05-02 09:45:23 -04:00
|
|
|
# object (or array of objects to use):
|
2007-10-07 15:50:30 -04:00
|
|
|
#
|
|
|
|
# error_messages_for 'user', :object => @question.user
|
|
|
|
#
|
2005-09-03 03:43:15 -04:00
|
|
|
# NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what
|
2008-05-02 09:45:23 -04:00
|
|
|
# you need is significantly different from the default presentation, it makes plenty of sense to access the <tt>object.errors</tt>
|
2005-09-03 03:43:15 -04:00
|
|
|
# instance yourself and set it up. View the source of this method to see how easy it is.
|
2006-04-26 19:09:08 -04:00
|
|
|
def error_messages_for(*params)
|
2007-07-24 12:48:57 -04:00
|
|
|
options = params.extract_options!.symbolize_keys
|
2008-06-19 10:25:27 -04:00
|
|
|
|
2009-07-28 22:04:34 -04:00
|
|
|
objects = Array.wrap(options.delete(:object) || params).map do |object|
|
2009-10-21 12:18:36 -04:00
|
|
|
object = instance_variable_get("@#{object}") unless object.respond_to?(:to_model)
|
|
|
|
object = convert_to_model(object)
|
|
|
|
|
|
|
|
if object.class.respond_to?(:model_name)
|
|
|
|
options[:object_name] ||= object.class.model_name.human.downcase
|
2009-07-28 22:04:34 -04:00
|
|
|
end
|
2009-10-21 12:18:36 -04:00
|
|
|
|
2009-07-28 22:04:34 -04:00
|
|
|
object
|
2007-10-07 15:50:30 -04:00
|
|
|
end
|
2008-07-27 17:34:20 -04:00
|
|
|
|
2009-07-28 22:04:34 -04:00
|
|
|
objects.compact!
|
|
|
|
count = objects.inject(0) {|sum, object| sum + object.errors.count }
|
2009-10-17 12:03:28 -04:00
|
|
|
|
2006-04-26 19:09:08 -04:00
|
|
|
unless count.zero?
|
2006-07-08 01:35:08 -04:00
|
|
|
html = {}
|
|
|
|
[:id, :class].each do |key|
|
|
|
|
if options.include?(key)
|
|
|
|
value = options[key]
|
|
|
|
html[key] = value unless value.blank?
|
|
|
|
else
|
|
|
|
html[key] = 'errorExplanation'
|
|
|
|
end
|
|
|
|
end
|
2007-10-13 22:59:32 -04:00
|
|
|
options[:object_name] ||= params.first
|
|
|
|
|
2009-10-17 12:03:28 -04:00
|
|
|
I18n.with_options :locale => options[:locale], :scope => [:activemodel, :errors, :template] do |locale|
|
2008-06-19 10:25:27 -04:00
|
|
|
header_message = if options.include?(:header_message)
|
|
|
|
options[:header_message]
|
2008-07-27 17:34:20 -04:00
|
|
|
else
|
2009-10-17 12:03:28 -04:00
|
|
|
locale.t :header, :count => count, :model => options[:object_name].to_s.gsub('_', ' ')
|
2008-06-19 10:25:27 -04:00
|
|
|
end
|
2009-10-17 12:03:28 -04:00
|
|
|
|
2008-08-13 19:28:31 -04:00
|
|
|
message = options.include?(:message) ? options[:message] : locale.t(:body)
|
2009-10-17 12:03:28 -04:00
|
|
|
|
|
|
|
error_messages = objects.sum do |object|
|
|
|
|
object.errors.full_messages.map do |msg|
|
|
|
|
content_tag(:li, ERB::Util.html_escape(msg))
|
|
|
|
end
|
|
|
|
end.join
|
2008-06-19 10:25:27 -04:00
|
|
|
|
|
|
|
contents = ''
|
|
|
|
contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
|
|
|
|
contents << content_tag(:p, message) unless message.blank?
|
|
|
|
contents << content_tag(:ul, error_messages)
|
2007-10-13 22:59:32 -04:00
|
|
|
|
2008-06-19 10:25:27 -04:00
|
|
|
content_tag(:div, contents, html)
|
|
|
|
end
|
2006-01-19 03:47:18 -05:00
|
|
|
else
|
2006-04-26 19:09:08 -04:00
|
|
|
''
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
2008-07-27 17:34:20 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
private
|
|
|
|
def all_input_tags(record, record_name, options)
|
|
|
|
input_block = options[:input_block] || default_input_block
|
|
|
|
record.class.content_columns.collect{ |column| input_block.call(record_name, column) }.join("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_input_block
|
2005-06-12 01:33:23 -04:00
|
|
|
Proc.new { |record, column| %(<p><label for="#{record}_#{column.name}">#{column.human_name}</label><br />#{input(record, column.name)}</p>) }
|
2005-03-06 06:50:41 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2009-07-19 11:27:04 -04:00
|
|
|
module ActiveRecordInstanceTag
|
|
|
|
def object
|
|
|
|
@active_model_object ||= begin
|
|
|
|
object = super
|
|
|
|
object.respond_to?(:to_model) ? object.to_model : object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def to_tag(options = {})
|
|
|
|
case column_type
|
|
|
|
when :string
|
|
|
|
field_type = @method_name.include?("password") ? "password" : "text"
|
|
|
|
to_input_field_tag(field_type, options)
|
|
|
|
when :text
|
|
|
|
to_text_area_tag(options)
|
2006-07-12 23:25:17 -04:00
|
|
|
when :integer, :float, :decimal
|
2004-11-23 20:04:44 -05:00
|
|
|
to_input_field_tag("text", options)
|
|
|
|
when :date
|
|
|
|
to_date_select_tag(options)
|
2006-01-21 18:46:56 -05:00
|
|
|
when :datetime, :timestamp
|
2004-11-23 20:04:44 -05:00
|
|
|
to_datetime_select_tag(options)
|
2006-12-06 14:15:24 -05:00
|
|
|
when :time
|
|
|
|
to_time_select_tag(options)
|
2004-11-23 20:04:44 -05:00
|
|
|
when :boolean
|
|
|
|
to_boolean_select_tag(options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-17 22:37:08 -04:00
|
|
|
%w(tag content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
|
2009-08-07 20:04:56 -04:00
|
|
|
module_eval "def #{meth}(*) error_wrapping(super) end"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2009-06-17 22:37:08 -04:00
|
|
|
def error_wrapping(html_tag)
|
|
|
|
if object.respond_to?(:errors) && object.errors.respond_to?(:full_messages) && object.errors[@method_name].any?
|
|
|
|
Base.field_error_proc.call(html_tag, self)
|
2006-12-06 14:15:24 -05:00
|
|
|
else
|
2009-06-17 22:37:08 -04:00
|
|
|
html_tag
|
2006-12-06 14:15:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def column_type
|
2008-09-02 04:00:41 -04:00
|
|
|
object.send(:column_for_attribute, @method_name).type
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
2009-07-19 11:27:04 -04:00
|
|
|
|
|
|
|
class InstanceTag
|
|
|
|
include ActiveRecordInstanceTag
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|