Skip to content

Commit

Permalink
Merge branch 'master' of github.com:lifo/docrails
Browse files Browse the repository at this point in the history
  • Loading branch information
vijaydev committed Jul 7, 2012
2 parents 5fde4d4 + 700e5ff commit ee20be7
Show file tree
Hide file tree
Showing 20 changed files with 258 additions and 176 deletions.
3 changes: 0 additions & 3 deletions actionpack/lib/action_controller/metal/streaming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ module ActionController #:nodoc:
# session or flash after the template starts rendering will not propagate
# to the client.
#
# If you try to modify cookies, session or flash, an <tt>ActionDispatch::ClosedError</tt>
# will be raised, showing those objects are closed for modification.
#
# == Middlewares
#
# Middlewares that need to manipulate the body won't work with streaming.
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/asset_tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module Helpers #:nodoc:
# have SSL certificates for each of the asset hosts this technique allows you
# to avoid warnings in the client about mixed media.
#
# ActionController::Base.asset_host = Proc.new { |source, request|
# config.action_controller.asset_host = Proc.new { |source, request|
# if request.ssl?
# "#{request.protocol}#{request.host_with_port}"
# else
Expand Down
18 changes: 15 additions & 3 deletions activemodel/lib/active_model/conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,30 @@ def to_model

# Returns an Enumerable of all key attributes if any is set, regardless if
# the object is persisted or not. If there no key attributes, returns +nil+.
#
# class Person < ActiveRecord::Base
# end
#
# person = Person.create
# person.to_key # => [1]
def to_key
key = respond_to?(:id) && id
key ? [key] : nil
end

# Returns a string representing the object's key suitable for use in URLs,
# or +nil+ if <tt>persisted?</tt> is false.
# Returns a +string+ representing the object's key suitable for use in URLs,
# or +nil+ if <tt>persisted?</tt> is +false+.
#
# class Person < ActiveRecord::Base
# end
#
# person = Person.create
# person.to_param # => "1"
def to_param
persisted? ? to_key.join('-') : nil
end

# Returns a string identifying the path associated with the object.
# Returns a +string+ identifying the path associated with the object.
# ActionPack uses this to find a suitable partial to represent the object.
#
# class Person
Expand Down
6 changes: 4 additions & 2 deletions activemodel/lib/active_model/mass_assignment_security.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ def attr_protected(*args)
#
# attr_accessor :name, :credit_rating
#
# attr_accessible :name
# attr_accessible :name, :credit_rating, :as => :admin
# # Both admin and default user can change name of a customer
# attr_accessible :name, :as => [:admin, :default]
# # Only admin can change credit rating of a customer
# attr_accessible :credit_rating, :as => :admin
#
# def assign_attributes(values, options = {})
# sanitize_for_mass_assignment(values, options[:as]).each do |k, v|
Expand Down
50 changes: 36 additions & 14 deletions activemodel/lib/active_model/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ module ActiveModel
# person.name = "Bob"
# person.serializable_hash # => {"name"=>"Bob"}
#
# You need to declare an attributes hash which contains the attributes
# you want to serialize. Attributes must be strings, not symbols.
# When called, serializable hash will use
# instance methods that match the name of the attributes hash's keys.
# In order to override this behavior, take a look at the private
# method +read_attribute_for_serialization+.
# You need to declare an attributes hash which contains the attributes you
# want to serialize. Attributes must be strings, not symbols. When called,
# serializable hash will use instance methods that match the name of the
# attributes hash's keys. In order to override this behavior, take a look at
# the private method +read_attribute_for_serialization+.
#
# Most of the time though, you will want to include the JSON or XML
# serializations. Both of these modules automatically include the
# <tt>ActiveModel::Serialization</tt> module, so there is no need to explicitly
# include it.
# <tt>ActiveModel::Serialization</tt> module, so there is no need to
# explicitly include it.
#
# A minimal implementation including XML and JSON would be:
#
Expand Down Expand Up @@ -64,13 +63,37 @@ module ActiveModel
# person.to_json # => "{\"name\":\"Bob\"}"
# person.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person...
#
# Valid options are <tt>:only</tt>, <tt>:except</tt>, <tt>:methods</tt> and <tt>:include</tt>.
# The following are all valid examples:
# Valid options are <tt>:only</tt>, <tt>:except</tt>, <tt>:methods</tt> and
# <tt>:include</tt>. The following are all valid examples:
#
# person.serializable_hash(:only => 'name')
# person.serializable_hash(:include => :address)
# person.serializable_hash(:include => { :address => { :only => 'city' }})
# person.serializable_hash(only: 'name')
# person.serializable_hash(include: :address)
# person.serializable_hash(include: { address: { only: 'city' }})
module Serialization
# Returns a serialized hash of your object.
#
# class Person
# include ActiveModel::Serialization
#
# attr_accessor :name, :age
#
# def attributes
# {'name' => nil, 'age' => nil}
# end
#
# def capitalized_name
# name.capitalize
# end
# end
#
# person = Person.new
# person.name = 'bob'
# person.age = 22
# person.serializable_hash # => {"name"=>"bob", "age"=>22}
# person.serializable_hash(only: :name) # => {"name"=>"bob"}
# person.serializable_hash(except: :name) # => {"age"=>22}
# person.serializable_hash(methods: :capitalized_name)
# # => {"name"=>"bob", "age"=>22, "capitalized_name"=>"Bob"}
def serializable_hash(options = nil)
options ||= {}

