Skip to content

Commit f97832b

Browse files
committed
Merge docrails
1 parent 6ed42eb commit f97832b

File tree

15 files changed

+141
-66
lines changed

15 files changed

+141
-66
lines changed

actionpack/lib/action_view/helpers/form_helper.rb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -628,20 +628,23 @@ def text_area(object_name, method, options = {})
628628
#
629629
# The HTML specification says unchecked check boxes are not successful, and
630630
# thus web browsers do not send them. Unfortunately this introduces a gotcha:
631-
# if an Invoice model has a +paid+ flag, and in the form that edits a paid
631+
# if an +Invoice+ model has a +paid+ flag, and in the form that edits a paid
632632
# invoice the user unchecks its check box, no +paid+ parameter is sent. So,
633633
# any mass-assignment idiom like
634634
#
635635
# @invoice.update_attributes(params[:invoice])
636636
#
637637
# wouldn't update the flag.
638638
#
639-
# To prevent this the helper generates a hidden field with the same name as
640-
# the checkbox after the very check box. So, the client either sends only the
641-
# hidden field (representing the check box is unchecked), or both fields.
642-
# Since the HTML specification says key/value pairs have to be sent in the
643-
# same order they appear in the form and Rails parameters extraction always
644-
# gets the first occurrence of any given key, that works in ordinary forms.
639+
# To prevent this the helper generates an auxiliary hidden field before
640+
# the very check box. The hidden field has the same name and its
641+
# attributes mimick an unchecked check box.
642+
#
643+
# This way, the client either sends only the hidden field (representing
644+
# the check box is unchecked), or both fields. Since the HTML specification
645+
# says key/value pairs have to be sent in the same order they appear in the
646+
# form, and parameters extraction gets the last occurrence of any repeated
647+
# key in the query string, that works for ordinary forms.
645648
#
646649
# Unfortunately that workaround does not work when the check box goes
647650
# within an array-like parameter, as in
@@ -652,22 +655,26 @@ def text_area(object_name, method, options = {})
652655
# <% end %>
653656
#
654657
# because parameter name repetition is precisely what Rails seeks to distinguish
655-
# the elements of the array.
658+
# the elements of the array. For each item with a checked check box you
659+
# get an extra ghost item with only that attribute, assigned to "0".
660+
#
661+
# In that case it is preferable to either use +check_box_tag+ or to use
662+
# hashes instead of arrays.
656663
#
657664
# ==== Examples
658665
# # Let's say that @post.validated? is 1:
659666
# check_box("post", "validated")
660-
# # => <input type="checkbox" id="post_validated" name="post[validated]" value="1" />
661-
# # <input name="post[validated]" type="hidden" value="0" />
667+
# # => <input name="post[validated]" type="hidden" value="0" />
668+
# # <input type="checkbox" id="post_validated" name="post[validated]" value="1" />
662669
#
663670
# # Let's say that @puppy.gooddog is "no":
664671
# check_box("puppy", "gooddog", {}, "yes", "no")
665-
# # => <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
666-
# # <input name="puppy[gooddog]" type="hidden" value="no" />
672+
# # => <input name="puppy[gooddog]" type="hidden" value="no" />
673+
# # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
667674
#
668675
# check_box("eula", "accepted", { :class => 'eula_check' }, "yes", "no")
669-
# # => <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
670-
# # <input name="eula[accepted]" type="hidden" value="no" />
676+
# # => <input name="eula[accepted]" type="hidden" value="no" />
677+
# # <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
671678
#
672679
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
673680
InstanceTag.new(object_name, method, self, options.delete(:object)).to_check_box_tag(options, checked_value, unchecked_value)

actionpack/lib/action_view/helpers/text_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def simple_format(text, html_options={})
324324

