Merge branch 'master' of github.com:rails/rails

This commit is contained in:
wycats 2010-04-11 14:53:17 -07:00
commit 803df08d89
16 changed files with 146 additions and 137 deletions

@ -1,5 +1,7 @@
*Rails 3.0.0 [beta 3] (pending)*
* New option :as added to form_for allows to change the object name. The old <% form_for :client, @post %> becomes <% form_for @post, :as => :client %> [spastorino]
* Removed verify method in controllers. [JV]
It's now available as a plugin at http://github.com/rails/verification

@ -55,6 +55,14 @@ def extract_path_and_options(args)
path = args.first
end
if @scope[:module] && options[:to]
if options[:to].to_s.include?("#")
options[:to] = "#{@scope[:module]}/#{options[:to]}"
elsif @scope[:controller].nil?
options[:to] = "#{@scope[:module]}##{options[:to]}"
end
end
path = normalize_path(path)
if using_match_shorthand?(path, options)

@ -8,90 +8,91 @@
module ActionView
module Helpers
# Form helpers are designed to make working with models much easier
# compared to using just standard HTML elements by providing a set of
# methods for creating forms based on your models. This helper generates
# the HTML for forms, providing a method for each sort of input
# (e.g., text, password, select, and so on). When the form is submitted
# (i.e., when the user hits the submit button or <tt>form.submit</tt> is
# called via JavaScript), the form inputs will be bundled into the
# <tt>params</tt> object and passed back to the controller.
# Form helpers are designed to make working with resources much easier
# compared to using vanilla HTML.
#
# There are two types of form helpers: those that specifically work with
# model attributes and those that don't. This helper deals with those that
# work with model attributes; to see an example of form helpers that don't
# work with model attributes, check the ActionView::Helpers::FormTagHelper
# documentation.
# Forms for models are created with +form_for+. That method yields a form
# builder that knows the model the form is about. The form builder is thus
# able to generate default values for input fields that correspond to model
# attributes, and also convenient names, IDs, endpoints, etc.
#
# The core method of this helper, form_for, gives you the ability to create
# a form for a model instance; for example, let's say that you have a model
# <tt>Person</tt> and want to create a new instance of it:
# Conventions in the generated field names allow controllers to receive form
# data nicely structured in +params+ with no effort on your side.
#
# # Note: a @person variable will have been created in the controller.
# # For example: @person = Person.new
# <%= form_for @person do |f| %>
# <%= f.text_field :first_name %>
# <%= f.text_field :last_name %>
# <%= submit_tag 'Create' %>
# <% end %>
# For example, to create a new person you typically set up a new instance of
# +Person+ in the <tt>PeopleController#new</tt> action, <tt>@person</tt>, and
# pass it to +form_for+:
#
# The HTML generated for this would be:
# <%= form_for @person do |f| %>
# <%= f.label :first_name %>:
# <%= f.text_field :first_name %><br />
#
# <form action="/persons/create" method="post">
# <input id="person_first_name" name="person[first_name]" size="30" type="text" />
# <input id="person_last_name" name="person[last_name]" size="30" type="text" />
# <input name="commit" type="submit" value="Create" />
# </form>
# <%= f.label :last_name %>:
# <%= f.text_field :last_name %><br />
#
# If you are using a partial for your form fields, you can use this shortcut:
# <%= f.submit %>
# <% end %>
#
# <%= form_for :person, @person do |form| %>
# <%= render :partial => f %>
# <%= submit_tag 'Create' %>
# <% end %>
# The HTML generated for this would be (modulus formatting):
#
# This example will render the <tt>people/_form</tt> partial, setting a
# local variable called <tt>form</tt> which references the yielded
# FormBuilder. The <tt>params</tt> object created when this form is
# submitted would look like:
# <form action="/people" class="new_person" id="new_person" method="post">
# <div style="margin:0;padding:0;display:inline">
# <input name="authenticity_token" type="hidden" value="NrOp5bsjoLRuK8IW5+dQEYjKGUJDe7TQoZVvq95Wteg=" />
# </div>
# <label for="person_first_name">First name</label>:
# <input id="person_first_name" name="person[first_name]" size="30" type="text" /><br />
#
# {"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}}
# <label for="person_last_name">Last name</label>:
# <input id="person_last_name" name="person[last_name]" size="30" type="text" /><br />
#
# The params hash has a nested <tt>person</tt> value, which can therefore
# be accessed with <tt>params[:person]</tt> in the controller. If were
# editing/updating an instance (e.g., <tt>Person.find(1)</tt> rather than
# <tt>Person.new</tt> in the controller), the objects attribute values are
# filled into the form (e.g., the <tt>person_first_name</tt> field would
# have that person's first name in it).
# <input id="person_submit" name="commit" type="submit" value="Create Person" />
# </form>
#
# If the object name contains square brackets the id for the object will be
# inserted. For example:
# As you see, the HTML reflects knowledge about the resource in several spots,
# like the path the form should be submitted to, or the names of the input fields.
#
# <%= text_field "person[]", "name" %>
# In particular, thanks to the conventions followed in the generated field names, the
# controller gets a nested hash <tt>params[:person]</tt> with the person attributes
# set in the form. That hash is ready to be passed to <tt>Person.create</tt>:
#
# ...will generate the following ERb.
# if @person = Person.create(params[:person])
# # success
# else
# # error handling
# end
#
# <input type="text" id="person_<%= @person.id %>_name" name="person[<%= @person.id %>][name]" value="<%= @person.name %>" />
# Interestingly, the exact same view code in the previous example can be used to edit
# a person. If <tt>@person</tt> is an existing record with name "John Smith" and ID 256,
# the code above as is would yield instead:
#
# If the helper is being used to generate a repetitive sequence of similar
# form elements, for example in a partial used by
# <tt>render_collection_of_partials</tt>, the <tt>index</tt> option may
# come in handy. Example:
# <form action="/people/256" class="edit_person" id="edit_person_256" method="post">
# <div style="margin:0;padding:0;display:inline">
# <input name="_method" type="hidden" value="put" />
# <input name="authenticity_token" type="hidden" value="NrOp5bsjoLRuK8IW5+dQEYjKGUJDe7TQoZVvq95Wteg=" />
# </div>
# <label for="person_first_name">First name</label>:
# <input id="person_first_name" name="person[first_name]" size="30" type="text" value="John" /><br />
#
# <%= text_field "person", "name", "index" => 1 %>
# <label for="person_last_name">Last name</label>:
# <input id="person_last_name" name="person[last_name]" size="30" type="text" value="Smith" /><br />
#
# ...becomes...
# <input id="person_submit" name="commit" type="submit" value="Update Person" />
# </form>
#
# <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" />
# Note that the endpoint, default values, and submit button label are tailored for <tt>@person</tt>.
# That works that way because the involved helpers know whether the resource is a new record or not,
# and generate HTML accordingly.
#
# An <tt>index</tt> option may also be passed to <tt>form_for</tt> and
# <tt>fields_for</tt>. This automatically applies the <tt>index</tt> to
# all the nested fields.
# The controller would receive the form data again in <tt>params[:person]</tt>, ready to be
# passed to <tt>Person#update_attributes</tt>:
#
# There are also methods for helping to build form tags in
# link:classes/ActionView/Helpers/FormOptionsHelper.html,
# link:classes/ActionView/Helpers/DateHelper.html, and
# link:classes/ActionView/Helpers/ActiveRecordHelper.html
# if @person.update_attributes(params[:person])
# # success
# else
# # error handling
# end
#
# That's how you tipically work with resources.
module FormHelper
extend ActiveSupport::Concern
@ -129,9 +130,8 @@ module FormHelper
# Admin? : <%= f.check_box :admin %><br />
# <% end %>
#
# There, the first argument is a symbol or string with the name of the
# object the form is about, and also the name of the instance variable
# the object is stored in.
# There, the argument is a symbol or string with the name of the
# object the form is about.
#
# The form builder acts as a regular form helper that somehow carries the
# model. Thus, the idea is that
@ -142,26 +142,7 @@ module FormHelper
#
# <%= text_field :person, :first_name %>
#
# If the instance variable is not <tt>@person</tt> you can pass the actual
# record as the second argument:
#
# <%= form_for :person, person do |f| %>
# ...
# <% end %>
#
# In that case you can think
#
# <%= f.text_field :first_name %>
#
# gets expanded to
#
# <%= text_field :person, :first_name, :object => person %>
#
# You can even display error messages of the wrapped model this way:
#
# <%= f.error_messages %>
#
# In any of its variants, the rightmost argument to +form_for+ is an
# The rightmost argument to +form_for+ is an
# optional hash of options:
#
# * <tt>:url</tt> - The URL the form is submitted to. It takes the same
@ -177,7 +158,7 @@ module FormHelper
# possible to use both the stand-alone FormHelper methods and methods
# from FormTagHelper. For example:
#
# <%= form_for :person, @person do |f| %>
# <%= form_for @person do |f| %>
# First name: <%= f.text_field :first_name %>
# Last name : <%= f.text_field :last_name %>
# Biography : <%= text_area :person, :biography %>
@ -203,7 +184,7 @@ module FormHelper
#
# is equivalent to something like:
#
# <%= form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %>
# <%= form_for @post, :as => :post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %>
# ...
# <% end %>
#
@ -213,9 +194,9 @@ module FormHelper
# ...
# <% end %>
#
# expands to
# is equivalent to something like:
#
# <%= form_for :post, Post.new, :url => posts_path, :html => { :class => "new_post", :id => "new_post" } do |f| %>
# <%= form_for @post, :as => :post, :url => post_path(@post), :html => { :class => "new_post", :id => "new_post" } do |f| %>
# ...
# <% end %>
#
@ -225,6 +206,13 @@ module FormHelper
# ...
# <% end %>
#
# If you have an object that needs to be represented as a different
# parameter, like a Client that acts as a Person:
#
# <%= form_for(@post, :as => :client do |f| %>
# ...
# <% end %>
#
# And for namespaced routes, like +admin_post_url+:
#
# <%= form_for([:admin, @post]) do |f| %>
@ -245,13 +233,13 @@ module FormHelper
#
# Example:
#
# <%= form_for(:post, @post, :remote => true, :html => { :id => 'create-post', :method => :put }) do |f| %>
# <%= form_for(@post, :remote => true) do |f| %>
# ...
# <% end %>
#
# The HTML generated for this would be:
#
# <form action='http://www.example.com' id='create-post' method='post' data-remote='true'>
# <form action='http://www.example.com' method='post' data-remote='true'>
# <div style='margin:0;padding:0;display:inline'>
# <input name='_method' type='hidden' value='put' />
# </div>
@ -265,7 +253,7 @@ module FormHelper
# custom builder. For example, let's say you made a helper to
# automatically add labels to form inputs.
#
# <%= form_for :person, @person, :url => { :action => "create" }, :builder => LabellingFormBuilder do |f| %>
# <%= form_for @person, :url => { :action => "create" }, :builder => LabellingFormBuilder do |f| %>
# <%= f.text_field :first_name %>
# <%= f.text_field :last_name %>
# <%= text_area :person, :biography %>