Expand Down Expand Up @@ -115,7 +138,6 @@ def serializable_hash(options = nil)
# @data[key]
# end
# end
#
alias :read_attribute_for_serialization :send

# Add associations specified via the <tt>:include</tt> option.
Expand Down
20 changes: 10 additions & 10 deletions activemodel/lib/active_model/validations/acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module HelperMethods
#
# class Person < ActiveRecord::Base
# validates_acceptance_of :terms_of_service
# validates_acceptance_of :eula, :message => "must be abided"
# validates_acceptance_of :eula, message: "must be abided"
# end
#
# If the database column does not exist, the +terms_of_service+ attribute
Expand All @@ -41,23 +41,23 @@ module HelperMethods
# validation contexts by default (+nil+), other options are <tt>:create</tt>
# and <tt>:update</tt>.
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+ (default
# is true).
# is +true+).
# * <tt>:accept</tt> - Specifies value that is considered accepted.
# The default value is a string "1", which makes it easy to relate to
# an HTML checkbox. This should be set to +true+ if you are validating
# a database column, since the attribute is typecast from "1" to +true+
# before validation.
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
# if the validation should occur (e.g. <tt>:if => :allow_validation</tt>,
# or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false
# value.
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The
# method, proc or string should return or evaluate to a +true+ or
# +false+ value.
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
# determine if the validation should not occur (for example,
# <tt>:unless => :skip_validation</tt>, or
# <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>).
# The method, proc or string should return or evaluate to a true or
# false value.
# <tt>unless: :skip_validation</tt>, or
# <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>).
# The method, proc or string should return or evaluate to a +true+ or
# +false+ value.
# * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
def validates_acceptance_of(*attr_names)
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/validations/clusivity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ActiveModel
module Validations
module Clusivity
module Clusivity #:nodoc:
ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " <<
"and must be supplied as the :in option of the configuration hash"

Expand Down
23 changes: 12 additions & 11 deletions activemodel/lib/active_model/validations/confirmation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module HelperMethods
# class Person < ActiveRecord::Base
# validates_confirmation_of :user_name, :password
# validates_confirmation_of :email_address,
# :message => "should match confirmation"
# message: 'should match confirmation'
# end
#
# View:
Expand All @@ -38,10 +38,10 @@ module HelperMethods
# attribute.
#
# NOTE: This check is performed only if +password_confirmation+ is not
# +nil+. To require confirmation, make sure
# to add a presence check for the confirmation attribute:
# +nil+. To require confirmation, make sure to add a presence check for
# the confirmation attribute:
#
# validates_presence_of :password_confirmation, :if => :password_changed?
# validates_presence_of :password_confirmation, if: :password_changed?
#
# Configuration options:
# * <tt>:message</tt> - A custom error message (default is: "doesn't match
Expand All @@ -50,15 +50,16 @@ module HelperMethods
# validation contexts by default (+nil+), other options are <tt>:create</tt>
# and <tt>:update</tt>.
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
# if the validation should occur (e.g. <tt>:if => :allow_validation</tt>,
# or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false
# value.
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The
# method, proc or string should return or evaluate to a +true+ or
# +false+ value.
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
# determine if the validation should not occur (e.g.
# <tt>:unless => :skip_validation</tt>, or
# <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
# <tt>unless: :skip_validation</tt>, or
# <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a +true+ or
# +false+ value.
# * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
def validates_confirmation_of(*attr_names)
Expand Down
31 changes: 16 additions & 15 deletions activemodel/lib/active_model/validations/exclusion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,36 @@ module HelperMethods
# particular enumerable object.
#
# class Person < ActiveRecord::Base
# validates_exclusion_of :username, :in => %w( admin superuser ), :message => "You don't belong here"
# validates_exclusion_of :age, :in => 30..60, :message => "This site is only for under 30 and over 60"
# validates_exclusion_of :format, :in => %w( mov avi ), :message => "extension %{value} is not allowed"
# validates_exclusion_of :password, :in => lambda { |p| [p.username, p.first_name] },
# :message => "should not be the same as your username or first name"
# validates_exclusion_of :username, in: %w( admin superuser ), message: "You don't belong here"
# validates_exclusion_of :age, in: 30..60, message: 'This site is only for under 30 and over 60'
# validates_exclusion_of :format, in: %w( mov avi ), message: "extension %{value} is not allowed"
# validates_exclusion_of :password, in: ->(person) { [person.username, person.first_name] },
# message: 'should not be the same as your username or first name'
# end
#
# Configuration options:
# * <tt>:in</tt> - An enumerable object of items that the value shouldn't be
# part of. This can be supplied as a proc or lambda which returns an
# * <tt>:in</tt> - An enumerable object of items that the value shouldn't
# be part of. This can be supplied as a proc or lambda which returns an
# enumerable. If the enumerable is a range the test is performed with
# <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>.
# * <tt>:message</tt> - Specifies a custom error message (default is: "is
# reserved").
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
# is +nil+ (default is +false+).
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the
# attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the
# attribute is blank(default is +false+).
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
# validation contexts by default (+nil+), other options are <tt>:create</tt>
# and <tt>:update</tt>.
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if
# the validation should occur (e.g. <tt>:if => :allow_validation</tt>, or
# <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
# or string should return or evaluate to a true or false value.
# the validation should occur (e.g. <tt>if: :allow_validation</tt>, or
# <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
# proc or string should return or evaluate to a +true+ or +false+ value.
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
# if the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
# or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
# if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a +true+ or
# +false+ value.
# * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
def validates_exclusion_of(*attr_names)
Expand Down
39 changes: 21 additions & 18 deletions activemodel/lib/active_model/validations/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ module HelperMethods
# attribute matches the regular expression:
#
# class Person < ActiveRecord::Base
# validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :on => :create
# validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create
# end
#
# Alternatively, you can require that the specified attribute does _not_
# match the regular expression:
#
# class Person < ActiveRecord::Base
# validates_format_of :email, :without => /NOSPAM/
# validates_format_of :email, without: /NOSPAM/
# end
#
# You can also provide a proc or lambda which will determine the regular
Expand All @@ -73,43 +73,46 @@ module HelperMethods
# class Person < ActiveRecord::Base
# # Admin can have number as a first letter in their screen name
# validates_format_of :screen_name,
# :with => lambda{ |person| person.admin? ? /\A[a-z0-9][a-z0-9_\-]*\z/i : /\A[a-z][a-z0-9_\-]*\z/i }
# with: ->(person) { person.admin? ? /\A[a-z0-9][a-z0-9_\-]*\z/i : /\A[a-z][a-z0-9_\-]*\z/i }
# end
#
# Note: use <tt>\A</tt> and <tt>\Z</tt> to match the start and end of the
# string, <tt>^</tt> and <tt>$</tt> match the start/end of a line.
#
# Due to frequent misuse of <tt>^</tt> and <tt>$</tt>, you need to pass the
# :multiline => true option in case you use any of these two anchors in the provided
# regular expression. In most cases, you should be using <tt>\A</tt> and <tt>\z</tt>.
# Due to frequent misuse of <tt>^</tt> and <tt>$</tt>, you need to pass
# the <tt>multiline: true</tt> option in case you use any of these two
# anchors in the provided regular expression. In most cases, you should be
# using <tt>\A</tt> and <tt>\z</tt>.
#
# You must pass either <tt>:with</tt> or <tt>:without</tt> as an option.
# In addition, both must be a regular expression or a proc or lambda, or
# else an exception will be raised.
#
# Configuration options:
# * <tt>:message</tt> - A custom error message (default is: "is invalid").
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
# is +nil+ (default is +false+).
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the
# attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the
# attribute is blank (default is +false+).
# * <tt>:with</tt> - Regular expression that if the attribute matches will
# result in a successful validation. This can be provided as a proc or lambda
# returning regular expression which will be called at runtime.
# * <tt>:without</tt> - Regular expression that if the attribute does not match
# will result in a successful validation. This can be provided as a proc or
# result in a successful validation. This can be provided as a proc or
# lambda returning regular expression which will be called at runtime.
# * <tt>:without</tt> - Regular expression that if the attribute does not
# match will result in a successful validation. This can be provided as
# a proc or lambda returning regular expression which will be called at
# runtime.
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
# validation contexts by default (+nil+), other options are <tt>:create</tt>
# and <tt>:update</tt>.
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
# if the validation should occur (e.g. <tt>:if => :allow_validation</tt>, or
# <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
# or string should return or evaluate to a true or false value.
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
# proc or string should return or evaluate to a +true+ or +false+ value.
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
# if the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
# or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
# if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a +true+ or
# +false+ value.
# * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
# * <tt>:multiline</tt> - Set to true if your regular expression contains
Expand Down
Loading

0 comments on commit ee20be7

Please sign in to comment.