325325
# Turns all URLs and e-mail addresses into clickable links. The <tt>:link</tt> option
326326
# will limit what should be linked. You can add HTML attributes to the links using
327-
# <tt>:href_options</tt>. Possible values for <tt>:link</tt> are <tt>:all</tt> (default),
327+
# <tt>:html</tt>. Possible values for <tt>:link</tt> are <tt>:all</tt> (default),
328328
# <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
329329
# e-mail address is yielded and the result is used as the link text.
330330
#
@@ -341,7 +341,7 @@ def simple_format(text, html_options={})
341341
# # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:[email protected]\">[email protected]</a>"
342342
#
343343
# post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at [email protected]."
344-
# auto_link(post_body, :href_options => { :target => '_blank' }) do |text|
344+
# auto_link(post_body, :html => { :target => '_blank' }) do |text|
345345
# truncate(text, 15)
346346
# end
347347
# # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
@@ -359,7 +359,7 @@ def simple_format(text, html_options={})
359359
# auto_link(post_body, :all, :target => "_blank") # => Once upon\na time
360360
# # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
361361
# Please e-mail me at <a href=\"mailto:[email protected]\">[email protected]</a>."
362-
def auto_link(text, *args, &block)#link = :all, href_options = {}, &block)
362+
def auto_link(text, *args, &block)#link = :all, html = {}, &block)
363363
return '' if text.blank?
364364

365365
options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter

activerecord/lib/active_record/base.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -810,25 +810,28 @@ def destroy(id)
810810

811811
# Updates all records with details given if they match a set of conditions supplied, limits and order can
812812
# also be supplied. This method constructs a single SQL UPDATE statement and sends it straight to the
813-
# database. It does not instantiate the involved models and it does not trigger Active Record callbacks.
813+
# database. It does not instantiate the involved models and it does not trigger Active Record callbacks
814+
# or validations.
814815
#
815816
# ==== Parameters
816817
#
817-
# * +updates+ - A string of column and value pairs that will be set on any records that match conditions. This creates the SET clause of the generated SQL.
818-
# * +conditions+ - An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro for more info.
818+
# * +updates+ - A string, array, or hash representing the SET part of an SQL statement.
819+
# * +conditions+ - A string, array, or hash representing the WHERE part of an SQL statement. See conditions in the intro.
819820
# * +options+ - Additional options are <tt>:limit</tt> and <tt>:order</tt>, see the examples for usage.
820821
#
821822
# ==== Examples
822823
#
823-
# # Update all billing objects with the 3 different attributes given
824-
# Billing.update_all( "category = 'authorized', approved = 1, author = 'David'" )
824+
# # Update all customers with the given attributes
825+
# Customer.update_all :wants_email => true
825826
#
826-
# # Update records that match our conditions
827-
# Billing.update_all( "author = 'David'", "title LIKE '%Rails%'" )
827+
# # Update all books with 'Rails' in their title
828+
# Book.update_all "author = 'David'", "title LIKE '%Rails%'"
828829
#
829-
# # Update records that match our conditions but limit it to 5 ordered by date
830-
# Billing.update_all( "author = 'David'", "title LIKE '%Rails%'",
831-
# :order => 'created_at', :limit => 5 )
830+
# # Update all avatars migrated more than a week ago
831+
# Avatar.update_all ['migrated_at = ?, Time.now.utc], ['migrated_at > ?', 1.week.ago]
832+
#
833+
# # Update all books that match our conditions, but limit it to 5 ordered by date
834+
# Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5
832835
def update_all(updates, conditions = nil, options = {})
833836
sql = "UPDATE #{quoted_table_name} SET #{sanitize_sql_for_assignment(updates)} "
834837

activeresource/README

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
= Active Resource
22

33
Active Resource (ARes) connects business objects and Representational State Transfer (REST)
4-
web services. It implements object-relational mapping for REST webservices to provide transparent
4+
web services. It implements object-relational mapping for REST web services to provide transparent
55
proxying capabilities between a client (ActiveResource) and a RESTful service (which is provided by Simply RESTful routing
66
in ActionController::Resources).
77

@@ -22,14 +22,14 @@ received and serialized into a usable Ruby object.
2222

2323
=== Configuration and Usage
2424

25-
Putting ActiveResource to use is very similar to ActiveRecord. It's as simple as creating a model class
25+
Putting Active Resource to use is very similar to Active Record. It's as simple as creating a model class
2626
that inherits from ActiveResource::Base and providing a <tt>site</tt> class variable to it:
2727

2828
class Person < ActiveResource::Base
2929
self.site = "http://api.people.com:3000/"
3030
end
3131

32-
Now the Person class is REST enabled and can invoke REST services very similarly to how ActiveRecord invokes
32+
Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes
3333
lifecycle methods that operate against a persistent store.
3434

