Skip to content

Commit

Permalink
Updated documentation
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@194 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Dec 16, 2004
1 parent f389a8f commit 0b55420
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 53 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/base.rb
Expand Up @@ -58,7 +58,7 @@ class UnknownAction < ActionControllerError #:nodoc:
# accessing the environment hash, like this: # accessing the environment hash, like this:
# #
# def hello_ip # def hello_ip
# location = @request.env["REMOTE_ADDRESS"] # location = @request.env["REMOTE_IP"]
# render_text "Hello stranger from #{location}" # render_text "Hello stranger from #{location}"
# end # end
# #
Expand Down
16 changes: 10 additions & 6 deletions actionpack/lib/action_controller/cookies.rb
Expand Up @@ -10,15 +10,19 @@ module ActionController #:nodoc:
# #
# cookies["user_name"] # => "david" # cookies["user_name"] # => "david"
# cookies.size # => 2 # cookies.size # => 2
#
# Example for deleting:
#
# cookies.delete "user_name"
# #
# All the option symbols for setting cookies are: # All the option symbols for setting cookies are:
# #
# value:: the cookie's value or list of values (as an array). # * <tt>value</tt> - the cookie's value or list of values (as an array).
# path:: the path for which this cookie applies. Defaults to the root of the application. # * <tt>path</tt> - the path for which this cookie applies. Defaults to the root of the application.
# domain:: the domain for which this cookie applies. # * <tt>domain</tt> - the domain for which this cookie applies.
# expires:: the time at which this cookie expires, as a +Time+ object. # * <tt>expires</tt> - the time at which this cookie expires, as a +Time+ object.
# secure:: whether this cookie is a secure cookie or not (default to false). # * <tt>secure</tt> - whether this cookie is a secure cookie or not (default to false).
# Secure cookies are only transmitted to HTTPS servers. # Secure cookies are only transmitted to HTTPS servers.
module Cookies module Cookies
# Returns the cookie container, which operates as described above. # Returns the cookie container, which operates as described above.
def cookies def cookies
Expand Down
31 changes: 29 additions & 2 deletions actionpack/lib/action_controller/dependencies.rb
Expand Up @@ -20,33 +20,60 @@ def self.append_features(base)
base.extend(ClassMethods) base.extend(ClassMethods)
end end


# Dependencies control what classes are needed for the controller to run its course. This is an alternative to doing explicit
# +require+ statements that bring a number of benefits. It's more succinct, communicates what type of dependency we're talking about,
# can trigger special behavior (as in the case of +observer+), and enables Rails to be clever about reloading in cached environments
# like FCGI. Example:
#
# class ApplicationController < ActionController::Base
# model :account, :company, :person, :project, :category
# helper :access_control
# service :notifications, :billings
# observer :project_change_observer
# end
#
# Please note that a controller like ApplicationController will automatically attempt to require_dependency on a model of its name and a helper
# of its name. If nothing is found, no error is raised. This is especially useful for concrete controllers like PostController:
#
# class PostController < ApplicationController
# # model :post (already required)
# # helper :post (already required)
# end
module ClassMethods module ClassMethods
# Loads the <tt>file_name</tt> if reload_dependencies is true or requires if it's false. # Loads the <tt>file_name</tt> if reload_dependencies is true or requires if it's false.
def require_dependency(file_name) def require_dependency(file_name)
reload_dependencies ? silence_warnings { load("#{file_name}.rb") } : require(file_name) reload_dependencies ? silence_warnings { load("#{file_name}.rb") } : require(file_name)
end end


# Specifies a variable number of models that this controller depends on. Models are normally Active Record classes or a similar
# backend for modelling entity classes.
def model(*models) def model(*models)
require_dependencies(:model, models) require_dependencies(:model, models)
depend_on(:model, models) depend_on(:model, models)
end end


# Specifies a variable number of services that this controller depends on. Services are normally singletons or factories, like
# Action Mailer service or a Payment Gateway service.
def service(*services) def service(*services)
require_dependencies(:service, services) require_dependencies(:service, services)
depend_on(:service, services) depend_on(:service, services)
end end


# Specifies a variable number of observers that are to govern when this controller is handling actions. The observers will
# automatically have .instance called on them to make them active on assignment.
def observer(*observers) def observer(*observers)
require_dependencies(:observer, observers) require_dependencies(:observer, observers)
depend_on(:observer, observers) depend_on(:observer, observers)
instantiate_observers(observers) instantiate_observers(observers)
end end


def dependencies_on(layer) # :nodoc: # Returns an array of symbols that specify the dependencies on a given layer. For the example at the top, calling
# <tt>ApplicationController.dependencies_on(:model)</tt> would return <tt>[:account, :company, :person, :project, :category]</tt>
def dependencies_on(layer)
read_inheritable_attribute("#{layer}_dependencies") read_inheritable_attribute("#{layer}_dependencies")
end end


