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 May 8, 2012
2 parents 882e750 + 7e26f7f commit 7918d7b
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 139 deletions.
20 changes: 10 additions & 10 deletions actionpack/lib/abstract_controller/callbacks.rb
Expand Up @@ -163,11 +163,11 @@ def _insert_callbacks(callbacks, block = nil)
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
# Append a before, after or around filter. See _insert_callbacks # Append a before, after or around filter. See _insert_callbacks
# for details on the allowed parameters. # for details on the allowed parameters.
def #{filter}_filter(*names, &blk) # def before_filter(*names, &blk) def #{filter}_filter(*names, &blk) # def before_filter(*names, &blk)
_insert_callbacks(names, blk) do |name, options| # _insert_callbacks(names, blk) do |name, options| _insert_callbacks(names, blk) do |name, options| # _insert_callbacks(names, blk) do |name, options|
set_callback(:process_action, :#{filter}, name, options) # set_callback(:process_action, :before, name, options) set_callback(:process_action, :#{filter}, name, options) # set_callback(:process_action, :before, name, options)
end # end end # end
end # end end # end
# Prepend a before, after or around filter. See _insert_callbacks # Prepend a before, after or around filter. See _insert_callbacks
# for details on the allowed parameters. # for details on the allowed parameters.
Expand All @@ -179,11 +179,11 @@ def prepend_#{filter}_filter(*names, &blk)
# Skip a before, after or around filter. See _insert_callbacks # Skip a before, after or around filter. See _insert_callbacks
# for details on the allowed parameters. # for details on the allowed parameters.
def skip_#{filter}_filter(*names) # def skip_before_filter(*names) def skip_#{filter}_filter(*names) # def skip_before_filter(*names)
_insert_callbacks(names) do |name, options| # _insert_callbacks(names) do |name, options| _insert_callbacks(names) do |name, options| # _insert_callbacks(names) do |name, options|
skip_callback(:process_action, :#{filter}, name, options) # skip_callback(:process_action, :before, name, options) skip_callback(:process_action, :#{filter}, name, options) # skip_callback(:process_action, :before, name, options)
end # end end # end
end # end end # end
# *_filter is the same as append_*_filter # *_filter is the same as append_*_filter
alias_method :append_#{filter}_filter, :#{filter}_filter # alias_method :append_before_filter, :before_filter alias_method :append_#{filter}_filter, :#{filter}_filter # alias_method :append_before_filter, :before_filter
Expand Down
6 changes: 3 additions & 3 deletions actionpack/lib/abstract_controller/helpers.rb
Expand Up @@ -49,9 +49,9 @@ def helper_method(*meths)


meths.each do |meth| meths.each do |meth|
_helpers.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1 _helpers.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
def #{meth}(*args, &blk) def #{meth}(*args, &blk) # def current_user(*args, &blk)
controller.send(%(#{meth}), *args, &blk) controller.send(%(#{meth}), *args, &blk) # controller.send(:current_user, *args, &blk)
end end # end
ruby_eval ruby_eval
end end
end end
Expand Down
3 changes: 3 additions & 0 deletions actionpack/lib/action_view/helpers/form_tag_helper.rb
Expand Up @@ -502,6 +502,9 @@ def button_tag(content_or_options = nil, options = nil, &block)
# #
# image_submit_tag("agree.png", :disabled => true, :class => "agree_disagree_button") # image_submit_tag("agree.png", :disabled => true, :class => "agree_disagree_button")
# # => <input class="agree_disagree_button" disabled="disabled" src="/images/agree.png" type="image" /> # # => <input class="agree_disagree_button" disabled="disabled" src="/images/agree.png" type="image" />
#
# image_submit_tag("save.png", :confirm => "Are you sure?")
# # => <input src="/images/save.png" data-confirm="Are you sure?" type="image" />
def image_submit_tag(source, options = {}) def image_submit_tag(source, options = {})
options = options.stringify_keys options = options.stringify_keys


Expand Down
253 changes: 161 additions & 92 deletions actionpack/lib/action_view/helpers/number_helper.rb

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions actionpack/lib/action_view/helpers/translation_helper.rb
Expand Up @@ -63,6 +63,9 @@ def translate(key, options = {})
alias :t :translate alias :t :translate


# Delegates to <tt>I18n.localize</tt> with no additional functionality. # Delegates to <tt>I18n.localize</tt> with no additional functionality.
#
# See http://rubydoc.info/github/svenfuchs/i18n/master/I18n/Backend/Base:localize
# for more information.
def localize(*args) def localize(*args)
I18n.localize(*args) I18n.localize(*args)
end end
Expand Down
10 changes: 10 additions & 0 deletions activemodel/README.rdoc
Expand Up @@ -135,6 +135,16 @@ behavior out of the box:
pattern in a Rails App and take advantage of all the standard observer pattern in a Rails App and take advantage of all the standard observer
functions. functions.


class PersonObserver < ActiveModel::Observer
def after_create(person)
person.logger.info("New person added!")
end

def after_destroy(person)
person.logger.warn("Person with an id of #{person.id} was destroyed!")
end
end

{Learn more}[link:classes/ActiveModel/Observer.html] {Learn more}[link:classes/ActiveModel/Observer.html]


* Making objects serializable * Making objects serializable
Expand Down
12 changes: 12 additions & 0 deletions activemodel/lib/active_model/attribute_methods.rb
Expand Up @@ -180,6 +180,18 @@ def attribute_method_affix(*affixes)
undefine_attribute_methods undefine_attribute_methods
end end



# Allows you to make aliases for attributes.
#
# class Person
# attr_accessor :name
# alias_attribute :nickname, :name
# end
#
# person = Person.new
# person.nickname = "Bob"
# person.nickname # => "Bob"
# person.name # => "Bob"
def alias_attribute(new_name, old_name) def alias_attribute(new_name, old_name)
attribute_method_matchers.each do |matcher| attribute_method_matchers.each do |matcher|
matcher_new = matcher.method_name(new_name).to_s matcher_new = matcher.method_name(new_name).to_s
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/relation/spawn_methods.rb
Expand Up @@ -7,7 +7,7 @@ module ActiveRecord
module SpawnMethods module SpawnMethods


# Merges in the conditions from <tt>other</tt>, if <tt>other</tt> is an <tt>ActiveRecord::Relation</tt>. # Merges in the conditions from <tt>other</tt>, if <tt>other</tt> is an <tt>ActiveRecord::Relation</tt>.
# Returns an array representing the union of the resulting records with <tt>other</tt>, if <tt>other</tt> is an array. # Returns an array representing the intersection of the resulting records with <tt>other</tt>, if <tt>other</tt> is an array.
# #
# ==== Examples # ==== Examples
# #
Expand All @@ -16,7 +16,7 @@ module SpawnMethods
# #
# recent_posts = Post.order('created_at DESC').first(5) # recent_posts = Post.order('created_at DESC').first(5)
# Post.where(:published => true).merge(recent_posts) # Post.where(:published => true).merge(recent_posts)
# # Returns the union of all published posts with the 5 most recently created posts. # # Returns the intersection of all published posts with the 5 most recently created posts.
# # (This is just an example. You'd probably want to do this with a single query!) # # (This is just an example. You'd probably want to do this with a single query!)
# #
def merge(other) def merge(other)
Expand Down
1 change: 1 addition & 0 deletions activerecord/lib/active_record/result.rb
Expand Up @@ -28,6 +28,7 @@ def to_hash
alias :map! :map alias :map! :map
alias :collect! :map alias :collect! :map


# Returns true if there are no records.
def empty? def empty?
rows.empty? rows.empty?
end end
Expand Down
134 changes: 111 additions & 23 deletions activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
Expand Up @@ -2,30 +2,33 @@


# Extends the class object with class and instance accessors for class attributes, # Extends the class object with class and instance accessors for class attributes,
# just like the native attr* accessors for instance attributes. # just like the native attr* accessors for instance attributes.
#
# Note that unlike +class_attribute+, if a subclass changes the value then that would
# also change the value for parent class. Similarly if parent class changes the value
# then that would change the value of subclasses too.
#
# class Person
# cattr_accessor :hair_colors
# end
#
# Person.hair_colors = [:brown, :black, :blonde, :red]
# Person.hair_colors # => [:brown, :black, :blonde, :red]
# Person.new.hair_colors # => [:brown, :black, :blonde, :red]
#
# To opt out of the instance writer method, pass :instance_writer => false.
# To opt out of the instance reader method, pass :instance_reader => false.
# To opt out of both instance methods, pass :instance_accessor => false.
#
# class Person
# cattr_accessor :hair_colors, :instance_writer => false, :instance_reader => false
# end
#
# Person.new.hair_colors = [:brown] # => NoMethodError
# Person.new.hair_colors # => NoMethodError
class Class class Class
# Defines a class attribute if it's not defined and creates a reader method that
# returns the attribute value.
#
# class Person
# cattr_reader :hair_colors
# end
#
# Person.class_variable_set("@@hair_colors", [:brown, :black])
# Person.hair_colors # => [:brown, :black]
# Person.new.hair_colors # => [:brown, :black]
#
# The attribute name must be a valid method name in Ruby.
#
# class Person
# cattr_reader :"1_Badname "
# end
# # => NameError: invalid attribute name
#
# If you want to opt out the instance reader method, you can pass <tt>instance_reader: false</tt>
# or <tt>instance_accessor: false</tt>.
#
# class Person
# cattr_reader :hair_colors, instance_reader: false
# end
#
# Person.new.hair_colors # => NoMethodError
def cattr_reader(*syms) def cattr_reader(*syms)
options = syms.extract_options! options = syms.extract_options!
syms.each do |sym| syms.each do |sym|
Expand All @@ -50,6 +53,43 @@ def #{sym}
end end
end end


# Defines a class attribute if it's not defined and creates a writer method to allow
# assignment to the attribute.
#
# class Person
# cattr_writer :hair_colors
# end
#
# Person.hair_colors = [:brown, :black]
# Person.class_variable_get("@@hair_colors") # => [:brown, :black]
# Person.new.hair_colors = [:blonde, :red]
# Person.class_variable_get("@@hair_colors") # => [:blonde, :red]
#
# The attribute name must be a valid method name in Ruby.
#
# class Person
# cattr_writer :"1_Badname "
# end
# # => NameError: invalid attribute name
#
# If you want to opt out the instance writer method, pass <tt>instance_writer: false</tt>
# or <tt>instance_accessor: false</tt>.
#
# class Person
# cattr_writer :hair_colors, instance_writer: false
# end
#
# Person.new.hair_colors = [:blonde, :red] # => NoMethodError
#
# Also, you can pass a block to set up the attribute with a default value.
#
# class Person
# cattr_writer :hair_colors do
# [:brown, :black, :blonde, :red]
# end
# end
#
# Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
def cattr_writer(*syms) def cattr_writer(*syms)
options = syms.extract_options! options = syms.extract_options!
syms.each do |sym| syms.each do |sym|
Expand All @@ -75,6 +115,54 @@ def #{sym}=(obj)
end end
end end


# Defines both class and instance accessors for class attributes.
#
# class Person
# cattr_accessor :hair_colors
# end
#
# Person.hair_colors = [:brown, :black, :blonde, :red]
# Person.hair_colors # => [:brown, :black, :blonde, :red]
# Person.new.hair_colors # => [:brown, :black, :blonde, :red]
#
# If a subclass changes the value then that would also change the value for
# parent class. Similarly if parent class changes the value then that would
# change the value of subclasses too.
#
# class Male < Person
# end
#
# Male.hair_colors << :blue
# Person.hair_colors # => [:brown, :black, :blonde, :red, :blue]
#
# To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
# To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
#
# class Person
# cattr_accessor :hair_colors, instance_writer: false, instance_reader: false
# end
#
# Person.new.hair_colors = [:brown] # => NoMethodError
# Person.new.hair_colors # => NoMethodError
#
# Or pass <tt>instance_accessor: false</tt>, to opt out both instance methods.
#
# class Person
# cattr_accessor :hair_colors, instance_accessor: false
# end
#
# Person.new.hair_colors = [:brown] # => NoMethodError
# Person.new.hair_colors # => NoMethodError
#
# Also you can pass a block to set up the attribute with a default value.
#
# class Person
# cattr_accessor :hair_colors do
# [:brown, :black, :blonde, :red]
# end
# end
#
# Person.class_variable_get("@@hair_colors") #=> [:brown, :black, :blonde, :red]
def cattr_accessor(*syms, &blk) def cattr_accessor(*syms, &blk)
cattr_reader(*syms) cattr_reader(*syms)
cattr_writer(*syms, &blk) cattr_writer(*syms, &blk)
Expand Down
4 changes: 3 additions & 1 deletion activesupport/lib/active_support/time_with_zone.rb
Expand Up @@ -35,8 +35,10 @@ module ActiveSupport
# t.is_a?(ActiveSupport::TimeWithZone) # => true # t.is_a?(ActiveSupport::TimeWithZone) # => true
# #
class TimeWithZone class TimeWithZone

# Report class name as 'Time' to thwart type checking
def self.name def self.name
'Time' # Report class name as 'Time' to thwart type checking 'Time'
end end


include Comparable include Comparable
Expand Down
33 changes: 31 additions & 2 deletions guides/source/active_support_core_extensions.textile
Expand Up @@ -3129,18 +3129,38 @@ The method +beginning_of_day+ returns a timestamp at the beginning of the day (0


<ruby> <ruby>
date = Date.new(2010, 6, 7) date = Date.new(2010, 6, 7)
date.beginning_of_day # => Sun Jun 07 00:00:00 +0200 2010 date.beginning_of_day # => Mon Jun 07 00:00:00 +0200 2010
</ruby> </ruby>


The method +end_of_day+ returns a timestamp at the end of the day (23:59:59): The method +end_of_day+ returns a timestamp at the end of the day (23:59:59):


<ruby> <ruby>
date = Date.new(2010, 6, 7) date = Date.new(2010, 6, 7)
date.end_of_day # => Sun Jun 06 23:59:59 +0200 2010 date.end_of_day # => Mon Jun 07 23:59:59 +0200 2010
</ruby> </ruby>


+beginning_of_day+ is aliased to +at_beginning_of_day+, +midnight+, +at_midnight+. +beginning_of_day+ is aliased to +at_beginning_of_day+, +midnight+, +at_midnight+.


h6. +beginning_of_hour+, +end_of_hour+

The method +beginning_of_hour+ returns a timestamp at the beginning of the hour (hh:00:00):

<ruby>
date = DateTime.new(2010, 6, 7, 19, 55, 25)
date.beginning_of_hour # => Mon Jun 07 19:00:00 +0200 2010
</ruby>

The method +end_of_hour+ returns a timestamp at the end of the hour (hh:59:59):

<ruby>
date = DateTime.new(2010, 6, 7, 19, 55, 25)
date.end_of_hour # => Mon Jun 07 19:59:59 +0200 2010
</ruby>

+beginning_of_hour+ is aliased to +at_beginning_of_hour+.

INFO: +beginning_of_hour+ and +end_of_hour+ are implemented for +Time+ and +DateTime+ but *not* +Date+ as it does not make sense to request the beginning or end of an hour on a +Date+ instance.

h6. +ago+, +since+ h6. +ago+, +since+


The method +ago+ receives a number of seconds as argument and returns a timestamp those many seconds ago from midnight: The method +ago+ receives a number of seconds as argument and returns a timestamp those many seconds ago from midnight:
Expand Down Expand Up @@ -3208,6 +3228,13 @@ since (in)


On the other hand, +advance+ and +change+ are also defined and support more options, they are documented below. On the other hand, +advance+ and +change+ are also defined and support more options, they are documented below.


The following methods are only implemented in +active_support/core_ext/date_time/calculations.rb+ as they only make sense when used with a +DateTime+ instance:

<ruby>
beginning_of_hour (at_beginning_of_hour)
end_of_hour
</ruby>

h5. Named Datetimes h5. Named Datetimes


h6. +DateTime.current+ h6. +DateTime.current+
Expand Down Expand Up @@ -3350,6 +3377,8 @@ ago
since (in) since (in)
beginning_of_day (midnight, at_midnight, at_beginning_of_day) beginning_of_day (midnight, at_midnight, at_beginning_of_day)
end_of_day end_of_day
beginning_of_hour (at_beginning_of_hour)
end_of_hour
beginning_of_week (at_beginning_of_week) beginning_of_week (at_beginning_of_week)
end_of_week (at_end_of_week) end_of_week (at_end_of_week)
monday monday
Expand Down

0 comments on commit 7918d7b

Please sign in to comment.