diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-05-13 01:10:37 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-05-13 12:00:15 -0700 |
commit | e8550ee0329586b32de425e905c7af7e65bc78a8 (patch) | |
tree | 0cdfe0f9f0d3fb010280ff0453fc553ab6ff2d92 /activerecord/lib | |
parent | fa5da8ad54d68ea0484825845eb6f6a8e8bca361 (diff) | |
download | rails-e8550ee0329586b32de425e905c7af7e65bc78a8.tar.gz rails-e8550ee0329586b32de425e905c7af7e65bc78a8.tar.bz2 rails-e8550ee0329586b32de425e905c7af7e65bc78a8.zip |
Cherry-pick core extensions
Diffstat (limited to 'activerecord/lib')
20 files changed, 62 insertions, 30 deletions
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index c9e9a84ce7..2d98239052 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -24,7 +24,6 @@ activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib" $:.unshift(activesupport_path) if File.directory?(activesupport_path) require 'active_support' -require 'active_support/core/all' module ActiveRecord # TODO: Review explicit loads to see if they will automatically be handled by the initilizer. diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index e2dd36158f..c5e4df4950 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/module/delegation' + module ActiveRecord class InverseOfAssociationNotFoundError < ActiveRecordError #:nodoc: def initialize(reflection) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 8d68d77eac..55d9a4d15d 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/enumerable' + module ActiveRecord module AttributeMethods #:nodoc: extend ActiveSupport::DependencyModule diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 97c36a675d..b9ba727a3f 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1,6 +1,15 @@ require 'yaml' require 'set' require 'active_support/dependencies' +require 'active_support/core_ext/class/attribute_accessors' +require 'active_support/core_ext/class/delegating_attributes' +require 'active_support/core_ext/class/inheritable_attributes' +require 'active_support/core_ext/array/extract_options' +require 'active_support/core_ext/hash/deep_merge' +require 'active_support/core_ext/hash/indifferent_access' +require 'active_support/core_ext/hash/slice' +require 'active_support/core_ext/string/behavior' +require 'active_support/core/time' module ActiveRecord #:nodoc: # Generic Active Record exception class. @@ -1888,7 +1897,7 @@ module ActiveRecord #:nodoc: else find(:#{finder}, options.merge(finder_options)) end - #{'result || raise(RecordNotFound, "Couldn\'t find #{name} with #{attributes.to_a.collect {|pair| "#{pair.first} = #{pair.second}"}.join(\', \')}")' if bang} + #{'result || raise(RecordNotFound, "Couldn\'t find #{name} with #{attributes.to_a.collect { |pair| pair.join(\' = \') }.join(\', \')}")' if bang} end }, __FILE__, __LINE__ send(method_id, *arguments) @@ -2610,11 +2619,11 @@ module ActiveRecord #:nodoc: # Note: The new instance will share a link to the same attributes as the original class. So any change to the attributes in either # instance will affect the other. def becomes(klass) - returning klass.new do |became| - became.instance_variable_set("@attributes", @attributes) - became.instance_variable_set("@attributes_cache", @attributes_cache) - became.instance_variable_set("@new_record", new_record?) - end + became = klass.new + became.instance_variable_set("@attributes", @attributes) + became.instance_variable_set("@attributes_cache", @attributes_cache) + became.instance_variable_set("@new_record", new_record?) + became end # Updates a single attribute and saves the record without going through the normal validation procedure. diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index e8e736bf38..500dafdc2e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -1,5 +1,6 @@ require 'monitor' require 'set' +require 'active_support/core_ext/module/synchronization' module ActiveRecord # Raised when a connection could not be obtained within the connection diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index 3a7bf35248..720fba29e9 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/big_decimal/conversions' + module ActiveRecord module ConnectionAdapters # :nodoc: module Quoting diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index a8cd9f033b..91b111ab55 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -12,6 +12,8 @@ require 'active_record/connection_adapters/abstract/connection_pool' require 'active_record/connection_adapters/abstract/connection_specification' require 'active_record/connection_adapters/abstract/query_cache' +require 'active_support/core_ext/benchmark' + module ActiveRecord module ConnectionAdapters # :nodoc: # ActiveRecord supports multiple database systems. AbstractAdapter and diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 9300df28ee..d5536e4d67 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -1,4 +1,5 @@ require 'active_record/connection_adapters/abstract_adapter' +require 'active_support/core_ext/kernel/requires' require 'set' module MysqlCompat #:nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 4961793866..002696d2c4 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -1,4 +1,5 @@ require 'active_record/connection_adapters/abstract_adapter' +require 'active_support/core_ext/kernel/requires' begin require_library_or_gem 'pg' diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 75420f69aa..5eef692d05 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -25,9 +25,9 @@ module ActiveRecord module ConnectionAdapters #:nodoc: class SQLite3Adapter < SQLiteAdapter # :nodoc: def table_structure(table_name) - returning structure = @connection.table_info(quote_table_name(table_name)) do - raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if structure.empty? - end + structure = @connection.table_info(quote_table_name(table_name)) + raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if structure.empty? + structure end end end diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 05334a830a..c9d0c9574f 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -1,4 +1,5 @@ require 'active_record/connection_adapters/abstract_adapter' +require 'active_support/core_ext/kernel/requires' module ActiveRecord class Base diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb index fac6ca40d3..ac84f6b209 100644 --- a/activerecord/lib/active_record/dirty.rb +++ b/activerecord/lib/active_record/dirty.rb @@ -168,7 +168,9 @@ module ActiveRecord module ClassMethods def self.extended(base) - base.metaclass.alias_method_chain(:alias_attribute, :dirty) + class << base + alias_method_chain :alias_attribute, :dirty + end end def alias_attribute_with_dirty(new_name, old_name) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 91b4b4e182..e30fcf9a4f 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -3,6 +3,7 @@ require 'yaml' require 'csv' require 'active_support/dependencies' require 'active_support/test_case' +require 'active_support/core_ext/logger' if RUBY_VERSION < '1.9' module YAML #:nodoc: diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 657acd6dc0..a7be3539d5 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -511,11 +511,11 @@ module ActiveRecord raise DuplicateMigrationNameError.new(name.camelize) end - klasses << returning(MigrationProxy.new) do |migration| - migration.name = name.camelize - migration.version = version - migration.filename = file - end + migration = MigrationProxy.new + migration.name = name.camelize + migration.version = version + migration.filename = file + klasses << migration end migrations = migrations.sort_by(&:version) diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 32bb36c07c..07f98dc743 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -1,3 +1,6 @@ +require 'active_support/core_ext/array' +require 'active_support/core_ext/hash/except' + module ActiveRecord module NamedScope extend ActiveSupport::DependencyModule diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 1ea2f53fd8..c532d3dfa3 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -1,3 +1,6 @@ +require 'active_support/core_ext/hash/except' +require 'active_support/core_ext/object/try' + module ActiveRecord module NestedAttributes #:nodoc: extend ActiveSupport::DependencyModule diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 557a554966..de530a3456 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -1,5 +1,5 @@ require 'stringio' -require 'bigdecimal' +require 'active_support/core_ext/big_decimal' module ActiveRecord # This class is used to dump the database schema for some connection to some @@ -176,4 +176,4 @@ HEADER end end end -end
\ No newline at end of file +end diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb index 78f66c3a73..7959f2b510 100644 --- a/activerecord/lib/active_record/serialization.rb +++ b/activerecord/lib/active_record/serialization.rb @@ -1,5 +1,3 @@ -require 'active_support/json' - module ActiveRecord #:nodoc: module Serialization class Serializer #:nodoc: @@ -73,16 +71,19 @@ module ActiveRecord #:nodoc: end def serializable_record - returning(serializable_record = {}) do - serializable_names.each { |name| serializable_record[name] = @record.send(name) } - add_includes do |association, records, opts| + record = {} + serializable_names.each { |name| record[name] = @record.send(name) } + + add_includes do |association, records, opts| + record[association] = if records.is_a?(Enumerable) - serializable_record[association] = records.collect { |r| self.class.new(r, opts).serializable_record } + records.collect { |r| self.class.new(r, opts).serializable_record } else - serializable_record[association] = self.class.new(records, opts).serializable_record + self.class.new(records, opts).serializable_record end - end end + + record end def serialize diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index fa75874603..4eaf9531e2 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/hash/conversions' + module ActiveRecord #:nodoc: module Serialization # Builds an XML document to represent the model. Some configuration is @@ -165,8 +167,9 @@ module ActiveRecord #:nodoc: class XmlSerializer < ActiveRecord::Serialization::Serializer #:nodoc: def builder @builder ||= begin + require 'builder' unless defined? ::Builder options[:indent] ||= 2 - builder = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) + builder = options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent]) unless options[:skip_instruct] builder.instruct! diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 9907a3c9b7..b6e848fa79 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -1,5 +1,3 @@ -require 'builder' - module ActiveRecord # Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the # +record+ method to retrieve the record which did not validate. @@ -247,9 +245,10 @@ module ActiveRecord # # <error>Address can't be blank</error> # # </errors> def to_xml(options={}) + require 'builder' unless defined? ::Builder options[:root] ||= "errors" options[:indent] ||= 2 - options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) + options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent]) options[:builder].instruct! unless options.delete(:skip_instruct) options[:builder].errors do |e| |