@ -140,7 +140,7 @@ def self.matches?(request)
namespace :account do
match 'shorthand'
match 'description', :to => "account#description", :as => "description"
match 'description', :to => "description", :as => "description"
resource :subscription, :credit, :credit_card
root :to => "account#index"
@ -864,7 +864,7 @@ def test_namespaced_roots
with_test_routes do
assert_equal '/account', account_root_path
get '/account'
assert_equal 'account#index', @response.body
assert_equal 'account/account#index', @response.body
end
end

@ -1,5 +1,6 @@
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/enumerable'
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/object/blank'
module ActiveRecord
@ -1707,9 +1708,9 @@ def create_extension_modules(association_id, block_extension, extensions)
silence_warnings do
self.parent.const_set(extension_module_name, Module.new(&block_extension))
end
Array(extensions).push("#{self.parent}::#{extension_module_name}".constantize)
Array.wrap(extensions).push("#{self.parent}::#{extension_module_name}".constantize)
else
Array(extensions)
Array.wrap(extensions)
end
end

@ -1,4 +1,5 @@
require 'set'
require 'active_support/core_ext/array/wrap'
module ActiveRecord
module Associations
@ -98,7 +99,7 @@ def to_ary
if @target.is_a?(Array)
@target.to_ary
else
Array(@target)
Array.wrap(@target)
end
end
alias_method :to_a, :to_ary

