From cccb0e6b9327fb562b72007a012933c9c61a33fa Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Sun, 9 Aug 2009 21:47:32 -0500 Subject: Add validates_format_of :without => /regexp/ option [Elliot Winkler, Peer Allan] [#430 state:resolved] Example : validates_format_of :subdomain, :without => /www|admin|mail/ Signed-off-by: Pratik Naik --- activemodel/CHANGELOG | 6 ++++ activemodel/lib/active_model/validations/format.rb | 37 +++++++++++++++++----- .../cases/validations/format_validation_test.rb | 29 +++++++++++++++++ 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/activemodel/CHANGELOG b/activemodel/CHANGELOG index 142038cc87..26500568ee 100644 --- a/activemodel/CHANGELOG +++ b/activemodel/CHANGELOG @@ -1,5 +1,11 @@ *Edge* +* Add validates_format_of :without => /regexp/ option. #430 [Elliot Winkler, Peer Allan] + + Example : + + validates_format_of :subdomain, :without => /www|admin|mail/ + * Introduce validates_with to encapsulate attribute validations in a class. #2630 [Jeff Dean] * Extracted from Active Record and Active Resource. diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index 6f3b668bf0..3b3dd4b827 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -1,22 +1,30 @@ module ActiveModel module Validations module ClassMethods - # Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression - # provided. + # Validates whether the value of the specified attribute is of the correct form, going by the regular expression provided. + # You can require that the 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 # 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/ + # end + # # Note: use \A and \Z to match the start and end of the string, ^ and $ match the start/end of a line. # - # A regular expression must be provided or else an exception will be raised. + # You must pass either :with or :without as an option. In addition, both must be a regular expression, + # or else an exception will be raised. # # Configuration options: # * :message - A custom error message (default is: "is invalid"). # * :allow_nil - If set to true, skips this validation if the attribute is +nil+ (default is +false+). # * :allow_blank - If set to true, skips this validation if the attribute is blank (default is +false+). - # * :with - The regular expression used to validate the format with (note: must be supplied!). + # * :with - Regular expression that if the attribute matches will result in a successful validation. + # * :without - Regular expression that if the attribute does not match will result in a successful validation. # * :on - Specifies when this validation is active (default is :save, other options :create, :update). # * :if - Specifies a method, proc or string to call to determine if the validation should # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The @@ -25,13 +33,26 @@ module ActiveModel # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The # method, proc or string should return or evaluate to a true or false value. def validates_format_of(*attr_names) - configuration = { :with => nil } - configuration.update(attr_names.extract_options!) + configuration = attr_names.extract_options! + + unless configuration.include?(:with) ^ configuration.include?(:without) # ^ == xor, or "exclusive or" + raise ArgumentError, "Either :with or :without must be supplied (but not both)" + end - raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp) + if configuration[:with] && !configuration[:with].is_a?(Regexp) + raise ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash" + end + + if configuration[:without] && !configuration[:without].is_a?(Regexp) + raise ArgumentError, "A regular expression must be supplied as the :without option of the configuration hash" + end validates_each(attr_names, configuration) do |record, attr_name, value| - unless value.to_s =~ configuration[:with] + if configuration[:with] && value.to_s !~ configuration[:with] + record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) + end + + if configuration[:without] && value.to_s =~ configuration[:without] record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) end end diff --git a/activemodel/test/cases/validations/format_validation_test.rb b/activemodel/test/cases/validations/format_validation_test.rb index 2c06a9dd02..e19e4bf7b3 100644 --- a/activemodel/test/cases/validations/format_validation_test.rb +++ b/activemodel/test/cases/validations/format_validation_test.rb @@ -71,6 +71,35 @@ class PresenceValidationTest < ActiveModel::TestCase assert_equal ["can't be Invalid title"], t.errors[:title] end + def test_validate_format_with_not_option + Topic.validates_format_of(:title, :without => /foo/, :message => "should not contain foo") + t = Topic.new + + t.title = "foobar" + t.valid? + assert_equal ["should not contain foo"], t.errors[:title] + + t.title = "something else" + t.valid? + assert_equal [], t.errors[:title] + end + + def test_validate_format_of_without_any_regexp_should_raise_error + assert_raise(ArgumentError) { Topic.validates_format_of(:title) } + end + + def test_validates_format_of_with_both_regexps_should_raise_error + assert_raise(ArgumentError) { Topic.validates_format_of(:title, :with => /this/, :without => /that/) } + end + + def test_validates_format_of_when_with_isnt_a_regexp_should_raise_error + assert_raise(ArgumentError) { Topic.validates_format_of(:title, :with => "clearly not a regexp") } + end + + def test_validates_format_of_when_not_isnt_a_regexp_should_raise_error + assert_raise(ArgumentError) { Topic.validates_format_of(:title, :without => "clearly not a regexp") } + end + def test_validates_format_of_with_custom_error_using_quotes repair_validations(Developer) do Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message=> "format 'single' and \"double\" quotes" -- cgit v1.2.3 From e202c6c814886251d3c7a9b6a33ba6a8f1a2d448 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 10 Aug 2009 15:24:48 +0100 Subject: Move :with/:without check outside the method generated by validates_format_of --- activemodel/lib/active_model/validations/format.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index 3b3dd4b827..c670dafc7c 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -47,13 +47,13 @@ module ActiveModel raise ArgumentError, "A regular expression must be supplied as the :without option of the configuration hash" end - validates_each(attr_names, configuration) do |record, attr_name, value| - if configuration[:with] && value.to_s !~ configuration[:with] - record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) + if configuration[:with] + validates_each(attr_names, configuration) do |record, attr_name, value| + record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) if value.to_s !~ configuration[:with] end - - if configuration[:without] && value.to_s =~ configuration[:without] - record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) + elsif configuration[:without] + validates_each(attr_names, configuration) do |record, attr_name, value| + record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) if value.to_s =~ configuration[:without] end end end -- cgit v1.2.3 From 55b5cf586ac448aedd4e6f38e368607e729feb48 Mon Sep 17 00:00:00 2001 From: Arthur Zapparoli Date: Mon, 10 Aug 2009 12:07:05 -0300 Subject: Fixed typo in test name and CHANGELOG [#3017 state:resolved] Signed-off-by: Pratik Naik --- activerecord/CHANGELOG | 4 ++-- activerecord/test/cases/validations/uniqueness_validation_test.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index d40251b9ca..acfd470c95 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -2199,7 +2199,7 @@ during calendar reform. #7649, #7724 [fedot, Geoff Buesing] * Escape database name in MySQL adapter when creating and dropping databases. #3409 [anna@wota.jp] -* Disambiguate table names for columns in validates_uniquness_of's WHERE clause. #3423 [alex.borovsky@gmail.com] +* Disambiguate table names for columns in validates_uniqueness_of's WHERE clause. #3423 [alex.borovsky@gmail.com] * .with_scope imposed create parameters now bypass attr_protected [Tobias Lütke] @@ -3714,7 +3714,7 @@ in effect. Added :readonly finder constraint. Calling an association collectio * Escape database name in MySQL adapter when creating and dropping databases. #3409 [anna@wota.jp] -* Disambiguate table names for columns in validates_uniquness_of's WHERE clause. #3423 [alex.borovsky@gmail.com] +* Disambiguate table names for columns in validates_uniqueness_of's WHERE clause. #3423 [alex.borovsky@gmail.com] * .with_scope imposed create parameters now bypass attr_protected [Tobias Lütke] diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index cb123d3498..17ba4e2f8a 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -59,7 +59,7 @@ class UniquenessValidationTest < ActiveRecord::TestCase assert t2.save, "Should now save t2 as unique" end - def test_validates_uniquness_with_newline_chars + def test_validates_uniqueness_with_newline_chars Topic.validates_uniqueness_of(:title, :case_sensitive => false) t = Topic.new("title" => "new\nline") -- cgit v1.2.3 From d574cb31f0406e267edb0e9ed1ffc7998d0da1ee Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 10 Aug 2009 11:53:10 -0500 Subject: Centralize attr method name concatenation in AttributeMethodMatch --- activemodel/lib/active_model/attribute_methods.rb | 33 +++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index de80559036..1ae042e00f 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -135,16 +135,19 @@ module ActiveModel def define_attribute_methods(attr_names) return if attribute_methods_generated? - attr_names.each do |name| - attribute_method_matchers.each do |method| - method_name = "#{method.prefix}#{name}#{method.suffix}" - unless instance_method_already_implemented?(method_name) - generate_method = "define_method_#{method.prefix}attribute#{method.suffix}" + attr_names.each do |attr_name| + attribute_method_matchers.each do |matcher| + unless instance_method_already_implemented?(matcher.method_name(attr_name)) + generate_method = "define_method_#{matcher.prefix}attribute#{matcher.suffix}" if respond_to?(generate_method) - send(generate_method, name) + send(generate_method, attr_name) else - generated_attribute_methods.module_eval("def #{method_name}(*args); send(:#{method.prefix}attribute#{method.suffix}, '#{name}', *args); end", __FILE__, __LINE__) + generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__+1 + def #{matcher.method_name(attr_name)}(*args) + send(:#{matcher.method_missing_target}, '#{attr_name}', *args) + end + STR end end end @@ -180,7 +183,7 @@ module ActiveModel class AttributeMethodMatcher attr_reader :prefix, :suffix - AttributeMethodMatch = Struct.new(:prefix, :base, :suffix) + AttributeMethodMatch = Struct.new(:target, :attr_name) def initialize(options = {}) options.symbolize_keys! @@ -190,11 +193,19 @@ module ActiveModel def match(method_name) if matchdata = @regex.match(method_name) - AttributeMethodMatch.new(matchdata[1], matchdata[2], matchdata[3]) + AttributeMethodMatch.new(method_missing_target, matchdata[2]) else nil end end + + def method_name(attr_name) + "#{prefix}#{attr_name}#{suffix}" + end + + def method_missing_target + :"#{prefix}attribute#{suffix}" + end end def attribute_method_matchers #:nodoc: @@ -214,7 +225,7 @@ module ActiveModel method_name = method_id.to_s if match = match_attribute_method?(method_name) guard_private_attribute_method!(method_name, args) - return __send__("#{match.prefix}attribute#{match.suffix}", match.base, *args, &block) + return __send__(match.target, match.attr_name, *args, &block) end super end @@ -246,7 +257,7 @@ module ActiveModel # The struct's attributes are prefix, base and suffix. def match_attribute_method?(method_name) self.class.send(:attribute_method_matchers).each do |method| - if (match = method.match(method_name)) && attribute_method?(match.base) + if (match = method.match(method_name)) && attribute_method?(match.attr_name) return match end end -- cgit v1.2.3 From 391f978acdf9b4789f9ac301a72b99e05ace64f1 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 10 Aug 2009 11:58:44 -0500 Subject: AMo overrides alias_attribute and manages aliasing all known attribute method matchers --- activemodel/lib/active_model/attribute_methods.rb | 10 ++++++++++ .../lib/active_record/attribute_methods/dirty.rb | 17 ----------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 1ae042e00f..1091ad3095 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -133,6 +133,16 @@ module ActiveModel undefine_attribute_methods end + def alias_attribute(new_name, old_name) + attribute_method_matchers.each do |matcher| + module_eval <<-STR, __FILE__, __LINE__+1 + def #{matcher.method_name(new_name)}(*args) + send(:#{matcher.method_name(old_name)}, *args) + end + STR + end + end + def define_attribute_methods(attr_names) return if attribute_methods_generated? attr_names.each do |attr_name| diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 911c908c8b..37a46f7d88 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -182,23 +182,6 @@ module ActiveRecord old != value end - - module ClassMethods - def self.extended(base) - class << base - alias_method_chain :alias_attribute, :dirty - end - end - - def alias_attribute_with_dirty(new_name, old_name) - alias_attribute_without_dirty(new_name, old_name) - DIRTY_AFFIXES.each do |affixes| - module_eval <<-STR, __FILE__, __LINE__+1 - def #{affixes[:prefix]}#{new_name}#{affixes[:suffix]}; self.#{affixes[:prefix]}#{old_name}#{affixes[:suffix]}; end # def reset_subject!; self.reset_title!; end - STR - end - end - end end end end -- cgit v1.2.3 From f97dae5ebe2f19273d3f92e5ea9baba788c8e89f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 10 Aug 2009 13:51:48 -0500 Subject: Extract common dirty tracking methods in AMo --- activemodel/lib/active_model.rb | 1 + activemodel/lib/active_model/dirty.rb | 112 +++++++++++++++++++ .../lib/active_record/attribute_methods/dirty.rb | 123 ++------------------- 3 files changed, 122 insertions(+), 114 deletions(-) create mode 100644 activemodel/lib/active_model/dirty.rb diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 9bb4cf8b54..b24a929ff5 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -29,6 +29,7 @@ module ActiveModel autoload :AttributeMethods, 'active_model/attribute_methods' autoload :Conversion, 'active_model/conversion' autoload :DeprecatedErrorMethods, 'active_model/deprecated_error_methods' + autoload :Dirty, 'active_model/dirty' autoload :Errors, 'active_model/errors' autoload :Name, 'active_model/naming' autoload :Naming, 'active_model/naming' diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb new file mode 100644 index 0000000000..624c3647ca --- /dev/null +++ b/activemodel/lib/active_model/dirty.rb @@ -0,0 +1,112 @@ +module ActiveModel + # Track unsaved attribute changes. + # + # A newly instantiated object is unchanged: + # person = Person.find_by_name('Uncle Bob') + # person.changed? # => false + # + # Change the name: + # person.name = 'Bob' + # person.changed? # => true + # person.name_changed? # => true + # person.name_was # => 'Uncle Bob' + # person.name_change # => ['Uncle Bob', 'Bob'] + # person.name = 'Bill' + # person.name_change # => ['Uncle Bob', 'Bill'] + # + # Save the changes: + # person.save + # person.changed? # => false + # person.name_changed? # => false + # + # Assigning the same value leaves the attribute unchanged: + # person.name = 'Bill' + # person.name_changed? # => false + # person.name_change # => nil + # + # Which attributes have changed? + # person.name = 'Bob' + # person.changed # => ['name'] + # person.changes # => { 'name' => ['Bill', 'Bob'] } + # + # Resetting an attribute returns it to its original state: + # person.reset_name! # => 'Bill' + # person.changed? # => false + # person.name_changed? # => false + # person.name # => 'Bill' + # + # Before modifying an attribute in-place: + # person.name_will_change! + # person.name << 'y' + # person.name_change # => ['Bill', 'Billy'] + module Dirty + extend ActiveSupport::Concern + include ActiveModel::AttributeMethods + + included do + attribute_method_suffix '_changed?', '_change', '_will_change!', '_was' + attribute_method_affix :prefix => 'reset_', :suffix => '!' + end + + # Do any attributes have unsaved changes? + # person.changed? # => false + # person.name = 'bob' + # person.changed? # => true + def changed? + !changed_attributes.empty? + end + + # List of attributes with unsaved changes. + # person.changed # => [] + # person.name = 'bob' + # person.changed # => ['name'] + def changed + changed_attributes.keys + end + + # Map of changed attrs => [original value, new value]. + # person.changes # => {} + # person.name = 'bob' + # person.changes # => { 'name' => ['bill', 'bob'] } + def changes + changed.inject({}) { |h, attr| h[attr] = attribute_change(attr); h } + end + + private + # Map of change attr => original value. + def changed_attributes + @changed_attributes ||= {} + end + + # Handle *_changed? for +method_missing+. + def attribute_changed?(attr) + changed_attributes.include?(attr) + end + + # Handle *_change for +method_missing+. + def attribute_change(attr) + [changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) + end + + # Handle *_was for +method_missing+. + def attribute_was(attr) + attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr) + end + + # Handle *_will_change! for +method_missing+. + def attribute_will_change!(attr) + begin + value = __send__(attr) + value = value.duplicable? ? value.clone : value + rescue TypeError, NoMethodError + end + + changed_attributes[attr] = value + end + + # Handle reset_*! for +method_missing+. + def reset_attribute!(attr) + __send__("#{attr}=", changed_attributes[attr]) if attribute_changed?(attr) + end + end +end diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 37a46f7d88..b6c4df2a49 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -1,92 +1,21 @@ +require 'active_support/core_ext/object/tap' + module ActiveRecord module AttributeMethods - # Track unsaved attribute changes. - # - # A newly instantiated object is unchanged: - # person = Person.find_by_name('Uncle Bob') - # person.changed? # => false - # - # Change the name: - # person.name = 'Bob' - # person.changed? # => true - # person.name_changed? # => true - # person.name_was # => 'Uncle Bob' - # person.name_change # => ['Uncle Bob', 'Bob'] - # person.name = 'Bill' - # person.name_change # => ['Uncle Bob', 'Bill'] - # - # Save the changes: - # person.save - # person.changed? # => false - # person.name_changed? # => false - # - # Assigning the same value leaves the attribute unchanged: - # person.name = 'Bill' - # person.name_changed? # => false - # person.name_change # => nil - # - # Which attributes have changed? - # person.name = 'Bob' - # person.changed # => ['name'] - # person.changes # => { 'name' => ['Bill', 'Bob'] } - # - # Resetting an attribute returns it to its original state: - # person.reset_name! # => 'Bill' - # person.changed? # => false - # person.name_changed? # => false - # person.name # => 'Bill' - # - # Before modifying an attribute in-place: - # person.name_will_change! - # person.name << 'y' - # person.name_change # => ['Bill', 'Billy'] module Dirty extend ActiveSupport::Concern - - DIRTY_AFFIXES = [ - { :suffix => '_changed?' }, - { :suffix => '_change' }, - { :suffix => '_will_change!' }, - { :suffix => '_was' }, - { :prefix => 'reset_', :suffix => '!' } - ] + include ActiveModel::Dirty included do - attribute_method_affix *DIRTY_AFFIXES - - alias_method_chain :save, :dirty - alias_method_chain :save!, :dirty - alias_method_chain :update, :dirty - alias_method_chain :reload, :dirty + alias_method_chain :save, :dirty + alias_method_chain :save!, :dirty + alias_method_chain :update, :dirty + alias_method_chain :reload, :dirty superclass_delegating_accessor :partial_updates self.partial_updates = true end - # Do any attributes have unsaved changes? - # person.changed? # => false - # person.name = 'bob' - # person.changed? # => true - def changed? - !changed_attributes.empty? - end - - # List of attributes with unsaved changes. - # person.changed # => [] - # person.name = 'bob' - # person.changed # => ['name'] - def changed - changed_attributes.keys - end - - # Map of changed attrs => [original value, new value]. - # person.changes # => {} - # person.name = 'bob' - # person.changes # => { 'name' => ['bill', 'bob'] } - def changes - changed.inject({}) { |h, attr| h[attr] = attribute_change(attr); h } - end - # Attempts to +save+ the record and clears changed attributes if successful. def save_with_dirty(*args) #:nodoc: if status = save_without_dirty(*args) @@ -97,49 +26,15 @@ module ActiveRecord # Attempts to save! the record and clears changed attributes if successful. def save_with_dirty!(*args) #:nodoc: - status = save_without_dirty!(*args) - changed_attributes.clear - status + save_without_dirty!(*args).tap { changed_attributes.clear } end # reload the record and clears changed attributes. def reload_with_dirty(*args) #:nodoc: - record = reload_without_dirty(*args) - changed_attributes.clear - record + reload_without_dirty(*args).tap { changed_attributes.clear } end private - # Map of change attr => original value. - def changed_attributes - @changed_attributes ||= {} - end - - # Handle *_changed? for +method_missing+. - def attribute_changed?(attr) - changed_attributes.include?(attr) - end - - # Handle *_change for +method_missing+. - def attribute_change(attr) - [changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) - end - - # Handle *_was for +method_missing+. - def attribute_was(attr) - attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr) - end - - # Handle reset_*! for +method_missing+. - def reset_attribute!(attr) - self[attr] = changed_attributes[attr] if attribute_changed?(attr) - end - - # Handle *_will_change! for +method_missing+. - def attribute_will_change!(attr) - changed_attributes[attr] = clone_attribute_value(:read_attribute, attr) - end - # Wrap write_attribute to remember original attribute value. def write_attribute(attr, value) attr = attr.to_s -- cgit v1.2.3 From d9c4087a9e73ea31dbb83baa26c02904039fb480 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 10 Aug 2009 21:02:06 +0100 Subject: Unify hm:t#create and create! implementation --- .../associations/has_many_through_association.rb | 30 ++++++++++------------ 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 292da58831..c662bb8ec4 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -8,25 +8,11 @@ module ActiveRecord alias_method :new, :build def create!(attrs = nil) - ensure_owner_is_not_new - - transaction do - self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association! } : @reflection.create_association!) - object - end + create_record(attrs, true) end def create(attrs = nil) - ensure_owner_is_not_new - - transaction do - object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association - raise_on_type_mismatch(object) - add_record_to_target_with_callbacks(object) do |r| - insert_record(object, false) - end - object - end + create_record(attrs, false) end def destroy(*records) @@ -44,8 +30,18 @@ module ActiveRecord return @target.size if loaded? return count end - + protected + def create_record(attrs, force = true) + ensure_owner_is_not_new + + transaction do + object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association + add_record_to_target_with_callbacks(object) {|r| insert_record(object, force) } + object + end + end + def target_reflection_has_associated_record? if @reflection.through_reflection.macro == :belongs_to && @owner[@reflection.through_reflection.primary_key_name].blank? false -- cgit v1.2.3 From 50b83984f111552b91a25b3b1d139f65efcc8e8f Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 10 Aug 2009 21:06:49 +0100 Subject: Remove unnecessary scoping and validation checks from hm:t#create --- .../lib/active_record/associations/has_many_through_association.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index c662bb8ec4..f09289ada3 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -36,7 +36,7 @@ module ActiveRecord ensure_owner_is_not_new transaction do - object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association + object = @reflection.klass.new(attrs) add_record_to_target_with_callbacks(object) {|r| insert_record(object, force) } object end -- cgit v1.2.3 From ad28e0037b1d27494b846b1e65db874c37445e91 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 10 Aug 2009 21:20:01 +0100 Subject: Remove unnecessary scoping for creating hm:t join record --- .../lib/active_record/associations/has_many_through_association.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index f09289ada3..829f0ac0c5 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -65,9 +65,10 @@ module ActiveRecord return false unless record.save(validate) end end - through_reflection = @reflection.through_reflection - klass = through_reflection.klass - @owner.send(@reflection.through_reflection.name).proxy_target << klass.send(:with_scope, :create => construct_join_attributes(record)) { through_reflection.create_association! } + + through_association = @owner.send(@reflection.through_reflection.name) + through_record = through_association.create!(construct_join_attributes(record)) + through_association.proxy_target << through_record end # TODO - add dependent option support -- cgit v1.2.3 From d0f891ae0215d7963b3918f3847ee4c015a6b90c Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 10 Aug 2009 21:29:48 +0100 Subject: Rewrite hm:t#create tests using assert_no_difference and assert_difference --- .../has_many_through_associations_test.rb | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 59985374d3..a9d4d88148 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -180,27 +180,23 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase end def test_associate_with_create_and_invalid_options - peeps = companies(:first_firm).developers.count - assert_nothing_raised { companies(:first_firm).developers.create(:name => '0') } - assert_equal peeps, companies(:first_firm).developers.count + firm = companies(:first_firm) + assert_no_difference('firm.developers.count') { assert_nothing_raised { firm.developers.create(:name => '0') } } end def test_associate_with_create_and_valid_options - peeps = companies(:first_firm).developers.count - assert_nothing_raised { companies(:first_firm).developers.create(:name => 'developer') } - assert_equal peeps + 1, companies(:first_firm).developers.count + firm = companies(:first_firm) + assert_difference('firm.developers.count', 1) { firm.developers.create(:name => 'developer') } end def test_associate_with_create_bang_and_invalid_options - peeps = companies(:first_firm).developers.count - assert_raises(ActiveRecord::RecordInvalid) { companies(:first_firm).developers.create!(:name => '0') } - assert_equal peeps, companies(:first_firm).developers.count + firm = companies(:first_firm) + assert_no_difference('firm.developers.count') { assert_raises(ActiveRecord::RecordInvalid) { firm.developers.create!(:name => '0') } } end def test_associate_with_create_bang_and_valid_options - peeps = companies(:first_firm).developers.count - assert_nothing_raised { companies(:first_firm).developers.create!(:name => 'developer') } - assert_equal peeps + 1, companies(:first_firm).developers.count + firm = companies(:first_firm) + assert_difference('firm.developers.count', 1) { firm.developers.create!(:name => 'developer') } end def test_clear_associations -- cgit v1.2.3 From d15ddf04ec6fb0cd6d350ba57d9981ebee3eddd0 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 10 Aug 2009 18:34:24 -0400 Subject: Allow delegating to nil, because the method might actually exist on it --- .../lib/active_support/core_ext/module/delegation.rb | 13 +++++++++---- activesupport/test/core_ext/module_test.rb | 13 ++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index 11e01437aa..df8aefea5a 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -120,10 +120,15 @@ class Module end module_eval(<<-EOS, file, line) - def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block) - #{on_nil} if #{to}.nil? - #{to}.__send__(#{method.inspect}, *args, &block) # client && client.__send__(:name, *args, &block) - end # end + def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block) + #{to}.__send__(#{method.inspect}, *args, &block) # client.__send__(:name, *args, &block) + rescue NoMethodError # rescue NoMethodError + if #{to}.nil? # if client.nil? + #{on_nil} + else # else + raise # raise + end # end + end # end EOS end end diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index f8387ae4ab..87f056ea85 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -32,7 +32,7 @@ end Somewhere = Struct.new(:street, :city) Someone = Struct.new(:name, :place) do - delegate :street, :city, :to => :place + delegate :street, :city, :to_f, :to => :place delegate :state, :to => :@place delegate :upcase, :to => "place.city" end @@ -44,6 +44,7 @@ end Project = Struct.new(:description, :person) do delegate :name, :to => :person, :allow_nil => true + delegate :to_f, :to => :description, :allow_nil => true end class Name @@ -145,6 +146,16 @@ class ModuleTest < Test::Unit::TestCase assert_raise(RuntimeError) { david.street } end + def test_delegation_to_method_that_exists_on_nil + nil_person = Someone.new(nil) + assert_equal 0.0, nil_person.to_f + end + + def test_delegation_to_method_that_exists_on_nil_when_allowing_nil + nil_project = Project.new(nil) + assert_equal 0.0, nil_project.to_f + end + def test_parent assert_equal Yz::Zy, Yz::Zy::Cd.parent assert_equal Yz, Yz::Zy.parent -- cgit v1.2.3 From 0a558b36eb3858ceeb926ada1388b0bd41da11f7 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 11 Aug 2009 02:36:50 +0100 Subject: Add tests for hm:t#push failures --- .../associations/has_many_through_associations_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index a9d4d88148..5f13b66d11 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -199,6 +199,24 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_difference('firm.developers.count', 1) { firm.developers.create!(:name => 'developer') } end + def test_push_with_invalid_record + firm = companies(:first_firm) + assert_raises(ActiveRecord::RecordInvalid) { firm.developers << Developer.new(:name => '0') } + end + + def test_push_with_invalid_join_record + repair_validations(Contract) do + Contract.validate {|r| r.errors[:base] << 'Invalid Contract' } + + firm = companies(:first_firm) + lifo = Developer.new(:name => 'lifo') + assert_raises(ActiveRecord::RecordInvalid) { firm.developers << lifo } + + lifo = Developer.create!(:name => 'lifo') + assert_raises(ActiveRecord::RecordInvalid) { firm.developers << lifo } + end + end + def test_clear_associations assert_queries(2) { posts(:welcome);posts(:welcome).people(true) } -- cgit v1.2.3 From 04d4537cd40d0415d15af4395213632735c8683f Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sun, 9 Aug 2009 07:14:24 -0300 Subject: This change causes some failing tests, but it should be possible to make them pass with minimal performance impact. --- actionpack/examples/minimal.rb | 6 ++++-- actionpack/lib/action_dispatch/http/request.rb | 23 ++++++++++++----------- actionpack/lib/action_view/template/resolver.rb | 6 ++++-- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/actionpack/examples/minimal.rb b/actionpack/examples/minimal.rb index a9015da053..b3733b487b 100644 --- a/actionpack/examples/minimal.rb +++ b/actionpack/examples/minimal.rb @@ -91,6 +91,8 @@ class HttpPostController < ActionController::Metal end end +ActionController::Base.use_accept_header = false + unless ENV["PROFILE"] Runner.run(BasePostController.action(:overhead), N, 'overhead', false) Runner.run(BasePostController.action(:index), N, 'index', false) @@ -108,10 +110,10 @@ unless ENV["PROFILE"] Runner.run(BasePostController.action(:show_template), N, 'template') end else - Runner.run(BasePostController.action(:many_partials), N, 'many_partials') + Runner.run(BasePostController.action(ENV["PROFILE"].to_sym), N, ENV["PROFILE"]) require "ruby-prof" RubyProf.start - Runner.run(BasePostController.action(:many_partials), N, 'many_partials') + Runner.run(BasePostController.action(ENV["PROFILE"].to_sym), N, ENV["PROFILE"]) result = RubyProf.stop printer = RubyProf::CallStackPrinter.new(result) printer.print(File.open("output.html", "w")) diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index 4190fa21cd..958a541436 100755 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -176,17 +176,18 @@ module ActionDispatch # Expand raw_formats by converting Mime::ALL to the Mime::SET. # def formats - if ActionController::Base.use_accept_header - raw_formats.tap do |ret| - if ret == ONLY_ALL - ret.replace Mime::SET - elsif all = ret.index(Mime::ALL) - ret.delete_at(all) && ret.insert(all, *Mime::SET) - end - end - else - raw_formats + Mime::SET - end + return raw_formats + # if ActionController::Base.use_accept_header + # raw_formats.tap do |ret| + # if ret == ONLY_ALL + # ret.replace Mime::SET + # elsif all = ret.index(Mime::ALL) + # ret.delete_at(all) && ret.insert(all, *Mime::SET) + # end + # end + # else + # raw_formats + Mime::SET + # end end # Sets the \format by string extension, which can be used to force custom formats diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index ebfc6cc8ce..3bd2acae7a 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -41,8 +41,10 @@ module ActionView end def handler_glob - e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join - "{#{e}}" + @handler_glob ||= begin + e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join + "{#{e}}" + end end def formats_glob -- cgit v1.2.3 From 02d9dd900048407ef555cf09b0038a57ae924b0a Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sun, 9 Aug 2009 09:46:50 -0300 Subject: Add some more caching to the lookup --- actionpack/lib/abstract_controller/layouts.rb | 22 ++++++++++++++---- actionpack/lib/action_view/template/resolver.rb | 30 +++++++++++++++---------- actionpack/test/dispatch/request_test.rb | 4 ++-- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index 0063d54149..d7317b415c 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -15,6 +15,18 @@ module AbstractController klass._write_layout_method end + def cache_layout(details) + layout = @found_layouts ||= {} + values = details.values_at(:formats, :locale) + + # Cache nil + if layout.key?(values) + return layout[values] + else + layout[values] = yield + end + end + # Specify the layout to use for this class. # # If the specified layout is a: @@ -76,10 +88,12 @@ module AbstractController when nil self.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1 def _layout(details) - if view_paths.exists?("#{_implied_layout_name}", details, "layouts") - "#{_implied_layout_name}" - else - super + self.class.cache_layout(details) do + if view_paths.exists?("#{_implied_layout_name}", details, "layouts") + "#{_implied_layout_name}" + else + super + end end end ruby_eval diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 3bd2acae7a..10f664736f 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -62,6 +62,10 @@ module ActionView class FileSystemResolver < Resolver + def self.cached_glob + @@cached_glob ||= {} + end + def initialize(path, options = {}) raise ArgumentError, "path already is a Resolver class" if path.is_a?(Resolver) super(options) @@ -107,20 +111,22 @@ module ActionView # :api: plugin def details_to_glob(name, details, prefix, partial, root) - path = "" - path << "#{prefix}/" unless prefix.empty? - path << (partial ? "_#{name}" : name) - - extensions = "" - [:locales, :formats].each do |k| - extensions << if exts = details[k] - '{' + exts.map {|e| ".#{e},"}.join + '}' - else - k == :formats ? formats_glob : '' + self.class.cached_glob[[name, prefix, partial, details, root]] ||= begin + path = "" + path << "#{prefix}/" unless prefix.empty? + path << (partial ? "_#{name}" : name) + + extensions = "" + [:locales, :formats].each do |k| + extensions << if exts = details[k] + '{' + exts.map {|e| ".#{e},"}.join + '}' + else + k == :formats ? formats_glob : '' + end end - end - "#{root}#{path}#{extensions}#{handler_glob}" + "#{root}#{path}#{extensions}#{handler_glob}" + end end # TODO: fix me diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index f3500fca34..b626063df4 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -427,7 +427,7 @@ class RequestTest < ActiveSupport::TestCase request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8' request.expects(:parameters).at_least_once.returns({}) - assert_equal with_set(Mime::XML, Mime::HTML), request.formats + assert_equal with_set(Mime::XML, Mime::HTML, Mime::ALL), request.formats end with_accept_header false do @@ -460,7 +460,7 @@ protected end def with_set(*args) - args + Mime::SET + args end def with_accept_header(value) -- cgit v1.2.3 From bef7576c09bbd51aeeb8e83b4cf24a994a0256b0 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sun, 9 Aug 2009 09:59:54 -0300 Subject: Add a few more benches --- actionpack/examples/minimal.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/actionpack/examples/minimal.rb b/actionpack/examples/minimal.rb index b3733b487b..c4e5ffd36e 100644 --- a/actionpack/examples/minimal.rb +++ b/actionpack/examples/minimal.rb @@ -73,10 +73,18 @@ class BasePostController < ActionController::Base render :partial => "/many_partials" end + def hundred_partials + render :partial => "/hundred_partials" + end + def partial_collection render :partial => "/collection", :collection => [1,2,3,4,5,6,7,8,9,10] end + def large_collection + render :partial => "/collection", :collection => (1...100).to_a + end + def show_template render :template => "template" end @@ -99,6 +107,8 @@ unless ENV["PROFILE"] Runner.run(BasePostController.action(:partial), N, 'partial', false) Runner.run(BasePostController.action(:many_partials), N, 'many_partials', false) Runner.run(BasePostController.action(:partial_collection), N, 'collection', false) + Runner.run(BasePostController.action(:hundred_partials), N, 'hundred_partials', false) + Runner.run(BasePostController.action(:large_collection), N, 'large_collection', false) Runner.run(BasePostController.action(:show_template), N, 'template', false) (ENV["M"] || 1).to_i.times do @@ -107,6 +117,8 @@ unless ENV["PROFILE"] Runner.run(BasePostController.action(:partial), N, 'partial') Runner.run(BasePostController.action(:many_partials), N, 'many_partials') Runner.run(BasePostController.action(:partial_collection), N, 'collection') + Runner.run(BasePostController.action(:hundred_partials), N, 'hundred_partials') + Runner.run(BasePostController.action(:large_collection), N, 'large_collection') Runner.run(BasePostController.action(:show_template), N, 'template') end else -- cgit v1.2.3 From 4945d8223964d4ccb3c0a0f4107f15ae1c6c4a09 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 10 Aug 2009 02:54:17 -0400 Subject: Further experimentation. Was able to cut the cost of rendering 100 partials in a collection in half. To discuss: What are the desired semantics (if any) for layouts in a collection. There are no tests for it at present, and I'm not sure if it's needed at all. Deprecated on this branch: `object` pointing at the current object in partials. You can still use the partial name, or use :as to achieve the same thing. This is obviously up for discussion. --- actionpack/examples/minimal.rb | 4 +- actionpack/examples/views/_hundred_partials.erb | 12 ++++++ actionpack/lib/action_view/render/partials.rb | 49 ++++++++++++++++++++----- actionpack/lib/action_view/test_case.rb | 20 +++++----- 4 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 actionpack/examples/views/_hundred_partials.erb diff --git a/actionpack/examples/minimal.rb b/actionpack/examples/minimal.rb index c4e5ffd36e..90435d0b89 100644 --- a/actionpack/examples/minimal.rb +++ b/actionpack/examples/minimal.rb @@ -104,22 +104,22 @@ ActionController::Base.use_accept_header = false unless ENV["PROFILE"] Runner.run(BasePostController.action(:overhead), N, 'overhead', false) Runner.run(BasePostController.action(:index), N, 'index', false) + Runner.run(BasePostController.action(:show_template), N, 'template', false) Runner.run(BasePostController.action(:partial), N, 'partial', false) Runner.run(BasePostController.action(:many_partials), N, 'many_partials', false) Runner.run(BasePostController.action(:partial_collection), N, 'collection', false) Runner.run(BasePostController.action(:hundred_partials), N, 'hundred_partials', false) Runner.run(BasePostController.action(:large_collection), N, 'large_collection', false) - Runner.run(BasePostController.action(:show_template), N, 'template', false) (ENV["M"] || 1).to_i.times do Runner.run(BasePostController.action(:overhead), N, 'overhead') Runner.run(BasePostController.action(:index), N, 'index') + Runner.run(BasePostController.action(:show_template), N, 'template') Runner.run(BasePostController.action(:partial), N, 'partial') Runner.run(BasePostController.action(:many_partials), N, 'many_partials') Runner.run(BasePostController.action(:partial_collection), N, 'collection') Runner.run(BasePostController.action(:hundred_partials), N, 'hundred_partials') Runner.run(BasePostController.action(:large_collection), N, 'large_collection') - Runner.run(BasePostController.action(:show_template), N, 'template') end else Runner.run(BasePostController.action(ENV["PROFILE"].to_sym), N, ENV["PROFILE"]) diff --git a/actionpack/examples/views/_hundred_partials.erb b/actionpack/examples/views/_hundred_partials.erb new file mode 100644 index 0000000000..15e99c1b68 --- /dev/null +++ b/actionpack/examples/views/_hundred_partials.erb @@ -0,0 +1,12 @@ +<% 10.times do %> + <%= render :partial => '/hello' %> + <%= render :partial => '/hello' %> + <%= render :partial => '/hello' %> + <%= render :partial => '/hello' %> + <%= render :partial => '/hello' %> + <%= render :partial => '/hello' %> + <%= render :partial => '/hello' %> + <%= render :partial => '/hello' %> + <%= render :partial => '/hello' %> + <%= render :partial => '/hello' %> +<% end %> \ No newline at end of file diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 64f08c447d..2aa3bd7db8 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -207,9 +207,7 @@ module ActionView end def render_collection - # Even if no template is rendered, this will ensure that the MIME type - # for the empty response is the same as the provided template - @options[:_template] = default_template = find_template + @options[:_template] = template = find_template return nil if collection.blank? @@ -217,15 +215,48 @@ module ActionView spacer = find_template(@options[:spacer_template]).render(@view, @locals) end - segments = [] + result = template ? collection_with_template(template) : collection_without_template + result.join(spacer) + end + + def collection_with_template(template) + options = @options + + segments, locals, as = [], @locals, options[:as] + + [].tap do |segments| + variable_name = template.variable_name + counter_name = template.counter_name + locals[counter_name] = -1 - collection.each_with_index do |object, index| - template = default_template || find_template(partial_path(object)) - @locals[template.counter_name] = index - segments << render_template(template, object) + collection.each do |object| + locals[counter_name] += 1 + locals[variable_name] = object + locals[as] = object if as + + segments << template.render(@view, locals) + end end + end - segments.join(spacer) + def collection_without_template + options = @options + + segments, locals, as = [], @locals, options[:as] + index, template = -1, nil + + [].tap do |segments| + collection.each do |object| + template = find_template(partial_path(object)) + locals[template.counter_name] = (index += 1) + locals[template.variable_name] = object + locals[as] = object if as + + segments << template.render(@view, locals) + end + + @options[:_template] = template + end end def render_template(template, object = @object) diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index e51744d095..b317b6dc1a 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -9,14 +9,16 @@ module ActionView end attr_internal :rendered - alias_method :_render_template_without_template_tracking, :_render_single_template - def _render_single_template(template, local_assigns, &block) - if template.respond_to?(:identifier) && template.present? - @_rendered[:partials][template] += 1 if template.partial? - @_rendered[:template] ||= [] - @_rendered[:template] << template - end - _render_template_without_template_tracking(template, local_assigns, &block) + end + + class Template + alias_method :render_without_tracking, :render + def render(view, locals, &blk) + rendered = view.rendered + rendered[:partials][self] += 1 if partial? + rendered[:template] ||= [] + rendered[:template] << self + render_without_tracking(view, locals, &blk) end end @@ -68,7 +70,7 @@ module ActionView def initialize @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new - + @params = {} send(:initialize_current_url) end -- cgit v1.2.3 From 945a7df9f8ad2986ba2351b331a9f7225dfaf61c Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 10 Aug 2009 03:28:21 -0400 Subject: Make large_collection 1,000 partials --- actionpack/examples/minimal.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/actionpack/examples/minimal.rb b/actionpack/examples/minimal.rb index 90435d0b89..62b71de2cb 100644 --- a/actionpack/examples/minimal.rb +++ b/actionpack/examples/minimal.rb @@ -82,7 +82,7 @@ class BasePostController < ActionController::Base end def large_collection - render :partial => "/collection", :collection => (1...100).to_a + render :partial => "/collection", :collection => (1...1000).to_a end def show_template @@ -102,14 +102,14 @@ end ActionController::Base.use_accept_header = false unless ENV["PROFILE"] - Runner.run(BasePostController.action(:overhead), N, 'overhead', false) - Runner.run(BasePostController.action(:index), N, 'index', false) - Runner.run(BasePostController.action(:show_template), N, 'template', false) - Runner.run(BasePostController.action(:partial), N, 'partial', false) - Runner.run(BasePostController.action(:many_partials), N, 'many_partials', false) - Runner.run(BasePostController.action(:partial_collection), N, 'collection', false) - Runner.run(BasePostController.action(:hundred_partials), N, 'hundred_partials', false) - Runner.run(BasePostController.action(:large_collection), N, 'large_collection', false) + Runner.run(BasePostController.action(:overhead), 1, 'overhead', false) + Runner.run(BasePostController.action(:index), 1, 'index', false) + Runner.run(BasePostController.action(:show_template), 1, 'template', false) + Runner.run(BasePostController.action(:partial), 1, 'partial', false) + Runner.run(BasePostController.action(:many_partials), 1, 'many_partials', false) + Runner.run(BasePostController.action(:partial_collection), 1, 'collection', false) + Runner.run(BasePostController.action(:hundred_partials), 1, 'hundred_partials', false) + Runner.run(BasePostController.action(:large_collection), 1, 'large_collection', false) (ENV["M"] || 1).to_i.times do Runner.run(BasePostController.action(:overhead), N, 'overhead') @@ -122,7 +122,7 @@ unless ENV["PROFILE"] Runner.run(BasePostController.action(:large_collection), N, 'large_collection') end else - Runner.run(BasePostController.action(ENV["PROFILE"].to_sym), N, ENV["PROFILE"]) + Runner.run(BasePostController.action(ENV["PROFILE"].to_sym), 1, ENV["PROFILE"]) require "ruby-prof" RubyProf.start Runner.run(BasePostController.action(ENV["PROFILE"].to_sym), N, ENV["PROFILE"]) -- cgit v1.2.3 From 9e62d6d1c0c53e8b03c9659500e2b5549a1fd2ec Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 10 Aug 2009 08:57:44 -0400 Subject: Tentatively accept the ":as or :object, but not both" solution --- actionpack/lib/action_view/render/partials.rb | 43 +++++++++++----------- .../test/fixtures/test/_customer_with_var.erb | 2 +- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 2aa3bd7db8..83175ab4cf 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -184,6 +184,7 @@ module ActionView def initialize(view_context, options, block) partial = options[:partial] + @memo = {} @view = view_context @options = options @locals = options[:locals] || {} @@ -222,41 +223,39 @@ module ActionView def collection_with_template(template) options = @options - segments, locals, as = [], @locals, options[:as] + segments, locals, as = [], @locals, options[:as] || :object - [].tap do |segments| - variable_name = template.variable_name - counter_name = template.counter_name - locals[counter_name] = -1 + variable_name = template.variable_name + counter_name = template.counter_name + locals[counter_name] = -1 - collection.each do |object| - locals[counter_name] += 1 - locals[variable_name] = object - locals[as] = object if as + collection.each do |object| + locals[counter_name] += 1 + locals[variable_name] = object + locals[as] = object if as - segments << template.render(@view, locals) - end + segments << template.render(@view, locals) end + segments end def collection_without_template options = @options - segments, locals, as = [], @locals, options[:as] + segments, locals, as = [], @locals, options[:as] || :object index, template = -1, nil - [].tap do |segments| - collection.each do |object| - template = find_template(partial_path(object)) - locals[template.counter_name] = (index += 1) - locals[template.variable_name] = object - locals[as] = object if as - - segments << template.render(@view, locals) - end + collection.each do |object| + template = find_template(partial_path(object)) + locals[template.counter_name] = (index += 1) + locals[template.variable_name] = object + locals[as] = object if as - @options[:_template] = template + segments << template.render(@view, locals) end + + @options[:_template] = template + segments end def render_template(template, object = @object) diff --git a/actionpack/test/fixtures/test/_customer_with_var.erb b/actionpack/test/fixtures/test/_customer_with_var.erb index 3379246b7e..c28824936b 100644 --- a/actionpack/test/fixtures/test/_customer_with_var.erb +++ b/actionpack/test/fixtures/test/_customer_with_var.erb @@ -1 +1 @@ -<%= customer.name %> <%= object.name %> <%= customer_with_var.name %> \ No newline at end of file +<%= customer.name %> <%= customer.name %> <%= customer_with_var.name %> \ No newline at end of file -- cgit v1.2.3 From 0adbeeb0c92c6de2e4a148e4b54d56cd4a325800 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 10 Aug 2009 11:40:41 -0400 Subject: Got overhead down from 127 to 85. All tests pass: * Tentatively replaced HeaderHash with SimpleHeaderHash, which does not preserve case but does handle converting Arrays to Strings in to_hash. This requires further discussion. * Moved default_charset to ActionDispatch::Response to avoid having to hop over to ActionController. Ideally, this would be a constant on AD::Response, but some tests expect to be able to change it dynamically and I didn't want to change them yet. * Completely override #initialize from Rack::Response. Previously, it was creating a HeaderHash, and then we were creating an entirely new one. There is no way to call super without incurring the overhead of creating a HeaderHash. * Override #write from Rack::Response. Its implementation tracks Content-Length, and doing so adds additional overhead that could be mooted if other middleware changes the body. It is more efficiently done at the top-level server. * Change sending_file to an instance_variable instead of header inspection. In general, if a state is important, it should be set as a property of the response not reconstructed later. * Set the Etag to @body instead of .body. AS::Cache.expand_cache_key handles Arrays fine, and it's more efficient to let it handle the body parts, since it is not forced to create a joined String. * If we detect the default cache control case, just set it, rather than setting the constituent parts and then running the normal (expensive) code to generate the string. --- .../lib/action_controller/metal/compatibility.rb | 5 +- .../lib/action_controller/metal/streaming.rb | 3 +- .../lib/action_controller/testing/process.rb | 2 +- actionpack/lib/action_dispatch/http/response.rb | 92 +++++++++++++--------- .../core_ext/class/attribute_accessors.rb | 5 +- 5 files changed, 65 insertions(+), 42 deletions(-) diff --git a/actionpack/lib/action_controller/metal/compatibility.rb b/actionpack/lib/action_controller/metal/compatibility.rb index f94d1c669c..a008b53d45 100644 --- a/actionpack/lib/action_controller/metal/compatibility.rb +++ b/actionpack/lib/action_controller/metal/compatibility.rb @@ -25,8 +25,9 @@ module ActionController cattr_accessor :relative_url_root self.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT'] - cattr_accessor :default_charset - self.default_charset = "utf-8" + class << self + delegate :default_charset=, :to => "ActionDispatch::Response" + end # cattr_reader :protected_instance_variables cattr_accessor :protected_instance_variables diff --git a/actionpack/lib/action_controller/metal/streaming.rb b/actionpack/lib/action_controller/metal/streaming.rb index 57318e8747..4761763a26 100644 --- a/actionpack/lib/action_controller/metal/streaming.rb +++ b/actionpack/lib/action_controller/metal/streaming.rb @@ -145,7 +145,6 @@ module ActionController #:nodoc: def send_data(data, options = {}) #:doc: logger.info "Sending data #{options[:filename]}" if logger send_file_headers! options.merge(:length => data.bytesize) - @performed_render = false render :status => options[:status], :text => data end @@ -175,6 +174,8 @@ module ActionController #:nodoc: 'Content-Transfer-Encoding' => 'binary' ) + response.sending_file = true + # Fix a problem with IE 6.0 on opening downloaded files: # If Cache-Control: no-cache is set (which Rails does by default), # IE removes the file it just downloaded from its cache immediately diff --git a/actionpack/lib/action_controller/testing/process.rb b/actionpack/lib/action_controller/testing/process.rb index d32d5562e8..147a7e7631 100644 --- a/actionpack/lib/action_controller/testing/process.rb +++ b/actionpack/lib/action_controller/testing/process.rb @@ -52,7 +52,7 @@ module ActionController #:nodoc: class TestResponse < ActionDispatch::TestResponse def recycle! @status = 200 - @header = Rack::Utils::HeaderHash.new + @header = ActionDispatch::Response::SimpleHeaderHash.new @writer = lambda { |x| @body << x } @block = nil @length = 0 diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb index 03d1780b77..1d3cf63984 100644 --- a/actionpack/lib/action_dispatch/http/response.rb +++ b/actionpack/lib/action_dispatch/http/response.rb @@ -32,18 +32,42 @@ module ActionDispatch # :nodoc: # end # end class Response < Rack::Response + class SimpleHeaderHash < Hash + def to_hash + result = {} + each do |k,v| + v = v.join("\n") if v.is_a?(Array) + result[k] = v + end + result + end + end + attr_accessor :request attr_reader :cache_control - attr_writer :header + attr_writer :header, :sending_file alias_method :headers=, :header= - delegate :default_charset, :to => 'ActionController::Base' - def initialize - super + @status = 200 + @header = SimpleHeaderHash.new @cache_control = {} - @header = Rack::Utils::HeaderHash.new + + @writer = lambda { |x| @body << x } + @block = nil + @length = 0 + + @body = [] + @sending_file = false + + yield self if block_given? + end + + def write(str) + s = str.to_s + @writer.call s + str end def status=(status) @@ -128,20 +152,20 @@ module ActionDispatch # :nodoc: end end - def sending_file? - headers["Content-Transfer-Encoding"] == "binary" - end + CONTENT_TYPE = "Content-Type" + + cattr_accessor(:default_charset) { "utf-8" } def assign_default_content_type_and_charset! - return if !headers["Content-Type"].blank? + return if !headers[CONTENT_TYPE].blank? @content_type ||= Mime::HTML - @charset ||= default_charset + @charset ||= self.class.default_charset type = @content_type.to_s.dup - type << "; charset=#{@charset}" unless sending_file? + type << "; charset=#{@charset}" unless @sending_file - headers["Content-Type"] = type + headers[CONTENT_TYPE] = type end def prepare! @@ -168,17 +192,6 @@ module ActionDispatch # :nodoc: str end - def set_cookie(key, value) - if value.has_key?(:http_only) - ActiveSupport::Deprecation.warn( - "The :http_only option in ActionController::Response#set_cookie " + - "has been renamed. Please use :httponly instead.", caller) - value[:httponly] ||= value.delete(:http_only) - end - - super(key, value) - end - # Returns the response cookies, converted to a Hash of (name => value) pairs # # assert_equal 'AuthorOfNewPage', r.cookies['author'] @@ -201,7 +214,7 @@ module ActionDispatch # :nodoc: if etag? || last_modified? || !cache_control.empty? set_conditional_cache_control! elsif nonempty_ok_response? - self.etag = body + self.etag = @body if request && request.etag_matches?(etag) self.status = 304 @@ -214,30 +227,37 @@ module ActionDispatch # :nodoc: end end + EMPTY_RESPONSE = [" "] + def nonempty_ok_response? ok = !@status || @status == 200 - ok && string_body? + ok && string_body? && @body != EMPTY_RESPONSE end def string_body? !body_parts.respond_to?(:call) && body_parts.any? && body_parts.all? { |part| part.is_a?(String) } end + DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate" + def set_conditional_cache_control! - if cache_control.empty? - cache_control.merge!(:public => false, :max_age => 0, :must_revalidate => true) - end + control = cache_control - public_cache, max_age, must_revalidate, extras = - cache_control.values_at(:public, :max_age, :must_revalidate, :extras) + if control.empty? + headers["Cache-Control"] = DEFAULT_CACHE_CONTROL + else + extras = control[:extras] + max_age = control[:max_age] + + options = [] + options << "max-age=#{max_age}" if max_age + options << (control[:public] ? "public" : "private") + options << "must-revalidate" if control[:must_revalidate] + options.concat(extras) if extras - options = [] - options << "max-age=#{max_age}" if max_age - options << (public_cache ? "public" : "private") - options << "must-revalidate" if must_revalidate - options.concat(extras) if extras + headers["Cache-Control"] = options.join(", ") + end - headers["Cache-Control"] = options.join(", ") end end 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 74ce85a1c2..1602a609eb 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -46,11 +46,12 @@ class Class end # end " unless options[:instance_writer] == false } # # instance writer above is generated unless options[:instance_writer] == false EOS + self.send("#{sym}=", yield) if block_given? end end - def cattr_accessor(*syms) + def cattr_accessor(*syms, &blk) cattr_reader(*syms) - cattr_writer(*syms) + cattr_writer(*syms, &blk) end end -- cgit v1.2.3 From 4bf516e072f5279bdb462c6592e17b195fd9cf05 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 10 Aug 2009 15:49:33 -0700 Subject: More perf work: * Move #set_cookie and #delete_cookie inline to optimize. These optimizations should almost certainly be sent back upstream to Rack. The optimization involves using an ivar for cookies instead of indexing into the headers each time. * Was able to use a bare Hash for headers now that cookies have their own joining semantics (some code assumed that the raw cookies were an Array). * Cache blankness of body on body= * Improve expand_cache_key for Arrays of a single element (common in our case) * Use a simple layout condition check unless conditions are used * Cache visible actions * Lazily load the UrlRewriter * Make etag an ivar that is set on prepare! --- actionpack/lib/abstract_controller/layouts.rb | 49 ++++++++----- .../lib/action_controller/metal/compatibility.rb | 7 +- .../lib/action_controller/metal/hide_actions.rb | 14 +++- actionpack/lib/action_controller/metal/url_for.rb | 10 +-- .../lib/action_controller/testing/process.rb | 2 +- .../lib/action_controller/testing/test_case.rb | 1 - actionpack/lib/action_dispatch/http/request.rb | 37 +++------- actionpack/lib/action_dispatch/http/response.rb | 84 ++++++++++++++-------- actionpack/lib/action_view/test_case.rb | 1 - actionpack/test/controller/caching_test.rb | 1 - activesupport/lib/active_support/cache.rb | 22 ++++-- 11 files changed, 128 insertions(+), 100 deletions(-) diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index d7317b415c..ac2154dffc 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -6,17 +6,21 @@ module AbstractController included do extlib_inheritable_accessor(:_layout_conditions) { Hash.new } + extlib_inheritable_accessor(:_action_has_layout) { Hash.new } _write_layout_method end module ClassMethods def inherited(klass) super - klass._write_layout_method + klass.class_eval do + _write_layout_method + @found_layouts = {} + end end def cache_layout(details) - layout = @found_layouts ||= {} + layout = @found_layouts values = details.values_at(:formats, :locale) # Cache nil @@ -27,6 +31,28 @@ module AbstractController end end + # This module is mixed in if layout conditions are provided. This means + # that if no layout conditions are used, this method is not used + module LayoutConditions + # Determines whether the current action has a layout by checking the + # action name against the :only and :except conditions set on the + # layout. + # + # ==== Returns + # Boolean:: True if the action has a layout, false otherwise. + def _action_has_layout? + conditions = _layout_conditions + + if only = conditions[:only] + only.include?(action_name) + elsif except = conditions[:except] + !except.include?(action_name) + else + true + end + end + end + # Specify the layout to use for this class. # # If the specified layout is a: @@ -43,6 +69,8 @@ module AbstractController # :only<#to_s, Array[#to_s]>:: A list of actions to apply this layout to. # :except<#to_s, Array[#to_s]>:: Apply this layout to all actions but this one def layout(layout, conditions = {}) + include LayoutConditions unless conditions.empty? + conditions.each {|k, v| conditions[k] = Array(v).map {|a| a.to_s} } self._layout_conditions = conditions @@ -150,7 +178,7 @@ module AbstractController view_paths.find(name, details, prefix) end - # Returns the default layout for this controller and a given set of details. + # Returns the default layout for this controller and a given set of details. # Optionally raises an exception if the layout could not be found. # # ==== Parameters @@ -176,21 +204,8 @@ module AbstractController end end - # Determines whether the current action has a layout by checking the - # action name against the :only and :except conditions set on the - # layout. - # - # ==== Returns - # Boolean:: True if the action has a layout, false otherwise. def _action_has_layout? - conditions = _layout_conditions - if only = conditions[:only] - only.include?(action_name) - elsif except = conditions[:except] - !except.include?(action_name) - else - true - end + true end end end diff --git a/actionpack/lib/action_controller/metal/compatibility.rb b/actionpack/lib/action_controller/metal/compatibility.rb index a008b53d45..5b0165f0e7 100644 --- a/actionpack/lib/action_controller/metal/compatibility.rb +++ b/actionpack/lib/action_controller/metal/compatibility.rb @@ -102,11 +102,10 @@ module ActionController options[:template].sub!(/^\//, '') end - options[:text] = nil if options[:nothing] == true + options[:text] = nil if options.delete(:nothing) == true + options[:text] = " " if options.key?(:text) && options[:text].nil? - body = super - body = [' '] if body.blank? - body + super || " " end def _handle_method_missing diff --git a/actionpack/lib/action_controller/metal/hide_actions.rb b/actionpack/lib/action_controller/metal/hide_actions.rb index af68c772b1..cdacdc40a6 100644 --- a/actionpack/lib/action_controller/metal/hide_actions.rb +++ b/actionpack/lib/action_controller/metal/hide_actions.rb @@ -13,7 +13,9 @@ module ActionController # Overrides AbstractController::Base#action_method? to return false if the # action name is in the list of hidden actions. def action_method?(action_name) - !hidden_actions.include?(action_name) && super + self.class.visible_action?(action_name) do + !hidden_actions.include?(action_name) && super + end end module ClassMethods @@ -25,6 +27,16 @@ module ActionController hidden_actions.merge(args.map! {|a| a.to_s }) end + def inherited(klass) + klass.instance_variable_set("@visible_actions", {}) + super + end + + def visible_action?(action_name) + return @visible_actions[action_name] if @visible_actions.key?(action_name) + @visible_actions[action_name] = yield + end + # Overrides AbstractController::Base#action_methods to remove any methods # that are listed as hidden methods. def action_methods diff --git a/actionpack/lib/action_controller/metal/url_for.rb b/actionpack/lib/action_controller/metal/url_for.rb index 7119c14cd3..14c6523045 100644 --- a/actionpack/lib/action_controller/metal/url_for.rb +++ b/actionpack/lib/action_controller/metal/url_for.rb @@ -4,15 +4,6 @@ module ActionController include RackConvenience - def process_action(*) - initialize_current_url - super - end - - def initialize_current_url - @url = UrlRewriter.new(request, params.clone) - end - # Overwrite to implement a number of default options that all url_for-based methods will use. The default options should come in # the form of a hash, just like the one you would use for url_for directly. Example: # @@ -40,6 +31,7 @@ module ActionController when String options when Hash + @url ||= UrlRewriter.new(request, params) @url.rewrite(rewrite_options(options)) else polymorphic_url(options) diff --git a/actionpack/lib/action_controller/testing/process.rb b/actionpack/lib/action_controller/testing/process.rb index 147a7e7631..09b1a59254 100644 --- a/actionpack/lib/action_controller/testing/process.rb +++ b/actionpack/lib/action_controller/testing/process.rb @@ -52,7 +52,7 @@ module ActionController #:nodoc: class TestResponse < ActionDispatch::TestResponse def recycle! @status = 200 - @header = ActionDispatch::Response::SimpleHeaderHash.new + @header = {} @writer = lambda { |x| @body << x } @block = nil @length = 0 diff --git a/actionpack/lib/action_controller/testing/test_case.rb b/actionpack/lib/action_controller/testing/test_case.rb index a11755b517..b66a4c15ff 100644 --- a/actionpack/lib/action_controller/testing/test_case.rb +++ b/actionpack/lib/action_controller/testing/test_case.rb @@ -179,7 +179,6 @@ module ActionController if @controller @controller.request = @request @controller.params = {} - @controller.send(:initialize_current_url) end end diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index 958a541436..b23306af62 100755 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -173,21 +173,16 @@ module ActionDispatch end end - # Expand raw_formats by converting Mime::ALL to the Mime::SET. - # def formats - return raw_formats - # if ActionController::Base.use_accept_header - # raw_formats.tap do |ret| - # if ret == ONLY_ALL - # ret.replace Mime::SET - # elsif all = ret.index(Mime::ALL) - # ret.delete_at(all) && ret.insert(all, *Mime::SET) - # end - # end - # else - # raw_formats + Mime::SET - # end + if ActionController::Base.use_accept_header + if param = parameters[:format] + Array.wrap(Mime[param]) + else + accepts.dup + end + else + [format] + end end # Sets the \format by string extension, which can be used to force custom formats @@ -488,7 +483,7 @@ EOM # matches the order array. # def negotiate_mime(order) - raw_formats.each do |priority| + formats.each do |priority| if priority == Mime::ALL return order.first elsif order.include?(priority) @@ -501,18 +496,6 @@ EOM private - def raw_formats - if ActionController::Base.use_accept_header - if param = parameters[:format] - Array.wrap(Mime[param]) - else - accepts.dup - end - else - [format] - end - end - def named_host?(host) !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)) end diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb index 1d3cf63984..055f29a972 100644 --- a/actionpack/lib/action_dispatch/http/response.rb +++ b/actionpack/lib/action_dispatch/http/response.rb @@ -32,18 +32,7 @@ module ActionDispatch # :nodoc: # end # end class Response < Rack::Response - class SimpleHeaderHash < Hash - def to_hash - result = {} - each do |k,v| - v = v.join("\n") if v.is_a?(Array) - result[k] = v - end - result - end - end - - attr_accessor :request + attr_accessor :request, :blank attr_reader :cache_control attr_writer :header, :sending_file @@ -51,19 +40,23 @@ module ActionDispatch # :nodoc: def initialize @status = 200 - @header = SimpleHeaderHash.new + @header = {} @cache_control = {} @writer = lambda { |x| @body << x } @block = nil @length = 0 - @body = [] + @body, @cookie = [], [] @sending_file = false yield self if block_given? end + def cache_control + @cache_control ||= {} + end + def write(str) s = str.to_s @writer.call s @@ -95,7 +88,10 @@ module ActionDispatch # :nodoc: str end + EMPTY = " " + def body=(body) + @blank = true if body == EMPTY @body = body.respond_to?(:to_str) ? [body] : body end @@ -137,19 +133,16 @@ module ActionDispatch # :nodoc: end def etag - headers['ETag'] + @etag end def etag? - headers.include?('ETag') + @etag end def etag=(etag) - if etag.blank? - headers.delete('ETag') - else - headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}") - end + key = ActiveSupport::Cache.expand_cache_key(etag) + @etag = %("#{Digest::MD5.hexdigest(key)}") end CONTENT_TYPE = "Content-Type" @@ -157,7 +150,7 @@ module ActionDispatch # :nodoc: cattr_accessor(:default_charset) { "utf-8" } def assign_default_content_type_and_charset! - return if !headers[CONTENT_TYPE].blank? + return if headers[CONTENT_TYPE].present? @content_type ||= Mime::HTML @charset ||= self.class.default_charset @@ -171,7 +164,8 @@ module ActionDispatch # :nodoc: def prepare! assign_default_content_type_and_charset! handle_conditional_get! - self["Set-Cookie"] ||= "" + self["Set-Cookie"] = @cookie.join("\n") + self["ETag"] = @etag if @etag end def each(&callback) @@ -197,7 +191,7 @@ module ActionDispatch # :nodoc: # assert_equal 'AuthorOfNewPage', r.cookies['author'] def cookies cookies = {} - if header = headers['Set-Cookie'] + if header = @cookie header = header.split("\n") if header.respond_to?(:to_str) header.each do |cookie| if pair = cookie.split(';').first @@ -209,9 +203,40 @@ module ActionDispatch # :nodoc: cookies end + def set_cookie(key, value) + case value + when Hash + domain = "; domain=" + value[:domain] if value[:domain] + path = "; path=" + value[:path] if value[:path] + # According to RFC 2109, we need dashes here. + # N.B.: cgi.rb uses spaces... + expires = "; expires=" + value[:expires].clone.gmtime. + strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires] + secure = "; secure" if value[:secure] + httponly = "; HttpOnly" if value[:httponly] + value = value[:value] + end + value = [value] unless Array === value + cookie = Rack::Utils.escape(key) + "=" + + value.map { |v| Rack::Utils.escape v }.join("&") + + "#{domain}#{path}#{expires}#{secure}#{httponly}" + + @cookie << cookie + end + + def delete_cookie(key, value={}) + @cookie.reject! { |cookie| + cookie =~ /\A#{Rack::Utils.escape(key)}=/ + } + + set_cookie(key, + {:value => '', :path => nil, :domain => nil, + :expires => Time.at(0) }.merge(value)) + end + private def handle_conditional_get! - if etag? || last_modified? || !cache_control.empty? + if etag? || last_modified? || !@cache_control.empty? set_conditional_cache_control! elsif nonempty_ok_response? self.etag = @body @@ -227,21 +252,18 @@ module ActionDispatch # :nodoc: end end - EMPTY_RESPONSE = [" "] - def nonempty_ok_response? - ok = !@status || @status == 200 - ok && string_body? && @body != EMPTY_RESPONSE + @status == 200 && string_body? end def string_body? - !body_parts.respond_to?(:call) && body_parts.any? && body_parts.all? { |part| part.is_a?(String) } + !@blank && @body.respond_to?(:all?) && @body.all? { |part| part.is_a?(String) } end DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate" def set_conditional_cache_control! - control = cache_control + control = @cache_control if control.empty? headers["Cache-Control"] = DEFAULT_CACHE_CONTROL diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index b317b6dc1a..c2ccd1d3a5 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -72,7 +72,6 @@ module ActionView @response = ActionController::TestResponse.new @params = {} - send(:initialize_current_url) end end diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 346fa09414..f1c93e6b1e 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -536,7 +536,6 @@ class FragmentCachingTest < ActionController::TestCase @controller.params = @params @controller.request = @request @controller.response = @response - @controller.send(:initialize_current_url) @controller.send(:initialize_template_class, @response) @controller.send(:assign_shortcuts, @request, @response) end diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 448db538ab..e28df8efa5 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -62,19 +62,27 @@ module ActiveSupport end end + RAILS_CACHE_ID = ENV["RAILS_CACHE_ID"] + RAILS_APP_VERION = ENV["RAILS_APP_VERION"] + EXPANDED_CACHE = RAILS_CACHE_ID || RAILS_APP_VERION + def self.expand_cache_key(key, namespace = nil) expanded_cache_key = namespace ? "#{namespace}/" : "" - if ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"] - expanded_cache_key << "#{ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]}/" + if EXPANDED_CACHE + expanded_cache_key << "#{RAILS_CACHE_ID || RAILS_APP_VERION}/" end - expanded_cache_key << case - when key.respond_to?(:cache_key) + expanded_cache_key << + if key.respond_to?(:cache_key) key.cache_key - when key.is_a?(Array) - key.collect { |element| expand_cache_key(element) }.to_param - when key + elsif key.is_a?(Array) + if key.size > 1 + key.collect { |element| expand_cache_key(element) }.to_param + else + key.first.to_param + end + elsif key key.to_param end.to_s -- cgit v1.2.3 From ccd1c5e521c020118b5bcfeb4cd5a651997b6806 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 10 Aug 2009 15:49:53 -0700 Subject: Allow superclass_delegating_accessor to take a block for initial set. --- .../lib/active_support/core_ext/class/delegating_attributes.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb index fd029544c3..6c67df7f50 100644 --- a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb @@ -26,13 +26,14 @@ class Class end end - def superclass_delegating_writer(*names) + def superclass_delegating_writer(*names, &block) names.each do |name| class_eval(<<-EOS, __FILE__, __LINE__ + 1) def self.#{name}=(value) # def self.property=(value) @#{name} = value # @property = value end # end EOS + self.send("#{name}=", yield) if block_given? end end @@ -42,8 +43,8 @@ class Class # delegate to their superclass unless they have been given a # specific value. This stops the strange situation where values # set after class definition don't get applied to subclasses. - def superclass_delegating_accessor(*names) + def superclass_delegating_accessor(*names, &block) superclass_delegating_reader(*names) - superclass_delegating_writer(*names) + superclass_delegating_writer(*names, &block) end end -- cgit v1.2.3 From 27e880729eeb058583b79ccf84a7577c434addfd Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 11 Aug 2009 16:49:27 -0700 Subject: Made benchmarks submodule so it's easier to keep in sync --- .gitmodules | 3 + actionpack/examples | 1 + actionpack/examples/minimal.rb | 132 ----------------------- actionpack/examples/very_simple.rb | 50 --------- actionpack/examples/views/_collection.erb | 1 - actionpack/examples/views/_hello.erb | 1 - actionpack/examples/views/_hundred_partials.erb | 12 --- actionpack/examples/views/_many_partials.erb | 10 -- actionpack/examples/views/_partial.erb | 10 -- actionpack/examples/views/layouts/alt.html.erb | 1 - actionpack/examples/views/layouts/kaigi.html.erb | 1 - actionpack/examples/views/template.html.erb | 1 - 12 files changed, 4 insertions(+), 219 deletions(-) create mode 100644 .gitmodules create mode 160000 actionpack/examples delete mode 100644 actionpack/examples/minimal.rb delete mode 100644 actionpack/examples/very_simple.rb delete mode 100644 actionpack/examples/views/_collection.erb delete mode 100644 actionpack/examples/views/_hello.erb delete mode 100644 actionpack/examples/views/_hundred_partials.erb delete mode 100644 actionpack/examples/views/_many_partials.erb delete mode 100644 actionpack/examples/views/_partial.erb delete mode 100644 actionpack/examples/views/layouts/alt.html.erb delete mode 100644 actionpack/examples/views/layouts/kaigi.html.erb delete mode 100644 actionpack/examples/views/template.html.erb diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000000..d6730e194a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "actionpack/examples"] + path = actionpack/examples + url = git://github.com/wycats/rails-simple-benches.git diff --git a/actionpack/examples b/actionpack/examples new file mode 160000 index 0000000000..4e1327f06d --- /dev/null +++ b/actionpack/examples @@ -0,0 +1 @@ +Subproject commit 4e1327f06da6df1a1981d69c04e8d6463b38a4c1 diff --git a/actionpack/examples/minimal.rb b/actionpack/examples/minimal.rb deleted file mode 100644 index 62b71de2cb..0000000000 --- a/actionpack/examples/minimal.rb +++ /dev/null @@ -1,132 +0,0 @@ -# Pass NEW=1 to run with the new Base -ENV['RAILS_ENV'] ||= 'production' -ENV['NO_RELOAD'] ||= '1' - -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../activesupport/lib" -require 'action_controller' -require 'action_controller/new_base' if ENV['NEW'] -require 'action_view' -require 'benchmark' - -class Runner - def initialize(app, output) - @app, @output = app, output - end - - def puts(*) - super if @output - end - - def call(env) - env['n'].to_i.times { @app.call(env) } - @app.call(env).tap { |response| report(env, response) } - end - - def report(env, response) - return unless ENV["DEBUG"] - out = env['rack.errors'] - out.puts response[0], response[1].to_yaml, '---' - response[2].each { |part| out.puts part } - out.puts '---' - end - - def self.puts(*) - super if @output - end - - def self.run(app, n, label, output = true) - @output = output - puts label, '=' * label.size if label - env = Rack::MockRequest.env_for("/").merge('n' => n, 'rack.input' => StringIO.new(''), 'rack.errors' => $stdout) - t = Benchmark.realtime { new(app, output).call(env) } - puts "%d ms / %d req = %.1f usec/req" % [10**3 * t, n, 10**6 * t / n] - puts - end -end - - -N = (ENV['N'] || 1000).to_i - -module ActionController::Rails2Compatibility - instance_methods.each do |name| - remove_method name - end -end - -class BasePostController < ActionController::Base - append_view_path "#{File.dirname(__FILE__)}/views" - - def overhead - self.response_body = '' - end - - def index - render :text => '' - end - - def partial - render :partial => "/partial" - end - - def many_partials - render :partial => "/many_partials" - end - - def hundred_partials - render :partial => "/hundred_partials" - end - - def partial_collection - render :partial => "/collection", :collection => [1,2,3,4,5,6,7,8,9,10] - end - - def large_collection - render :partial => "/collection", :collection => (1...1000).to_a - end - - def show_template - render :template => "template" - end -end - -OK = [200, {}, []] -MetalPostController = lambda { OK } - -class HttpPostController < ActionController::Metal - def index - self.response_body = '' - end -end - -ActionController::Base.use_accept_header = false - -unless ENV["PROFILE"] - Runner.run(BasePostController.action(:overhead), 1, 'overhead', false) - Runner.run(BasePostController.action(:index), 1, 'index', false) - Runner.run(BasePostController.action(:show_template), 1, 'template', false) - Runner.run(BasePostController.action(:partial), 1, 'partial', false) - Runner.run(BasePostController.action(:many_partials), 1, 'many_partials', false) - Runner.run(BasePostController.action(:partial_collection), 1, 'collection', false) - Runner.run(BasePostController.action(:hundred_partials), 1, 'hundred_partials', false) - Runner.run(BasePostController.action(:large_collection), 1, 'large_collection', false) - - (ENV["M"] || 1).to_i.times do - Runner.run(BasePostController.action(:overhead), N, 'overhead') - Runner.run(BasePostController.action(:index), N, 'index') - Runner.run(BasePostController.action(:show_template), N, 'template') - Runner.run(BasePostController.action(:partial), N, 'partial') - Runner.run(BasePostController.action(:many_partials), N, 'many_partials') - Runner.run(BasePostController.action(:partial_collection), N, 'collection') - Runner.run(BasePostController.action(:hundred_partials), N, 'hundred_partials') - Runner.run(BasePostController.action(:large_collection), N, 'large_collection') - end -else - Runner.run(BasePostController.action(ENV["PROFILE"].to_sym), 1, ENV["PROFILE"]) - require "ruby-prof" - RubyProf.start - Runner.run(BasePostController.action(ENV["PROFILE"].to_sym), N, ENV["PROFILE"]) - result = RubyProf.stop - printer = RubyProf::CallStackPrinter.new(result) - printer.print(File.open("output.html", "w")) -end \ No newline at end of file diff --git a/actionpack/examples/very_simple.rb b/actionpack/examples/very_simple.rb deleted file mode 100644 index 6714185172..0000000000 --- a/actionpack/examples/very_simple.rb +++ /dev/null @@ -1,50 +0,0 @@ -$:.push "rails/activesupport/lib" -$:.push "rails/actionpack/lib" - -require "action_controller" - -class Kaigi < ActionController::Metal - include AbstractController::Callbacks - include ActionController::RackConvenience - include ActionController::RenderingController - include ActionController::Layouts - include ActionView::Context - - before_filter :set_name - append_view_path "views" - - def view_context - self - end - - def controller - self - end - - DEFAULT_LAYOUT = Object.new.tap {|l| def l.render(*) yield end } - - def render_template(template, layout = DEFAULT_LAYOUT, options = {}, partial = false) - ret = template.render(self, {}) - layout.render(self, {}) { ret } - end - - def index - render :template => "template" - end - - def alt - render :template => "template", :layout => "alt" - end - - private - def set_name - @name = params[:name] - end -end - -app = Rack::Builder.new do - map("/kaigi") { run Kaigi.action(:index) } - map("/kaigi/alt") { run Kaigi.action(:alt) } -end.to_app - -Rack::Handler::Mongrel.run app, :Port => 3000 diff --git a/actionpack/examples/views/_collection.erb b/actionpack/examples/views/_collection.erb deleted file mode 100644 index bcfe958e2c..0000000000 --- a/actionpack/examples/views/_collection.erb +++ /dev/null @@ -1 +0,0 @@ -<%= collection %> \ No newline at end of file diff --git a/actionpack/examples/views/_hello.erb b/actionpack/examples/views/_hello.erb deleted file mode 100644 index 5ab2f8a432..0000000000 --- a/actionpack/examples/views/_hello.erb +++ /dev/null @@ -1 +0,0 @@ -Hello \ No newline at end of file diff --git a/actionpack/examples/views/_hundred_partials.erb b/actionpack/examples/views/_hundred_partials.erb deleted file mode 100644 index 15e99c1b68..0000000000 --- a/actionpack/examples/views/_hundred_partials.erb +++ /dev/null @@ -1,12 +0,0 @@ -<% 10.times do %> - <%= render :partial => '/hello' %> - <%= render :partial => '/hello' %> - <%= render :partial => '/hello' %> - <%= render :partial => '/hello' %> - <%= render :partial => '/hello' %> - <%= render :partial => '/hello' %> - <%= render :partial => '/hello' %> - <%= render :partial => '/hello' %> - <%= render :partial => '/hello' %> - <%= render :partial => '/hello' %> -<% end %> \ No newline at end of file diff --git a/actionpack/examples/views/_many_partials.erb b/actionpack/examples/views/_many_partials.erb deleted file mode 100644 index 7e379d46f5..0000000000 --- a/actionpack/examples/views/_many_partials.erb +++ /dev/null @@ -1,10 +0,0 @@ -<%= render :partial => '/hello' %> -<%= render :partial => '/hello' %> -<%= render :partial => '/hello' %> -<%= render :partial => '/hello' %> -<%= render :partial => '/hello' %> -<%= render :partial => '/hello' %> -<%= render :partial => '/hello' %> -<%= render :partial => '/hello' %> -<%= render :partial => '/hello' %> -<%= render :partial => '/hello' %> \ No newline at end of file diff --git a/actionpack/examples/views/_partial.erb b/actionpack/examples/views/_partial.erb deleted file mode 100644 index 3ca8e80b52..0000000000 --- a/actionpack/examples/views/_partial.erb +++ /dev/null @@ -1,10 +0,0 @@ -<%= "Hello" %> -<%= "Hello" %> -<%= "Hello" %> -<%= "Hello" %> -<%= "Hello" %> -<%= "Hello" %> -<%= "Hello" %> -<%= "Hello" %> -<%= "Hello" %> -<%= "Hello" %> diff --git a/actionpack/examples/views/layouts/alt.html.erb b/actionpack/examples/views/layouts/alt.html.erb deleted file mode 100644 index c4816337a6..0000000000 --- a/actionpack/examples/views/layouts/alt.html.erb +++ /dev/null @@ -1 +0,0 @@ -+ <%= yield %> + \ No newline at end of file diff --git a/actionpack/examples/views/layouts/kaigi.html.erb b/actionpack/examples/views/layouts/kaigi.html.erb deleted file mode 100644 index 274607a96a..0000000000 --- a/actionpack/examples/views/layouts/kaigi.html.erb +++ /dev/null @@ -1 +0,0 @@ -Hello <%= yield %> Goodbye \ No newline at end of file diff --git a/actionpack/examples/views/template.html.erb b/actionpack/examples/views/template.html.erb deleted file mode 100644 index 5ab2f8a432..0000000000 --- a/actionpack/examples/views/template.html.erb +++ /dev/null @@ -1 +0,0 @@ -Hello \ No newline at end of file -- cgit v1.2.3 From ba67e256b8bb3749090fcf7ccaff497ea006b1d1 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 11 Aug 2009 23:44:44 -0700 Subject: Remove submodule --- .gitmodules | 3 --- actionpack/examples | 1 - 2 files changed, 4 deletions(-) delete mode 160000 actionpack/examples diff --git a/.gitmodules b/.gitmodules index d6730e194a..e69de29bb2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "actionpack/examples"] - path = actionpack/examples - url = git://github.com/wycats/rails-simple-benches.git diff --git a/actionpack/examples b/actionpack/examples deleted file mode 160000 index 4e1327f06d..0000000000 --- a/actionpack/examples +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4e1327f06da6df1a1981d69c04e8d6463b38a4c1 -- cgit v1.2.3 From f413a703ba5bcf4b369932ae805615c4a34d34cb Mon Sep 17 00:00:00 2001 From: Chad Woolley Date: Tue, 11 Aug 2009 22:53:55 -0700 Subject: make mysql and postgresql rebuild databases on every CI build, to prevent breakages such as collation and character set changing Signed-off-by: Yehuda Katz --- ci/ci_build.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/ci_build.rb b/ci/ci_build.rb index 0b9bd3d278..7d81fa843a 100755 --- a/ci/ci_build.rb +++ b/ci/ci_build.rb @@ -28,14 +28,14 @@ cd "#{root_dir}/activerecord" do puts puts "[CruiseControl] Building ActiveRecord with MySQL" puts - build_results[:activerecord_mysql] = system 'rake test_mysql' + build_results[:activerecord_mysql] = system 'rake mysql:rebuild_databases && rake test_mysql' end cd "#{root_dir}/activerecord" do puts puts "[CruiseControl] Building ActiveRecord with PostgreSQL" puts - build_results[:activerecord_postgresql8] = system 'rake test_postgresql' + build_results[:activerecord_postgresql8] = system 'rake postgresql:rebuild_databases && rake test_postgresql' end cd "#{root_dir}/activerecord" do -- cgit v1.2.3