diff options
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--[-rwxr-xr-x] | activerecord/lib/active_record/associations.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 6 | ||||
-rw-r--r--[-rwxr-xr-x] | activerecord/lib/active_record/base.rb | 36 | ||||
-rw-r--r-- | activerecord/lib/active_record/callbacks.rb | 27 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb | 4 | ||||
-rw-r--r--[-rwxr-xr-x] | activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 0 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/observer.rb | 3 | ||||
-rw-r--r-- | activerecord/lib/active_record/railtie.rb | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 92 | ||||
-rw-r--r-- | activerecord/lib/active_record/transactions.rb | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/validations.rb | 4 |
12 files changed, 71 insertions, 112 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 185e298f5c..399c1bf58a 100755..100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -2066,7 +2066,7 @@ module ActiveRecord unless klass.descends_from_active_record? sti_column = aliased_table[klass.inheritance_column] sti_condition = sti_column.eq(klass.sti_name) - klass.send(:subclasses).each {|subclass| sti_condition = sti_condition.or(sti_column.eq(subclass.sti_name)) } + klass.descendants.each {|subclass| sti_condition = sti_condition.or(sti_column.eq(subclass.sti_name)) } @join << sti_condition end diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 5b5094bcab..f8d46bcb48 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -390,7 +390,11 @@ module ActiveRecord begin if !loaded? if @target.is_a?(Array) && @target.any? - @target = find_target + @target.find_all {|t| t.new_record? } + @target = find_target.map do |f| + i = @target.index(f) + t = @target.delete_at(i) if i + (t && t.changed?) ? t : f + end + @target else @target = find_target end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 93249fc96c..3f81ca7555 100755..100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2,6 +2,7 @@ require 'yaml' require 'set' require 'active_support/benchmarkable' require 'active_support/dependencies' +require 'active_support/descendants_tracker' require 'active_support/time' require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/class/attribute_accessors' @@ -276,28 +277,6 @@ module ActiveRecord #:nodoc: # on to any new database connections made and which can be retrieved on both a class and instance level by calling +logger+. cattr_accessor :logger, :instance_writer => false - def self.inherited(child) #:nodoc: - @@subclasses[self] ||= [] - @@subclasses[self] << child - super - end - - def self.reset_subclasses #:nodoc: - nonreloadables = [] - subclasses.each do |klass| - unless ActiveSupport::Dependencies.autoloaded? klass - nonreloadables << klass - next - end - klass.instance_variables.each { |var| klass.send(:remove_instance_variable, var) } - klass.instance_methods(false).each { |m| klass.send :undef_method, m } - end - @@subclasses = {} - nonreloadables.each { |klass| (@@subclasses[klass.superclass] ||= []) << klass } - end - - @@subclasses = {} - ## # :singleton-method: # Contains the database configuration - as is typically stored in config/database.yml - @@ -812,7 +791,7 @@ module ActiveRecord #:nodoc: end def reset_column_information_and_inheritable_attributes_for_all_subclasses#:nodoc: - subclasses.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information } + descendants.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information } end def attribute_method?(attribute) @@ -977,7 +956,7 @@ module ActiveRecord #:nodoc: def type_condition sti_column = arel_table[inheritance_column] condition = sti_column.eq(sti_name) - subclasses.each{|subclass| condition = condition.or(sti_column.eq(subclass.sti_name)) } + descendants.each { |subclass| condition = condition.or(sti_column.eq(subclass.sti_name)) } condition end @@ -1167,14 +1146,6 @@ module ActiveRecord #:nodoc: with_scope(method_scoping, :overwrite, &block) end - # Returns a list of all subclasses of this class, meaning all descendants. - def subclasses - @@subclasses[self] ||= [] - @@subclasses[self] + @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses } - end - - public :subclasses - # Sets the default options for the model. The format of the # <tt>options</tt> argument is the same as in find. # @@ -1902,6 +1873,7 @@ module ActiveRecord #:nodoc: extend ActiveModel::Naming extend QueryCache::ClassMethods extend ActiveSupport::Benchmarkable + extend ActiveSupport::DescendantsTracker include ActiveModel::Conversion include Validations diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index 7b7de0b070..637dac450b 100644 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -236,8 +236,7 @@ module ActiveRecord included do extend ActiveModel::Callbacks - - define_callbacks :validation, :terminator => "result == false", :scope => [:kind, :name] + include ActiveModel::Validations::Callbacks define_model_callbacks :initialize, :find, :only => :after define_model_callbacks :save, :create, :update, :destroy @@ -251,29 +250,6 @@ module ActiveRecord send(meth.to_sym, meth.to_sym) end end - - def before_validation(*args, &block) - options = args.last - if options.is_a?(Hash) && options[:on] - options[:if] = Array.wrap(options[:if]) - options[:if] << "@_on_validate == :#{options[:on]}" - end - set_callback(:validation, :before, *args, &block) - end - - def after_validation(*args, &block) - options = args.extract_options! - options[:prepend] = true - options[:if] = Array.wrap(options[:if]) - options[:if] << "!halted && value != false" - options[:if] << "@_on_validate == :#{options[:on]}" if options[:on] - set_callback(:validation, :after, *(args << options), &block) - end - end - - def valid?(*) #:nodoc: - @_on_validate = new_record? ? :create : :update - _run_validation_callbacks { super } end def destroy #:nodoc: @@ -288,6 +264,7 @@ module ActiveRecord end private + def create_or_update #:nodoc: _run_save_callbacks { super } end diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index b9fb452eee..25432e9985 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -304,7 +304,7 @@ module ActiveRecord begin record.rolledback!(rollback) rescue Exception => e - record.logger.error(e) if record.respond_to?(:logger) + record.logger.error(e) if record.respond_to?(:logger) && record.logger end end end @@ -319,7 +319,7 @@ module ActiveRecord begin record.committed! rescue Exception => e - record.logger.error(e) if record.respond_to?(:logger) + record.logger.error(e) if record.respond_to?(:logger) && record.logger end end end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 4ee9fee4a9..4ee9fee4a9 100755..100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index bb8850f134..e84242601b 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -927,7 +927,12 @@ module ActiveRecord # If using Active Record's time zone support configure the connection to return # TIMESTAMP WITH ZONE types in UTC. - execute("SET time zone 'UTC'") if ActiveRecord::Base.default_timezone == :utc + if ActiveRecord::Base.default_timezone == :utc + execute("SET time zone 'UTC'") + else + offset = Time.local(2000).utc_offset / 3600 + execute("SET time zone '#{offset}'") + end end # Returns the current ID of a table's sequence. diff --git a/activerecord/lib/active_record/observer.rb b/activerecord/lib/active_record/observer.rb index 9554dd8826..5f80bd86df 100644 --- a/activerecord/lib/active_record/observer.rb +++ b/activerecord/lib/active_record/observer.rb @@ -107,8 +107,9 @@ module ActiveRecord end protected + def observed_subclasses - observed_classes.sum([]) { |klass| klass.send(:subclasses) } + observed_classes.sum([]) { |klass| klass.send(:descendants) } end def observe_callbacks? diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 37f1ec11a6..36df878e1b 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -69,7 +69,6 @@ module ActiveRecord unless app.config.cache_classes ActiveSupport.on_load(:active_record) do ActionDispatch::Callbacks.after do - ActiveRecord::Base.reset_subclasses ActiveRecord::Base.clear_reloadable_connections! end end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 7a48a6596a..50e94134f5 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -116,45 +116,7 @@ module ActiveRecord def build_arel arel = table - joined_associations = [] - association_joins = [] - - joins = @joins_values.map {|j| j.respond_to?(:strip) ? j.strip : j}.uniq - - joins.each do |join| - association_joins << join if [Hash, Array, Symbol].include?(join.class) && !array_of_strings?(join) - end - - stashed_association_joins = joins.select {|j| j.is_a?(ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation)} - - non_association_joins = (joins - association_joins - stashed_association_joins).reject {|j| j.blank?} - custom_joins = custom_join_sql(*non_association_joins) - - join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, custom_joins) - - join_dependency.graft(*stashed_association_joins) - - @implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty? - - to_join = [] - - join_dependency.join_associations.each do |association| - if (association_relation = association.relation).is_a?(Array) - to_join << [association_relation.first, association.join_class, association.association_join.first] - to_join << [association_relation.last, association.join_class, association.association_join.last] - else - to_join << [association_relation, association.join_class, association.association_join] - end - end - - to_join.each do |tj| - unless joined_associations.detect {|ja| ja[0] == tj[0] && ja[1] == tj[1] && ja[2] == tj[2] } - joined_associations << tj - arel = arel.join(tj[0], tj[1]).on(*tj[2]) - end - end - - arel = arel.join(custom_joins) + arel = build_joins(arel, @joins_values) if @joins_values.present? @where_values.uniq.each do |where| next if where.blank? @@ -168,9 +130,7 @@ module ActiveRecord end end - @having_values.uniq.each do |h| - arel = h.is_a?(String) ? arel.having(h) : arel.having(*h) - end + arel = arel.having(*@having_values.uniq.select{|h| h.present?}) arel = arel.take(@limit_value) if @limit_value.present? arel = arel.skip(@offset_value) if @offset_value.present? @@ -181,18 +141,16 @@ module ActiveRecord selects = @select_values.uniq - quoted_table_name = @klass.quoted_table_name - if selects.present? selects.each do |s| @implicit_readonly = false arel = arel.project(s) if s.present? end else - arel = arel.project(quoted_table_name + '.*') + arel = arel.project(@klass.quoted_table_name + '.*') end - arel = @from_value.present? ? arel.from(@from_value) : arel.from(quoted_table_name) + arel = @from_value.present? ? arel.from(@from_value) : arel.from(@klass.quoted_table_name) case @lock_value when TrueClass @@ -221,6 +179,48 @@ module ActiveRecord private + def build_joins(relation, joins) + joined_associations = [] + association_joins = [] + + joins = @joins_values.map {|j| j.respond_to?(:strip) ? j.strip : j}.uniq + + joins.each do |join| + association_joins << join if [Hash, Array, Symbol].include?(join.class) && !array_of_strings?(join) + end + + stashed_association_joins = joins.select {|j| j.is_a?(ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation)} + + non_association_joins = (joins - association_joins - stashed_association_joins).reject {|j| j.blank?} + custom_joins = custom_join_sql(*non_association_joins) + + join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, custom_joins) + + join_dependency.graft(*stashed_association_joins) + + @implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty? + + to_join = [] + + join_dependency.join_associations.each do |association| + if (association_relation = association.relation).is_a?(Array) + to_join << [association_relation.first, association.join_class, association.association_join.first] + to_join << [association_relation.last, association.join_class, association.association_join.last] + else + to_join << [association_relation, association.join_class, association.association_join] + end + end + + to_join.each do |tj| + unless joined_associations.detect {|ja| ja[0] == tj[0] && ja[1] == tj[1] && ja[2] == tj[2] } + joined_associations << tj + relation = relation.join(tj[0], tj[1]).on(*tj[2]) + end + end + + relation.join(custom_joins) + end + def apply_modules(modules) values = Array.wrap(modules) @extensions += values if values.present? diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index b4b146994d..a7709b9489 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -322,6 +322,7 @@ module ActiveRecord if @_start_transaction_state[:level] < 1 restore_state = remove_instance_variable(:@_start_transaction_state) if restore_state + @attributes = @attributes.dup if @attributes.frozen? @new_record = restore_state[:new_record] @destroyed = restore_state[:destroyed] if restore_state[:id] diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 6b511e83db..b98fd353aa 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -52,12 +52,12 @@ module ActiveRecord # Runs all the specified validations and returns true if no errors were added otherwise false. def valid?(context = nil) context ||= (new_record? ? :create : :update) - super(context) + output = super(context) deprecated_callback_method(:validate) deprecated_callback_method(:"validate_on_#{context}") - errors.empty? + errors.empty? && output end protected |