@ -1,3 +1,5 @@
require 'active_support/core_ext/array/wrap'
module ActiveRecord
module Associations
# This is the root class of all association proxies:
@ -55,7 +57,7 @@ def initialize(owner, reflection)
@owner, @reflection = owner, reflection
@updated = false
reflection.check_validity!
Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) }
Array.wrap(reflection.options[:extend]).each { |ext| proxy_extend(ext) }
reset
end

@ -1,3 +1,5 @@
require 'active_support/core_ext/array/wrap'
module ActiveRecord
# Callbacks are hooks into the lifecycle of an Active Record object that allow you to trigger logic
# before or after an alteration of the object state. This can be used to make sure that associated and
@ -250,7 +252,7 @@ def method_added(meth)
def before_validation(*args, &block)
options = args.last
if options.is_a?(Hash) && options[:on]
options[:if] = Array(options[:if])
options[:if] = Array.wrap(options[:if])
options[:if] << "@_on_validate == :#{options[:on]}"
end
set_callback(:validation, :before, *args, &block)
@ -259,7 +261,7 @@ def before_validation(*args, &block)
def after_validation(*args, &block)
options = args.extract_options!
options[:prepend] = true
options[:if] = Array(options[:if])
options[:if] = Array.wrap(options[:if])
options[:if] << "!halted && value != false"
options[:if] << "@_on_validate == :#{options[:on]}" if options[:on]
set_callback(:validation, :after, *(args << options), &block)