def depend_on(layer, dependencies) def depend_on(layer, dependencies) #:nodoc:
write_inheritable_array("#{layer}_dependencies", dependencies) write_inheritable_array("#{layer}_dependencies", dependencies)
end end


Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/helpers.rb
Expand Up @@ -31,7 +31,7 @@ module ClassMethods
# Makes all the (instance) methods in the helper module available to templates rendered through this controller. # Makes all the (instance) methods in the helper module available to templates rendered through this controller.
# See ActionView::Helpers (link:classes/ActionView/Helpers.html) for more about making your own helper modules # See ActionView::Helpers (link:classes/ActionView/Helpers.html) for more about making your own helper modules
# available to the templates. # available to the templates.
def add_template_helper(helper_module) def add_template_helper(helper_module) #:nodoc:
template_class.class_eval "include #{helper_module}" template_class.class_eval "include #{helper_module}"
end end


Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/rescue.rb
Expand Up @@ -15,7 +15,7 @@ def self.append_features(base) #:nodoc:
end end
end end


module ClassMethods module ClassMethods #:nodoc:
def process_with_exception(request, response, exception) def process_with_exception(request, response, exception)
new.process(request, response, :rescue_action, exception) new.process(request, response, :rescue_action, exception)
end end
Expand Down
39 changes: 21 additions & 18 deletions actionpack/lib/action_controller/test_process.rb
Expand Up @@ -187,25 +187,28 @@ def delete() @attributes = {} end
end end
end end


class Test::Unit::TestCase #:nodoc: module Test
private module Unit
# execute the request and set/volley the response class TestCase #:nodoc:
def process(action, parameters = nil, session = nil) private
@request.env['REQUEST_METHOD'] ||= "GET" # execute the request and set/volley the response
@request.action = action.to_s def process(action, parameters = nil, session = nil)
@request.parameters.update(parameters) unless parameters.nil? @request.env['REQUEST_METHOD'] ||= "GET"
@request.session = ActionController::TestSession.new(session) unless session.nil? @request.action = action.to_s
@controller.process(@request, @response) @request.parameters.update(parameters) unless parameters.nil?
end @request.session = ActionController::TestSession.new(session) unless session.nil?
@controller.process(@request, @response)
end


# execute the request simulating a specific http method and set/volley the response # execute the request simulating a specific http method and set/volley the response
%w( get post put delete head ).each do |method| %w( get post put delete head ).each do |method|
class_eval <<-EOV class_eval <<-EOV
def #{method}(action, parameters = nil, session = nil) def #{method}(action, parameters = nil, session = nil)
@request.env['REQUEST_METHOD'] = "#{method.upcase}" @request.env['REQUEST_METHOD'] = "#{method.upcase}"
process(action, parameters, session) process(action, parameters, session)
end
EOV
end end
EOV
end end

end
end end
9 changes: 0 additions & 9 deletions actionpack/lib/action_view.rb
Expand Up @@ -28,15 +28,6 @@
# RubyGems is not available, use included Builder # RubyGems is not available, use included Builder
$:.unshift(File.dirname(__FILE__) + "/action_view/vendor") $:.unshift(File.dirname(__FILE__) + "/action_view/vendor")
require 'action_view/vendor/builder' require 'action_view/vendor/builder'
ensure
# Temporary patch until it's in Builder 1.2.2
class BlankSlate
class << self
def hide(name)
undef_method name if instance_methods.include?(name) and name !~ /^(__|instance_eval)/
end
end
end
end end


require 'action_view/base' require 'action_view/base'
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/base.rb
Expand Up @@ -141,7 +141,7 @@ def self.load_helpers(helper_dir)#:nodoc:
end end
end end


