diff --git a/actionpack/lib/abstract_controller/callbacks.rb b/actionpack/lib/abstract_controller/callbacks.rb index c0fa28cae9750..520c210721392 100644 --- a/actionpack/lib/abstract_controller/callbacks.rb +++ b/actionpack/lib/abstract_controller/callbacks.rb @@ -163,11 +163,11 @@ def _insert_callbacks(callbacks, block = nil) class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 # Append a before, after or around filter. See _insert_callbacks # for details on the allowed parameters. - def #{filter}_filter(*names, &blk) # def before_filter(*names, &blk) - _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) - end # end - end # end + def #{filter}_filter(*names, &blk) # def before_filter(*names, &blk) + _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) + end # end + end # end # Prepend a before, after or around filter. See _insert_callbacks # for details on the allowed parameters. @@ -179,11 +179,11 @@ def prepend_#{filter}_filter(*names, &blk) # Skip a before, after or around filter. See _insert_callbacks # for details on the allowed parameters. - def skip_#{filter}_filter(*names) # def skip_before_filter(*names) - _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) - end # end - end # end + def skip_#{filter}_filter(*names) # def skip_before_filter(*names) + _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) + end # end + end # end # *_filter is the same as append_*_filter alias_method :append_#{filter}_filter, :#{filter}_filter # alias_method :append_before_filter, :before_filter diff --git a/actionpack/lib/abstract_controller/helpers.rb b/actionpack/lib/abstract_controller/helpers.rb index 77cc4c07d9773..4e0672d590ca9 100644 --- a/actionpack/lib/abstract_controller/helpers.rb +++ b/actionpack/lib/abstract_controller/helpers.rb @@ -49,9 +49,9 @@ def helper_method(*meths) meths.each do |meth| _helpers.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1 - def #{meth}(*args, &blk) - controller.send(%(#{meth}), *args, &blk) - end + def #{meth}(*args, &blk) # def current_user(*args, &blk) + controller.send(%(#{meth}), *args, &blk) # controller.send(:current_user, *args, &blk) + end # end ruby_eval end end diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 0d824bd57436b..248cc2f6a3529 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -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("save.png", :confirm => "Are you sure?") + # # => def image_submit_tag(source, options = {}) options = options.stringify_keys diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index 2011351bd26b0..dfc26acfad328 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -28,17 +28,20 @@ def initialize(number) end end - # Formats a +number+ into a US phone number (e.g., (555) 123-9876). You can customize the format - # in the +options+ hash. + # Formats a +number+ into a US phone number (e.g., (555) + # 123-9876). You can customize the format in the +options+ hash. # # ==== Options # - # * :area_code - Adds parentheses around the area code. - # * :delimiter - Specifies the delimiter to use (defaults to "-"). - # * :extension - Specifies an extension to add to the end of the - # generated number. - # * :country_code - Sets the country code for the phone number. - # * :raise - If true, raises +InvalidNumberError+ when the argument is invalid. + # * :area_code - Adds parentheses around the area code. + # * :delimiter - Specifies the delimiter to use + # (defaults to "-"). + # * :extension - Specifies an extension to add to the + # end of the generated number. + # * :country_code - Sets the country code for the phone + # number. + # * :raise - If true, raises +InvalidNumberError+ when + # the argument is invalid. # # ==== Examples # @@ -81,24 +84,31 @@ def number_to_phone(number, options = {}) ERB::Util.html_escape(str) end - # Formats a +number+ into a currency string (e.g., $13.65). You can customize the format - # in the +options+ hash. + # Formats a +number+ into a currency string (e.g., $13.65). You + # can customize the format in the +options+ hash. # # ==== Options # - # * :locale - Sets the locale to be used for formatting (defaults to current locale). - # * :precision - Sets the level of precision (defaults to 2). - # * :unit - Sets the denomination of the currency (defaults to "$"). - # * :separator - Sets the separator between the units (defaults to "."). - # * :delimiter - Sets the thousands delimiter (defaults to ","). - # * :format - Sets the format for non-negative numbers (defaults to "%u%n"). - # Fields are %u for the currency, and %n - # for the number. - # * :negative_format - Sets the format for negative numbers (defaults to prepending - # an hyphen to the formatted number given by :format). - # Accepts the same fields than :format, except - # %n is here the absolute value of the number. - # * :raise - If true, raises +InvalidNumberError+ when the argument is invalid. + # * :locale - Sets the locale to be used for formatting + # (defaults to current locale). + # * :precision - Sets the level of precision (defaults + # to 2). + # * :unit - Sets the denomination of the currency + # (defaults to "$"). + # * :separator - Sets the separator between the units + # (defaults to "."). + # * :delimiter - Sets the thousands delimiter (defaults + # to ","). + # * :format - Sets the format for non-negative numbers + # (defaults to "%u%n"). Fields are %u for the + # currency, and %n for the number. + # * :negative_format - Sets the format for negative + # numbers (defaults to prepending an hyphen to the formatted + # number given by :format). Accepts the same fields + # than :format, except %n is here the + # absolute value of the number. + # * :raise - If true, raises +InvalidNumberError+ when + # the argument is invalid. # # ==== Examples # @@ -148,23 +158,29 @@ def number_to_currency(number, options = {}) end end - # Formats a +number+ as a percentage string (e.g., 65%). You can customize the format in the +options+ hash. + # Formats a +number+ as a percentage string (e.g., 65%). You can + # customize the format in the +options+ hash. # # ==== Options # - # * :locale - Sets the locale to be used for formatting (defaults to current - # locale). - # * :precision - Sets the precision of the number (defaults to 3). - # * :significant - If +true+, precision will be the # of significant_digits. If +false+, - # the # of fractional digits (defaults to +false+). - # * :separator - Sets the separator between the fractional and integer digits (defaults - # to "."). - # * :delimiter - Sets the thousands delimiter (defaults to ""). - # * :strip_insignificant_zeros - If +true+ removes insignificant zeros after the decimal separator - # (defaults to +false+). - # * :format - Specifies the format of the percentage string - # The number field is %n (defaults to "%n%"). - # * :raise - If true, raises +InvalidNumberError+ when the argument is invalid. + # * :locale - Sets the locale to be used for formatting + # (defaults to current locale). + # * :precision - Sets the precision of the number + # (defaults to 3). + # * :significant - If +true+, precision will be the # + # of significant_digits. If +false+, the # of fractional + # digits (defaults to +false+). + # * :separator - Sets the separator between the + # fractional and integer digits (defaults to "."). + # * :delimiter - Sets the thousands delimiter (defaults + # to ""). + # * :strip_insignificant_zeros - If +true+ removes + # insignificant zeros after the decimal separator (defaults to + # +false+). + # * :format - Specifies the format of the percentage + # string The number field is %n (defaults to "%n%"). + # * :raise - If true, raises +InvalidNumberError+ when + # the argument is invalid. # # ==== Examples # @@ -200,15 +216,20 @@ def number_to_percentage(number, options = {}) end end - # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can - # customize the format in the +options+ hash. + # Formats a +number+ with grouped thousands using +delimiter+ + # (e.g., 12,324). You can customize the format in the +options+ + # hash. # # ==== Options # - # * :locale - Sets the locale to be used for formatting (defaults to current locale). - # * :delimiter - Sets the thousands delimiter (defaults to ","). - # * :separator - Sets the separator between the fractional and integer digits (defaults to "."). - # * :raise - If true, raises +InvalidNumberError+ when the argument is invalid. + # * :locale - Sets the locale to be used for formatting + # (defaults to current locale). + # * :delimiter - Sets the thousands delimiter (defaults + # to ","). + # * :separator - Sets the separator between the + # fractional and integer digits (defaults to "."). + # * :raise - If true, raises +InvalidNumberError+ when + # the argument is invalid. # # ==== Examples # @@ -236,23 +257,32 @@ def number_with_delimiter(number, options = {}) safe_join(parts, options[:separator]) end - # Formats a +number+ with the specified level of :precision (e.g., 112.32 has a precision - # of 2 if +:significant+ is +false+, and 5 if +:significant+ is +true+). + # Formats a +number+ with the specified level of + # :precision (e.g., 112.32 has a precision of 2 if + # +:significant+ is +false+, and 5 if +:significant+ is +true+). # You can customize the format in the +options+ hash. # # ==== Options - # * :locale - Sets the locale to be used for formatting (defaults to current locale). - # * :precision - Sets the precision of the number (defaults to 3). - # * :significant - If +true+, precision will be the # of significant_digits. If +false+, - # the # of fractional digits (defaults to +false+). - # * :separator - Sets the separator between the fractional and integer digits (defaults - # to "."). - # * :delimiter - Sets the thousands delimiter (defaults to ""). - # * :strip_insignificant_zeros - If +true+ removes insignificant zeros after the decimal separator - # (defaults to +false+). - # * :raise - If true, raises +InvalidNumberError+ when the argument is invalid. + # + # * :locale - Sets the locale to be used for formatting + # (defaults to current locale). + # * :precision - Sets the precision of the number + # (defaults to 3). + # * :significant - If +true+, precision will be the # + # of significant_digits. If +false+, the # of fractional + # digits (defaults to +false+). + # * :separator - Sets the separator between the + # fractional and integer digits (defaults to "."). + # * :delimiter - Sets the thousands delimiter (defaults + # to ""). + # * :strip_insignificant_zeros - If +true+ removes + # insignificant zeros after the decimal separator (defaults to + # +false+). + # * :raise - If true, raises +InvalidNumberError+ when + # the argument is invalid. # # ==== Examples + # # number_with_precision(111.2345) # => 111.235 # number_with_precision(111.2345, :precision => 2) # => 111.23 # number_with_precision(13, :precision => 5) # => 13.00000 @@ -305,23 +335,37 @@ def number_with_precision(number, options = {}) STORAGE_UNITS = [:byte, :kb, :mb, :gb, :tb].freeze - # Formats the bytes in +number+ into a more understandable representation - # (e.g., giving it 1500 yields 1.5 KB). This method is useful for - # reporting file sizes to users. You can customize the - # format in the +options+ hash. + # Formats the bytes in +number+ into a more understandable + # representation (e.g., giving it 1500 yields 1.5 KB). This + # method is useful for reporting file sizes to users. You can + # customize the format in the +options+ hash. # - # See number_to_human if you want to pretty-print a generic number. + # See number_to_human if you want to pretty-print a + # generic number. # # ==== Options - # * :locale - Sets the locale to be used for formatting (defaults to current locale). - # * :precision - Sets the precision of the number (defaults to 3). - # * :significant - If +true+, precision will be the # of significant_digits. If +false+, the # of fractional digits (defaults to +true+) - # * :separator - Sets the separator between the fractional and integer digits (defaults to "."). - # * :delimiter - Sets the thousands delimiter (defaults to ""). - # * :strip_insignificant_zeros - If +true+ removes insignificant zeros after the decimal separator (defaults to +true+) - # * :prefix - If +:si+ formats the number using the SI prefix (defaults to :binary) - # * :raise - If true, raises +InvalidNumberError+ when the argument is invalid. + # + # * :locale - Sets the locale to be used for formatting + # (defaults to current locale). + # * :precision - Sets the precision of the number + # (defaults to 3). + # * :significant - If +true+, precision will be the # + # of significant_digits. If +false+, the # of fractional + # digits (defaults to +true+) + # * :separator - Sets the separator between the + # fractional and integer digits (defaults to "."). + # * :delimiter - Sets the thousands delimiter (defaults + # to ""). + # * :strip_insignificant_zeros - If +true+ removes + # insignificant zeros after the decimal separator (defaults to + # +true+) + # * :prefix - If +:si+ formats the number using the SI + # prefix (defaults to :binary) + # * :raise - If true, raises +InvalidNumberError+ when + # the argument is invalid. + # # ==== Examples + # # number_to_human_size(123) # => 123 Bytes # number_to_human_size(1234) # => 1.21 KB # number_to_human_size(12345) # => 12.1 KB @@ -332,8 +376,10 @@ def number_with_precision(number, options = {}) # number_to_human_size(483989, :precision => 2) # => 470 KB # number_to_human_size(1234567, :precision => 2, :separator => ',') # => 1,2 MB # - # Non-significant zeros after the fractional separator are stripped out by default (set - # :strip_insignificant_zeros to +false+ to change that): + # Non-significant zeros after the fractional separator are + # stripped out by default (set + # :strip_insignificant_zeros to +false+ to change + # that): # number_to_human_size(1234567890123, :precision => 5) # => "1.1229 TB" # number_to_human_size(524288000, :precision => 5) # => "500 MB" def number_to_human_size(number, options = {}) @@ -371,33 +417,55 @@ def number_to_human_size(number, options = {}) DECIMAL_UNITS = {0 => :unit, 1 => :ten, 2 => :hundred, 3 => :thousand, 6 => :million, 9 => :billion, 12 => :trillion, 15 => :quadrillion, -1 => :deci, -2 => :centi, -3 => :mili, -6 => :micro, -9 => :nano, -12 => :pico, -15 => :femto}.freeze - # Pretty prints (formats and approximates) a number in a way it is more readable by humans - # (eg.: 1200000000 becomes "1.2 Billion"). This is useful for numbers that - # can get very large (and too hard to read). + # Pretty prints (formats and approximates) a number in a way it + # is more readable by humans (eg.: 1200000000 becomes "1.2 + # Billion"). This is useful for numbers that can get very large + # (and too hard to read). # - # See number_to_human_size if you want to print a file size. + # See number_to_human_size if you want to print a file + # size. # - # You can also define you own unit-quantifier names if you want to use other decimal units - # (eg.: 1500 becomes "1.5 kilometers", 0.150 becomes "150 milliliters", etc). You may define - # a wide range of unit quantifiers, even fractional ones (centi, deci, mili, etc). + # You can also define you own unit-quantifier names if you want + # to use other decimal units (eg.: 1500 becomes "1.5 + # kilometers", 0.150 becomes "150 milliliters", etc). You may + # define a wide range of unit quantifiers, even fractional ones + # (centi, deci, mili, etc). # # ==== Options - # * :locale - Sets the locale to be used for formatting (defaults to current locale). - # * :precision - Sets the precision of the number (defaults to 3). - # * :significant - If +true+, precision will be the # of significant_digits. If +false+, the # of fractional digits (defaults to +true+) - # * :separator - Sets the separator between the fractional and integer digits (defaults to "."). - # * :delimiter - Sets the thousands delimiter (defaults to ""). - # * :strip_insignificant_zeros - If +true+ removes insignificant zeros after the decimal separator (defaults to +true+) - # * :units - A Hash of unit quantifier names. Or a string containing an i18n scope where to find this hash. It might have the following keys: - # * *integers*: :unit, :ten, :hundred, :thousand, :million, :billion, :trillion, :quadrillion - # * *fractionals*: :deci, :centi, :mili, :micro, :nano, :pico, :femto - # * :format - Sets the format of the output string (defaults to "%n %u"). The field types are: - # %u The quantifier (ex.: 'thousand') - # %n The number - # * :raise - If true, raises +InvalidNumberError+ when the argument is invalid. # + # * :locale - Sets the locale to be used for formatting + # (defaults to current locale). + # * :precision - Sets the precision of the number + # (defaults to 3). + # * :significant - If +true+, precision will be the # + # of significant_digits. If +false+, the # of fractional + # digits (defaults to +true+) + # * :separator - Sets the separator between the + # fractional and integer digits (defaults to "."). + # * :delimiter - Sets the thousands delimiter (defaults + # to ""). + # * :strip_insignificant_zeros - If +true+ removes + # insignificant zeros after the decimal separator (defaults to + # +true+) + # * :units - A Hash of unit quantifier names. Or a + # string containing an i18n scope where to find this hash. It + # might have the following keys: + # * *integers*: :unit, :ten, + # *:hundred, :thousand, :million, + # *:billion, :trillion, + # *:quadrillion + # * *fractionals*: :deci, :centi, + # *:mili, :micro, :nano, + # *:pico, :femto + # * :format - Sets the format of the output string + # (defaults to "%n %u"). The field types are: + # * %u - The quantifier (ex.: 'thousand') + # * %n - The number + # * :raise - If true, raises +InvalidNumberError+ when + # the argument is invalid. # # ==== Examples + # # number_to_human(123) # => "123" # number_to_human(1234) # => "1.23 Thousand" # number_to_human(12345) # => "12.3 Thousand" @@ -414,8 +482,9 @@ def number_to_human_size(number, options = {}) # :separator => ',', # :significant => false) # => "1,2 Million" # - # Unsignificant zeros after the decimal separator are stripped out by default (set - # :strip_insignificant_zeros to +false+ to change that): + # Non-significant zeros after the decimal separator are stripped + # out by default (set :strip_insignificant_zeros to + # +false+ to change that): # number_to_human(12345012345, :significant_digits => 6) # => "12.345 Billion" # number_to_human(500000000, :precision => 5) # => "500 Million" # diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb index fd06bfa2a83d6..8171bea8edf81 100644 --- a/actionpack/lib/action_view/helpers/translation_helper.rb +++ b/actionpack/lib/action_view/helpers/translation_helper.rb @@ -63,6 +63,9 @@ def translate(key, options = {}) alias :t :translate # Delegates to I18n.localize with no additional functionality. + # + # See http://rubydoc.info/github/svenfuchs/i18n/master/I18n/Backend/Base:localize + # for more information. def localize(*args) I18n.localize(*args) end diff --git a/activemodel/README.rdoc b/activemodel/README.rdoc index 1fd75141f879a..0c7089598c27c 100644 --- a/activemodel/README.rdoc +++ b/activemodel/README.rdoc @@ -135,6 +135,16 @@ behavior out of the box: pattern in a Rails App and take advantage of all the standard observer 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] * Making objects serializable diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 97a83e58afc87..25d5d84ce6abb 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -180,6 +180,18 @@ def attribute_method_affix(*affixes) undefine_attribute_methods 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) attribute_method_matchers.each do |matcher| matcher_new = matcher.method_name(new_name).to_s diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb index 41e55dfd0e54a..f6d178db7a792 100644 --- a/activerecord/lib/active_record/relation/spawn_methods.rb +++ b/activerecord/lib/active_record/relation/spawn_methods.rb @@ -7,7 +7,7 @@ module ActiveRecord module SpawnMethods # Merges in the conditions from other, if other is an ActiveRecord::Relation. - # Returns an array representing the union of the resulting records with other, if other is an array. + # Returns an array representing the intersection of the resulting records with other, if other is an array. # # ==== Examples # @@ -16,7 +16,7 @@ module SpawnMethods # # recent_posts = Post.order('created_at DESC').first(5) # 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!) # def merge(other) diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb index fb4b89b87b88d..fd276ccf5d750 100644 --- a/activerecord/lib/active_record/result.rb +++ b/activerecord/lib/active_record/result.rb @@ -28,6 +28,7 @@ def to_hash alias :map! :map alias :collect! :map + # Returns true if there are no records. def empty? rows.empty? end diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb index 5cb528cfe9cbd..fa1dbfdf06569 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -2,30 +2,33 @@ # Extends the class object with class and instance accessors for class 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 + # 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 instance_reader: false + # or instance_accessor: false. + # + # class Person + # cattr_reader :hair_colors, instance_reader: false + # end + # + # Person.new.hair_colors # => NoMethodError def cattr_reader(*syms) options = syms.extract_options! syms.each do |sym| @@ -50,6 +53,43 @@ def #{sym} 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 instance_writer: false + # or instance_accessor: false. + # + # 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) options = syms.extract_options! syms.each do |sym| @@ -75,6 +115,54 @@ def #{sym}=(obj) 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 instance_writer: false. + # To opt out of the instance reader method, pass instance_reader: 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 + # + # Or pass instance_accessor: false, 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) cattr_reader(*syms) cattr_writer(*syms, &blk) diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 120b2a4c28079..cd07c24257207 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -35,8 +35,10 @@ module ActiveSupport # t.is_a?(ActiveSupport::TimeWithZone) # => true # class TimeWithZone + + # Report class name as 'Time' to thwart type checking def self.name - 'Time' # Report class name as 'Time' to thwart type checking + 'Time' end include Comparable diff --git a/guides/source/active_support_core_extensions.textile b/guides/source/active_support_core_extensions.textile index b995d5380657e..8045316e9890d 100644 --- a/guides/source/active_support_core_extensions.textile +++ b/guides/source/active_support_core_extensions.textile @@ -3129,18 +3129,38 @@ The method +beginning_of_day+ returns a timestamp at the beginning of the day (0 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 The method +end_of_day+ returns a timestamp at the end of the day (23:59:59): 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 +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): + + +date = DateTime.new(2010, 6, 7, 19, 55, 25) +date.beginning_of_hour # => Mon Jun 07 19:00:00 +0200 2010 + + +The method +end_of_hour+ returns a timestamp at the end of the hour (hh:59:59): + + +date = DateTime.new(2010, 6, 7, 19, 55, 25) +date.end_of_hour # => Mon Jun 07 19:59:59 +0200 2010 + + ++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+ The method +ago+ receives a number of seconds as argument and returns a timestamp those many seconds ago from midnight: @@ -3208,6 +3228,13 @@ since (in) 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: + + +beginning_of_hour (at_beginning_of_hour) +end_of_hour + + h5. Named Datetimes h6. +DateTime.current+ @@ -3350,6 +3377,8 @@ ago since (in) beginning_of_day (midnight, at_midnight, at_beginning_of_day) end_of_day +beginning_of_hour (at_beginning_of_hour) +end_of_hour beginning_of_week (at_beginning_of_week) end_of_week (at_end_of_week) monday diff --git a/guides/source/configuring.textile b/guides/source/configuring.textile index 5629c82ca0718..c4e54348d4c20 100644 --- a/guides/source/configuring.textile +++ b/guides/source/configuring.textile @@ -186,13 +186,13 @@ The full set of methods that can be used in this block are as follows: * +force_plural+ allows pluralized model names. Defaults to +false+. * +helper+ defines whether or not to generate helpers. Defaults to +true+. * +integration_tool+ defines which integration tool to use. Defaults to +nil+. -* +javascripts+ turns on the hook for javascripts in generators. Used in Rails for when the +scaffold+ generator is ran. Defaults to +true+. +* +javascripts+ turns on the hook for javascripts in generators. Used in Rails for when the +scaffold+ generator is run. Defaults to +true+. * +javascript_engine+ configures the engine to be used (for eg. coffee) when generating assets. Defaults to +nil+. * +orm+ defines which orm to use. Defaults to +false+ and will use Active Record by default. * +performance_tool+ defines which performance tool to use. Defaults to +nil+. * +resource_controller+ defines which generator to use for generating a controller when using +rails generate resource+. Defaults to +:controller+. * +scaffold_controller+ different from +resource_controller+, defines which generator to use for generating a _scaffolded_ controller when using +rails generate scaffold+. Defaults to +:scaffold_controller+. -* +stylesheets+ turns on the hook for stylesheets in generators. Used in Rails for when the +scaffold+ generator is ran, but this hook can be used in other generates as well. Defaults to +true+. +* +stylesheets+ turns on the hook for stylesheets in generators. Used in Rails for when the +scaffold+ generator is run, but this hook can be used in other generates as well. Defaults to +true+. * +stylesheet_engine+ configures the stylesheet engine (for eg. sass) to be used when generating assets. Defaults to +:css+. * +test_framework+ defines which test framework to use. Defaults to +false+ and will use Test::Unit by default. * +template_engine+ defines which template engine to use, such as ERB or Haml. Defaults to +:erb+. @@ -589,13 +589,13 @@ TIP: If you have any ordering dependency in your initializers, you can control t h3. Initialization events -Rails has 5 initialization events which can be hooked into (listed in the order that they are ran): +Rails has 5 initialization events which can be hooked into (listed in the order that they are run): * +before_configuration+: This is run as soon as the application constant inherits from +Rails::Application+. The +config+ calls are evaluated before this happens. * +before_initialize+: This is run directly before the initialization process of the application occurs with the +:bootstrap_hook+ initializer near the beginning of the Rails initialization process. -* +to_prepare+: Run after the initializers are ran for all Railties (including the application itself), but before eager loading and the middleware stack is built. More importantly, will run upon every request in +development+, but only once (during boot-up) in +production+ and +test+. +* +to_prepare+: Run after the initializers are run for all Railties (including the application itself), but before eager loading and the middleware stack is built. More importantly, will run upon every request in +development+, but only once (during boot-up) in +production+ and +test+. * +before_eager_load+: This is run directly before eager loading occurs, which is the default behaviour for the _production_ environment and not for the +development+ environment. @@ -736,7 +736,7 @@ The error occurred while evaluating nil.each *+load_config_initializers+* Loads all Ruby files from +config/initializers+ in the application, railties and engines. The files in this directory can be used to hold configuration settings that should be made after all of the frameworks are loaded. -*+engines_blank_point+* Provides a point-in-initialization to hook into if you wish to do anything before engines are loaded. After this point, all railtie and engine initializers are ran. +*+engines_blank_point+* Provides a point-in-initialization to hook into if you wish to do anything before engines are loaded. After this point, all railtie and engine initializers are run. *+add_generator_templates+* Finds templates for generators at +lib/templates+ for the application, railities and engines and adds these to the +config.generators.templates+ setting, which will make the templates available for all generators to reference. diff --git a/guides/source/debugging_rails_applications.textile b/guides/source/debugging_rails_applications.textile index 903ed59e7b09b..45fa4ada789ad 100644 --- a/guides/source/debugging_rails_applications.textile +++ b/guides/source/debugging_rails_applications.textile @@ -124,7 +124,7 @@ h4. Log Levels When something is logged it's printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the +Rails.logger.level+ method. -The available log levels are: +:debug+, +:info+, +:warn+, +:error+, and +:fatal+, corresponding to the log level numbers from 0 up to 4 respectively. To change the default log level, use +The available log levels are: +:debug+, +:info+, +:warn+, +:error+, +:fatal+, and +:unknown+, corresponding to the log level numbers from 0 up to 5 respectively. To change the default log level, use config.log_level = :warn # In any environment initializer, or