@ -32,7 +32,6 @@ def #{method_name}_with_query_dirty(*args) # def update_with_query_dirty(
# Enable the query cache within the block.
def cache
old, @query_cache_enabled = @query_cache_enabled, true
@query_cache ||= {}
yield
ensure
clear_query_cache
@ -54,7 +53,7 @@ def uncached
# the same SQL query and repeatedly return the same result each time, silently
# undermining the randomness you were expecting.
def clear_query_cache
@query_cache.clear if @query_cache
@query_cache.clear
end
def select_all_with_query_cache(*args)

@ -1,3 +1,5 @@
require 'active_support/core_ext/array/wrap'
module ActiveRecord
module ConnectionAdapters # :nodoc:
module SchemaStatements
@ -267,7 +269,7 @@ def rename_column(table_name, column_name, new_column_name)
# generates
# CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
def add_index(table_name, column_name, options = {})
column_names = Array(column_name)
column_names = Array.wrap(column_name)
index_name = index_name(table_name, :column => column_names)
if Hash === options # legacy support, since this param was a string
@ -297,7 +299,7 @@ def remove_index(table_name, options = {})
def index_name(table_name, options) #:nodoc:
if Hash === options # legacy support
if options[:column]
"index_#{table_name}_on_#{Array(options[:column]) * '_and_'}"
"index_#{table_name}_on_#{Array.wrap(options[:column]) * '_and_'}"
elsif options[:name]
options[:name]
else

@ -41,6 +41,7 @@ def initialize(connection, logger = nil) #:nodoc:
@connection, @logger = connection, logger
@runtime = 0
@query_cache_enabled = false
@query_cache = {}
end
# Returns the human-readable name of the adapter. Use mixed case - one