3535
# Find a person with id = 1
@@ -42,7 +42,7 @@ records. But rather than dealing directly with a database record, you're dealin
4242
==== Protocol
4343

4444
Active Resource is built on a standard XML format for requesting and submitting resources over HTTP. It mirrors the RESTful routing
45-
built into ActionController but will also work with any other REST service that properly implements the protocol.
45+
built into Action Controller but will also work with any other REST service that properly implements the protocol.
4646
REST uses HTTP, but unlike "typical" web applications, it makes use of all the verbs available in the HTTP specification:
4747

4848
* GET requests are used for finding and retrieving resources.
@@ -55,8 +55,8 @@ for more general information on REST web services, see the article here[http://e
5555

5656
==== Find
5757

58-
GET Http requests expect the XML form of whatever resource/resources is/are being requested. So,
59-
for a request for a single element - the XML of that item is expected in response:
58+
Find requests use the GET method and expect the XML form of whatever resource/resources is/are being requested. So,
59+
for a request for a single element, the XML of that item is expected in response:
6060

6161
# Expects a response of
6262
#
@@ -101,7 +101,7 @@ Collections can also be requested in a similar fashion
101101

102102
==== Create
103103

104-
Creating a new resource submits the xml form of the resource as the body of the request and expects
104+
Creating a new resource submits the XML form of the resource as the body of the request and expects
105105
a 'Location' header in the response with the RESTful URL location of the newly created resource. The
106106
id of the newly created resource is parsed out of the Location response header and automatically set
107107
as the id of the ARes object.

activeresource/lib/active_resource/base.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module ActiveResource
1919
# end
2020
#
2121
# Now the Person class is mapped to RESTful resources located at <tt>http://api.people.com:3000/people/</tt>, and
22-
# you can now use Active Resource's lifecycles methods to manipulate resources. In the case where you already have
22+
# you can now use Active Resource's lifecycle methods to manipulate resources. In the case where you already have
2323
# an existing model with the same name as the desired RESTful resource you can set the +element_name+ value.
2424
#
2525
# class PersonResource < ActiveResource::Base
@@ -112,6 +112,7 @@ module ActiveResource
112112
#
113113
# Note: Some values cannot be provided in the URL passed to site. e.g. email addresses
114114
# as usernames. In those situations you should use the separate user and password option.
115+
#
115116
# == Errors & Validation
116117
#
117118
# Error handling and validation is handled in much the same manner as you're used to seeing in
@@ -156,7 +157,7 @@ module ActiveResource
156157
#
157158
# === Validation errors
158159
#
159-
# Active Resource supports validations on resources and will return errors if any these validations fail
160+
# Active Resource supports validations on resources and will return errors if any of these validations fail
160161
# (e.g., "First name can not be blank" and so on). These types of errors are denoted in the response by
161162
# a response code of <tt>422</tt> and an XML representation of the validation errors. The save operation will
162163
# then fail (with a <tt>false</tt> return value) and the validation errors can be accessed on the resource in question.
@@ -413,7 +414,7 @@ def element_path(id, prefix_options = {}, query_options = nil)
413414
# will split from the +prefix_options+.
414415
#
415416
# ==== Options
416-
# * +prefix_options+ - A hash to add a prefix to the request for nested URL's (e.g., <tt>:account_id => 19</tt>
417+
# * +prefix_options+ - A hash to add a prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt>
417418
# would yield a URL like <tt>/accounts/19/purchases.xml</tt>).
418419
# * +query_options+ - A hash to add items to the query string for the request.
419420
#
@@ -691,7 +692,7 @@ def clone
691692
end
692693

693694

694-
# A method to determine if the resource a \new object (i.e., it has not been POSTed to the remote service yet).
695+
# Returns +true+ if this object hasn't yet been saved, otherwise, returns +false+.
695696
#
696697
# ==== Examples
697698
# not_new = Computer.create(:brand => 'Apple', :make => 'MacBook', :vendor => 'MacMall')
@@ -760,7 +761,7 @@ def hash
760761
id.hash
761762
end
762763

763-
# Duplicate the current resource without saving it.
764+
# Duplicates the current resource without saving it.
764765
#
765766
# ==== Examples
766767
# my_invoice = Invoice.create(:customer => 'That Company')
@@ -779,8 +780,8 @@ def dup
779780
end
780781
end
781782

782-
# A method to \save (+POST+) or \update (+PUT+) a resource. It delegates to +create+ if a \new object,
783-
# +update+ if it is existing. If the response to the \save includes a body, it will be assumed that this body
783+
# Saves (+POST+) or \updates (+PUT+) a resource. Delegates to +create+ if the object is \new,
784+
# +update+ if it exists. If the response to the \save includes a body, it will be assumed that this body
784785
# is XML for the final object as it looked after the \save (which would include attributes like +created_at+
785786
# that weren't part of the original submit).
786787
#
@@ -832,7 +833,7 @@ def exists?
832833
!new? && self.class.exists?(to_param, :params => prefix_options)
833834
end
834835

835-
# A method to convert the the resource to an XML string.
836+
# Converts the resource to an XML string representation.
836837
#
837838
# ==== Options
838839
# The +options+ parameter is handed off to the +to_xml+ method on each
@@ -861,8 +862,7 @@ def to_xml(options={})
861862
attributes.to_xml({:root => self.class.element_name}.merge(options))
862863
end
863864

864-
# Returns a JSON string representing the model. Some configuration is
865-
# available through +options+.
865+
# Converts the resource to a JSON string representation.
866866
#
867867
# ==== Options
868868
# The +options+ are passed to the +to_json+ method on each

activeresource/lib/active_resource/connection.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,54 +95,54 @@ def site=(site)
9595
@password = URI.decode(@site.password) if @site.password
9696
end
9797

98-
# Set user for remote service.
98+
# Sets the user for remote service.
9999
def user=(user)
100100
@user = user
101101
end
102102

103-
# Set password for remote service.
103+
# Sets the password for remote service.
104104
def password=(password)
105105
@password = password
106106
end
107107

108-
# Set the number of seconds after which HTTP requests to the remote service should time out.
108+
# Sets the number of seconds after which HTTP requests to the remote service should time out.
109109
def timeout=(timeout)
110110
@timeout = timeout
111111
end
112112

113-
# Execute a GET request.
113+
# Executes a GET request.
114114
# Used to get (find) resources.
115115
def get(path, headers = {})
116116
format.decode(request(:get, path, build_request_headers(headers, :get)).body)
117117
end
118118

119-
# Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
119+
# Executes a DELETE request (see HTTP protocol documentation if unfamiliar).
120120
# Used to delete resources.
121121
def delete(path, headers = {})
122122
request(:delete, path, build_request_headers(headers, :delete))
123123
end
124124

125-
# Execute a PUT request (see HTTP protocol documentation if unfamiliar).
125+
# Executes a PUT request (see HTTP protocol documentation if unfamiliar).
126126
# Used to update resources.
127127
def put(path, body = '', headers = {})
128128
request(:put, path, body.to_s, build_request_headers(headers, :put))
129129
end
130130

131-
# Execute a POST request.
131+
# Executes a POST request.
132132
# Used to create new resources.
133133
def post(path, body = '', headers = {})
134134
request(:post, path, body.to_s, build_request_headers(headers, :post))
135135
end
136136

137-
# Execute a HEAD request.
137+
# Executes a HEAD request.
138138
# Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).
139139
def head(path, headers = {})
140140
request(:head, path, build_request_headers(headers))
141141
end
142142

143143

144144
private
145-
# Makes request to remote service.
145+
# Makes a request to the remote service.
146146
def request(method, path, *arguments)
147147
logger.info "#{method.to_s.upcase} #{site.scheme}://#{site.host}:#{site.port}#{path}" if logger
148148
result = nil
@@ -153,7 +153,7 @@ def request(method, path, *arguments)
153153
raise TimeoutError.new(e.message)
154154
end
155155

156-
# Handles response and error codes from remote service.
156+
# Handles response and error codes from the remote service.
157157
def handle_response(response)
158158
case response.code.to_i
159159
when 301,302
@@ -183,7 +183,7 @@ def handle_response(response)
183183
end
184184
end
185185

186-
# Creates new Net::HTTP instance for communication with
186+
# Creates new Net::HTTP instance for communication with the
187187
# remote service and resources.
188188
def http
189189
http = Net::HTTP.new(@site.host, @site.port)

0 commit comments

Comments
 (0)