def self.controller_delegate(*methods) def self.controller_delegate(*methods)#:nodoc:
methods.flatten.each do |method| methods.flatten.each do |method|
class_eval <<-end_eval class_eval <<-end_eval
def #{method}(*args, &block) def #{method}(*args, &block)
Expand Down
6 changes: 3 additions & 3 deletions actionpack/lib/action_view/helpers/active_record_helper.rb
Expand Up @@ -83,9 +83,9 @@ def error_message_on(object, method, prepend_text = "", append_text = "", css_cl
# Returns a string with a div containing all the error messages for the object located as an instance variable by the name # Returns a string with a div containing all the error messages for the object located as an instance variable by the name
# of <tt>object_name</tt>. This div can be tailored by the following options: # of <tt>object_name</tt>. This div can be tailored by the following options:
# #
# ::header_tag: Used for the header of the error div (default: h2) # * <tt>header_tag</tt> - Used for the header of the error div (default: h2)
# ::id: The id of the error div (default: errorExplanation) # * <tt>id</tt> - The id of the error div (default: errorExplanation)
# ::class: The class of the error div (default: errorExplanation) # * <tt>class</tt> - The class of the error div (default: errorExplanation)
def error_messages_for(object_name, options={}) def error_messages_for(object_name, options={})
object = instance_eval "@#{object_name}" object = instance_eval "@#{object_name}"
unless object.errors.empty? unless object.errors.empty?
Expand Down
6 changes: 4 additions & 2 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -91,11 +91,13 @@ def text_area(object, method, options = {})
# #
# Example (call, result). Imagine that @post.validated? returns 1: # Example (call, result). Imagine that @post.validated? returns 1:
# check_box("post", "validated") # check_box("post", "validated")
# <input type="checkbox" id="post_validate" name="post[validated] value="1" checked="checked" /><input name="post[validated]" type="hidden" value="0" /> # <input type="checkbox" id="post_validate" name="post[validated] value="1" checked="checked" />
# <input name="post[validated]" type="hidden" value="0" />
# #
# Example (call, result). Imagine that @puppy.gooddog returns no: # Example (call, result). Imagine that @puppy.gooddog returns no:
# check_box("puppy", "gooddog", {}, "yes", "no") # check_box("puppy", "gooddog", {}, "yes", "no")
# <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog] value="yes" /><input name="puppy[gooddog]" type="hidden" value="no" /> # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog] value="yes" />
# <input name="puppy[gooddog]" type="hidden" value="no" />
def check_box(object, method, options = {}, checked_value = "1", unchecked_value = "0") def check_box(object, method, options = {}, checked_value = "1", unchecked_value = "0")
InstanceTag.new(object, method, self).to_check_box_tag(options, checked_value, unchecked_value) InstanceTag.new(object, method, self).to_check_box_tag(options, checked_value, unchecked_value)
end end
Expand Down
10 changes: 5 additions & 5 deletions actionpack/lib/action_view/helpers/tag_helper.rb
Expand Up @@ -7,16 +7,16 @@ module TagHelper
include ERB::Util include ERB::Util


# Examples: # Examples:
# * tag("br") => <br /> # * <tt>tag("br") => <br /></tt>
# * tag("input", { "type" => "text"}) => <input type="text" /> # * <tt>tag("input", { "type" => "text"}) => <input type="text" /></tt>
def tag(name, options = {}, open = false) def tag(name, options = {}, open = false)
"<#{name}#{tag_options(options)}" + (open ? ">" : " />") "<#{name}#{tag_options(options)}" + (open ? ">" : " />")
end end


# Examples: # Examples:
# * content_tag("p", "Hello world!") => <p>Hello world!</p> # * <tt>content_tag("p", "Hello world!") => <p>Hello world!</p></tt>
# * content_tag("div", content_tag("p", "Hello world!"), "class" => "strong") => # * <tt>content_tag("div", content_tag("p", "Hello world!"), "class" => "strong") => </tt>
# <div class="strong"><p>Hello world!</p></div> # <tt><div class="strong"><p>Hello world!</p></div></tt>
def content_tag(name, content, options = {}) def content_tag(name, content, options = {})
"<#{name}#{tag_options(options)}>#{content}</#{name}>" "<#{name}#{tag_options(options)}>#{content}</#{name}>"
end end
Expand Down
8 changes: 4 additions & 4 deletions actionpack/lib/action_view/helpers/url_helper.rb
Expand Up @@ -30,10 +30,10 @@ def link_to(name, options = {}, html_options = {}, *parameters_for_method_refere
# get a link tag that just points without consideration. The <tt>html_options</tt> works jointly for the image and ahref tag by # get a link tag that just points without consideration. The <tt>html_options</tt> works jointly for the image and ahref tag by
# letting the following special values enter the options on the image and the rest goes to the ahref: # letting the following special values enter the options on the image and the rest goes to the ahref:
# #
# ::alt: If no alt text is given, the file name part of the +src+ is used (capitalized and without the extension) # * <tt>alt</tt> - If no alt text is given, the file name part of the +src+ is used (capitalized and without the extension)
# ::size: Supplied as "XxY", so "30x45" becomes width="30" and height="45" # * <tt>size</tt> - Supplied as "XxY", so "30x45" becomes width="30" and height="45"
# ::border: Is set to 0 by default # * <tt>border</tt> - Is set to 0 by default
# ::align: Sets the alignment, no special features # * <tt>align</tt> - Sets the alignment, no special features
# #
# The +src+ can be supplied as a... # The +src+ can be supplied as a...
# * full path, like "/my_images/image.gif" # * full path, like "/my_images/image.gif"
Expand Down

0 comments on commit 0b55420

Please sign in to comment.