@ -54,6 +54,12 @@ def initialize(name, default, sql_type = nil, null = true)
super(name, self.class.extract_value_from_default(default), sql_type, null)
end
# :stopdoc:
class << self
attr_accessor :money_precision
end
# :startdoc:
private
def extract_limit(sql_type)
case sql_type
@ -71,9 +77,11 @@ def extract_scale(sql_type)
# Extracts the precision from PostgreSQL-specific data types.
def extract_precision(sql_type)
# Actual code is defined dynamically in PostgreSQLAdapter.connect
# depending on the server specifics
super
if sql_type == 'money'
self.class.money_precision
else
super
end
end
# Maps PostgreSQL-specific data types to logical Rails types.
@ -83,18 +91,18 @@ def simplified_type(field_type)
when /^(?:real|double precision)$/
:float
# Monetary types
when /^money$/
when 'money'
:decimal
# Character types
when /^(?:character varying|bpchar)(?:\(\d+\))?$/
:string
# Binary data types
when /^bytea$/
when 'bytea'
:binary
# Date/time types
when /^timestamp with(?:out)? time zone$/
:datetime
when /^interval$/
when 'interval'
:string
# Geometric types
when /^(?:point|line|lseg|box|"?path"?|polygon|circle)$/
@ -106,16 +114,16 @@ def simplified_type(field_type)
when /^bit(?: varying)?(?:\(\d+\))?$/
:string
# XML type
when /^xml$/
when 'xml'
:xml
# Arrays
when /^\D+\[\]$/
:string
# Object identifier types
when /^oid$/
when 'oid'
:integer
# UUID type
when /^uuid$/
when 'uuid'
:string
# Small and big integer types
when /^(?:small|big)int$/
@ -383,9 +391,9 @@ def unescape_bytea(original_value)
def quote(value, column = nil) #:nodoc:
if value.kind_of?(String) && column && column.type == :binary
"#{quoted_string_prefix}'#{escape_bytea(value)}'"
elsif value.kind_of?(String) && column && column.sql_type =~ /^xml$/
elsif value.kind_of?(String) && column && column.sql_type == 'xml'
"xml E'#{quote_string(value)}'"
elsif value.kind_of?(Numeric) && column && column.sql_type =~ /^money$/
elsif value.kind_of?(Numeric) && column && column.sql_type == 'money'
# Not truly string input, so doesn't require (or allow) escape string syntax.
"'#{value.to_s}'"
elsif value.kind_of?(String) && column && column.sql_type =~ /^bit/
@ -925,7 +933,7 @@ def distinct(columns, order_by) #:nodoc:
# Construct a clean list of column names from the ORDER BY clause, removing
# any ASC/DESC modifiers
order_columns = order_by.split(',').collect { |s| s.split.first }
order_columns.delete_if &:blank?
order_columns.delete_if(&:blank?)
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
# Return a DISTINCT ON() clause that's distinct on the columns we want but includes
@ -989,17 +997,8 @@ def connect
# Money type has a fixed precision of 10 in PostgreSQL 8.2 and below, and as of
# PostgreSQL 8.3 it has a fixed precision of 19. PostgreSQLColumn.extract_precision
# should know about this but can't detect it there, so deal with it here.
money_precision = (postgresql_version >= 80300) ? 19 : 10
PostgreSQLColumn.module_eval(<<-end_eval)
def extract_precision(sql_type) # def extract_precision(sql_type)
if sql_type =~ /^money$/ # if sql_type =~ /^money$/
#{money_precision} # 19
else # else
super # super
end # end
end # end
end_eval
PostgreSQLColumn.money_precision =
(postgresql_version >= 80300) ? 19 : 10
configure_connection
end

@ -1,3 +1,4 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/object/blank'
module ActiveRecord
@ -9,7 +10,7 @@ module QueryMethods
attr_accessor :"#{query_method}_values"
next if [:where, :having].include?(query_method)
class_eval <<-CEVAL
class_eval <<-CEVAL, __FILE__
def #{query_method}(*args, &block)
new_relation = clone
new_relation.send(:apply_modules, Module.new(&block)) if block_given?
@ -21,12 +22,12 @@ def #{query_method}(*args, &block)
end
[:where, :having].each do |query_method|
class_eval <<-CEVAL
class_eval <<-CEVAL, __FILE__
def #{query_method}(*args, &block)
new_relation = clone
new_relation.send(:apply_modules, Module.new(&block)) if block_given?
value = build_where(*args)
new_relation.#{query_method}_values += [*value] if value.present?
new_relation.#{query_method}_values += Array.wrap(value) if value.present?
new_relation
end
CEVAL
@ -35,7 +36,7 @@ def #{query_method}(*args, &block)
ActiveRecord::Relation::SINGLE_VALUE_METHODS.each do |query_method|
attr_accessor :"#{query_method}_value"
class_eval <<-CEVAL
class_eval <<-CEVAL, __FILE__
def #{query_method}(value = true, &block)
new_relation = clone
new_relation.send(:apply_modules, Module.new(&block)) if block_given?

@ -1,3 +1,4 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/hash/conversions'
module ActiveRecord #:nodoc:
@ -186,7 +187,7 @@ def serializable_attributes
end
def serializable_method_attributes
Array(options[:methods]).inject([]) do |method_attributes, name|
Array.wrap(options[:methods]).inject([]) do |method_attributes, name|
method_attributes << MethodAttribute.new(name.to_s, @serializable) if @serializable.respond_to?(name.to_s)
method_attributes
end

@ -1,3 +1,5 @@
require 'active_support/core_ext/array/wrap'
module ActiveRecord
module Validations
class UniquenessValidator < ActiveModel::EachValidator
@ -19,7 +21,7 @@ def validate_each(record, attribute, value)
relation = table.where(sql, *params)
Array(options[:scope]).each do |scope_item|
Array.wrap(options[:scope]).each do |scope_item|
scope_value = record.send(scope_item)
relation = relation.where(scope_item => scope_value)
end

@ -97,7 +97,7 @@ def test_array_values
def test_money_values
assert_equal 567.89, @first_money.wealth
assert_equal -567.89, @second_money.wealth
assert_equal(-567.89, @second_money.wealth)
end
def test_number_values