From cfbe5958311f21397c98657c3370d03dcf720fe1 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Thu, 28 Oct 2010 18:00:52 +0200 Subject: Use application's generators defaults as engine defaults to not force users to manually set it --- activerecord/lib/active_record/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 868fd6c3ff..dfe255ad7c 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -14,7 +14,7 @@ module ActiveRecord config.active_record = ActiveSupport::OrderedOptions.new config.app_generators.orm :active_record, :migration => true, - :timestamps => true + :timestamps => true config.app_middleware.insert_after "::ActionDispatch::Callbacks", "ActiveRecord::QueryCache" -- cgit v1.2.3 From 1f06652a57e727700c3a673dc1f86e3b1e07ce1f Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Sun, 7 Nov 2010 08:05:18 -0600 Subject: use persisted? instead of new_record? wherever possible - persisted? is the API defined in ActiveModel - makes it easier for extension libraries to conform to ActiveModel APIs without concern for whether the extended object is specifically ActiveRecord [#5927 state:committed] Signed-off-by: Santiago Pastorino --- activerecord/lib/active_record/aggregations.rb | 2 +- activerecord/lib/active_record/associations.rb | 2 +- .../associations/association_collection.rb | 21 +++++++++++---------- .../active_record/associations/association_proxy.rb | 6 +++--- .../associations/belongs_to_association.rb | 6 +++--- .../has_and_belongs_to_many_association.rb | 2 +- .../associations/has_many_through_association.rb | 2 +- .../associations/has_one_association.rb | 12 ++++++------ .../associations/has_one_through_association.rb | 2 +- .../active_record/attribute_methods/primary_key.rb | 7 ++++--- .../lib/active_record/autosave_association.rb | 14 +++++++------- activerecord/lib/active_record/base.rb | 18 ++++++++++-------- .../lib/active_record/locking/optimistic.rb | 2 +- .../lib/active_record/locking/pessimistic.rb | 2 +- activerecord/lib/active_record/persistence.rb | 12 +++++++----- activerecord/lib/active_record/session_store.rb | 8 ++++---- activerecord/lib/active_record/transactions.rb | 13 +++++++------ activerecord/lib/active_record/validations.rb | 2 +- .../lib/active_record/validations/uniqueness.rb | 2 +- 19 files changed, 71 insertions(+), 64 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/aggregations.rb b/activerecord/lib/active_record/aggregations.rb index 16206c1056..8cd7389005 100644 --- a/activerecord/lib/active_record/aggregations.rb +++ b/activerecord/lib/active_record/aggregations.rb @@ -6,7 +6,7 @@ module ActiveRecord def clear_aggregation_cache #:nodoc: self.class.reflect_on_all_aggregations.to_a.each do |assoc| instance_variable_set "@#{assoc.name}", nil - end unless self.new_record? + end if self.persisted? end # Active Record implements aggregation through a macro-like class method called +composed_of+ diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index c84673e6a6..08a4ebfd7e 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -118,7 +118,7 @@ module ActiveRecord def clear_association_cache #:nodoc: self.class.reflect_on_all_associations.to_a.each do |assoc| instance_variable_set "@#{assoc.name}", nil - end unless self.new_record? + end if self.persisted? end private diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 896e18af01..398c41f3e9 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -120,13 +120,13 @@ module ActiveRecord # Since << flattens its argument list and inserts each record, +push+ and +concat+ behave identically. def <<(*records) result = true - load_target if @owner.new_record? + load_target unless @owner.persisted? transaction do flatten_deeper(records).each do |record| raise_on_type_mismatch(record) add_record_to_target_with_callbacks(record) do |r| - result &&= insert_record(record) unless @owner.new_record? + result &&= insert_record(record) if @owner.persisted? end end end @@ -285,12 +285,12 @@ module ActiveRecord # This method is abstract in the sense that it relies on # +count_records+, which is a method descendants have to provide. def size - if @owner.new_record? || (loaded? && !@reflection.options[:uniq]) + if !@owner.persisted? || (loaded? && !@reflection.options[:uniq]) @target.size elsif !loaded? && @reflection.options[:group] load_target.size elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array) - unsaved_records = @target.select { |r| r.new_record? } + unsaved_records = @target.reject { |r| r.persisted? } unsaved_records.size + count_records else count_records @@ -357,7 +357,7 @@ module ActiveRecord def include?(record) return false unless record.is_a?(@reflection.klass) - return include_in_memory?(record) if record.new_record? + return include_in_memory?(record) unless record.persisted? load_target if @reflection.options[:finder_sql] && !loaded? return @target.include?(record) if loaded? exists?(record) @@ -372,7 +372,7 @@ module ActiveRecord end def load_target - if !@owner.new_record? || foreign_key_present + if @owner.persisted? || foreign_key_present begin if !loaded? if @target.is_a?(Array) && @target.any? @@ -513,7 +513,7 @@ module ActiveRecord transaction do records.each { |record| callback(:before_remove, record) } - old_records = records.reject { |r| r.new_record? } + old_records = records.select { |r| r.persisted? } yield(records, old_records) records.each { |record| callback(:after_remove, record) } end @@ -538,14 +538,15 @@ module ActiveRecord end def ensure_owner_is_not_new - if @owner.new_record? + unless @owner.persisted? raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved" end end def fetch_first_or_last_using_find?(args) - args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] || - @target.any? { |record| record.new_record? } || args.first.kind_of?(Integer)) + args.first.kind_of?(Hash) || !(loaded? || !@owner.persisted? || @reflection.options[:finder_sql] || + @target.any? { |record| !record.persisted? } || args.first.kind_of?(Integer)) + # TODO - would prefer @target.none? { |r| r.persisted? } end def include_in_memory?(record) diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index ac2aa46edf..09bba213ce 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -175,10 +175,10 @@ module ActiveRecord # If the association is polymorphic the type of the owner is also set. def set_belongs_to_association_for(record) if @reflection.options[:as] - record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record? + record["#{@reflection.options[:as]}_id"] = @owner.id if @owner.persisted? record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s else - unless @owner.new_record? + if @owner.persisted? primary_key = @reflection.options[:primary_key] || :id record[@reflection.primary_key_name] = @owner.send(primary_key) end @@ -252,7 +252,7 @@ module ActiveRecord def load_target return nil unless defined?(@loaded) - if !loaded? and (!@owner.new_record? || foreign_key_present) + if !loaded? and (@owner.persisted? || foreign_key_present) @target = find_target end diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 34b6cd5576..b624951cd9 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -14,7 +14,7 @@ module ActiveRecord counter_cache_name = @reflection.counter_cache_column if record.nil? - if counter_cache_name && !@owner.new_record? + if counter_cache_name && @owner.persisted? @reflection.klass.decrement_counter(counter_cache_name, previous_record_id) if @owner[@reflection.primary_key_name] end @@ -22,13 +22,13 @@ module ActiveRecord else raise_on_type_mismatch(record) - if counter_cache_name && !@owner.new_record? && record.id != @owner[@reflection.primary_key_name] + if counter_cache_name && @owner.persisted? && record.id != @owner[@reflection.primary_key_name] @reflection.klass.increment_counter(counter_cache_name, record.id) @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name] end @target = (AssociationProxy === record ? record.target : record) - @owner[@reflection.primary_key_name] = record_id(record) unless record.new_record? + @owner[@reflection.primary_key_name] = record_id(record) if record.persisted? @updated = true end diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index 1fc9aba5cf..da742fa668 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -34,7 +34,7 @@ module ActiveRecord end def insert_record(record, force = true, validate = true) - if record.new_record? + unless record.persisted? if force record.save! else 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 437c8b1fd6..51770cff0f 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -59,7 +59,7 @@ module ActiveRecord end def insert_record(record, force = true, validate = true) - if record.new_record? + unless record.persisted? if force record.save! else diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 17901387e9..e6e037441f 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -30,18 +30,18 @@ module ActiveRecord if dependent? && !dont_save case @reflection.options[:dependent] when :delete - @target.delete unless @target.new_record? + @target.delete if @target.persisted? @owner.clear_association_cache when :destroy - @target.destroy unless @target.new_record? + @target.destroy if @target.persisted? @owner.clear_association_cache when :nullify @target[@reflection.primary_key_name] = nil - @target.save unless @owner.new_record? || @target.new_record? + @target.save if @owner.persisted? && @target.persisted? end else @target[@reflection.primary_key_name] = nil - @target.save unless @owner.new_record? || @target.new_record? + @target.save if @owner.persisted? && @target.persisted? end end @@ -56,7 +56,7 @@ module ActiveRecord set_inverse_instance(obj, @owner) @loaded = true - unless @owner.new_record? or obj.nil? or dont_save + unless !@owner.persisted? or obj.nil? or dont_save return (obj.save ? self : false) else return (obj.nil? ? nil : self) @@ -113,7 +113,7 @@ module ActiveRecord if replace_existing replace(record, true) else - record[@reflection.primary_key_name] = @owner.id unless @owner.new_record? + record[@reflection.primary_key_name] = @owner.id if @owner.persisted? self.target = record set_inverse_instance(record, @owner) end diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 7f28abf464..6e98f7dffb 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -21,7 +21,7 @@ module ActiveRecord if current_object new_value ? current_object.update_attributes(construct_join_attributes(new_value)) : current_object.destroy elsif new_value - if @owner.new_record? + unless @owner.persisted? self.target = new_value through_association = @owner.send(:association_instance_get, @reflection.through_reflection.name) through_association.build(construct_join_attributes(new_value)) diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index 82d94b848a..75ae06f5e9 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -3,10 +3,11 @@ module ActiveRecord module PrimaryKey extend ActiveSupport::Concern - # Returns this record's primary key value wrapped in an Array - # or nil if the record is a new_record? + # Returns this record's primary key value wrapped in an Array or nil if + # the record is not persisted? or has just been destroyed. def to_key - new_record? ? nil : [ id ] + key = send(self.class.primary_key) + [key] if key end module ClassMethods diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 0b89a49896..cb5bc06580 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -217,7 +217,7 @@ module ActiveRecord # Returns whether or not this record has been changed in any way (including whether # any of its nested autosave associations are likewise changed) def changed_for_autosave? - new_record? || changed? || marked_for_destruction? || nested_records_changed_for_autosave? + !persisted? || changed? || marked_for_destruction? || nested_records_changed_for_autosave? end private @@ -231,7 +231,7 @@ module ActiveRecord elsif autosave association.target.find_all { |record| record.changed_for_autosave? } else - association.target.find_all { |record| record.new_record? } + association.target.find_all { |record| !record.persisted? } end end @@ -257,7 +257,7 @@ module ActiveRecord # +reflection+. def validate_collection_association(reflection) if association = association_instance_get(reflection.name) - if records = associated_records_to_validate_or_save(association, new_record?, reflection.options[:autosave]) + if records = associated_records_to_validate_or_save(association, !persisted?, reflection.options[:autosave]) records.each { |record| association_valid?(reflection, record) } end end @@ -286,7 +286,7 @@ module ActiveRecord # Is used as a before_save callback to check while saving a collection # association whether or not the parent was a new record before saving. def before_save_collection_association - @new_record_before_save = new_record? + @new_record_before_save = !persisted? true end @@ -308,7 +308,7 @@ module ActiveRecord if autosave && record.marked_for_destruction? association.destroy(record) - elsif autosave != false && (@new_record_before_save || record.new_record?) + elsif autosave != false && (@new_record_before_save || !record.persisted?) if autosave saved = association.send(:insert_record, record, false, false) else @@ -343,7 +343,7 @@ module ActiveRecord association.destroy else key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id - if autosave != false && (new_record? || association.new_record? || association[reflection.primary_key_name] != key || autosave) + if autosave != false && (!persisted? || !association.persisted? || association[reflection.primary_key_name] != key || autosave) association[reflection.primary_key_name] = key saved = association.save(:validate => !autosave) raise ActiveRecord::Rollback if !saved && autosave @@ -363,7 +363,7 @@ module ActiveRecord if autosave && association.marked_for_destruction? association.destroy elsif autosave != false - saved = association.save(:validate => !autosave) if association.new_record? || autosave + saved = association.save(:validate => !autosave) if !association.persisted? || autosave if association.updated? association_id = association.send(reflection.options[:primary_key] || :id) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 06a388cd21..3e295eeaae 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -204,7 +204,7 @@ module ActiveRecord #:nodoc: # # # No 'Winter' tag exists # winter = Tag.find_or_initialize_by_name("Winter") - # winter.new_record? # true + # winter.persisted? # false # # To find by a subset of the attributes to be used for instantiating a new object, pass a hash instead of # a list of parameters. @@ -1368,7 +1368,7 @@ MSG def initialize(attributes = nil) @attributes = attributes_from_column_definition @attributes_cache = {} - @new_record = true + @persited = false @readonly = false @destroyed = false @marked_for_destruction = false @@ -1403,7 +1403,7 @@ MSG clear_aggregation_cache clear_association_cache @attributes_cache = {} - @new_record = true + @persisted = false ensure_proper_type populate_with_current_scope_attributes @@ -1422,7 +1422,8 @@ MSG def init_with(coder) @attributes = coder['attributes'] @attributes_cache, @previously_changed, @changed_attributes = {}, {}, {} - @new_record = @readonly = @destroyed = @marked_for_destruction = false + @readonly = @destroyed = @marked_for_destruction = false + @persisted = true _run_find_callbacks _run_initialize_callbacks end @@ -1463,7 +1464,7 @@ MSG # Person.find(5).cache_key # => "people/5-20071224150000" (updated_at available) def cache_key case - when new_record? + when !persisted? "#{self.class.model_name.cache_key}/new" when timestamp = self[:updated_at] "#{self.class.model_name.cache_key}/#{id}-#{timestamp.to_s(:number)}" @@ -1584,8 +1585,9 @@ MSG # Returns true if the +comparison_object+ is the same object, or is of the same type and has the same id. def ==(comparison_object) comparison_object.equal?(self) || - (comparison_object.instance_of?(self.class) && - comparison_object.id == id && !comparison_object.new_record?) + persisted? && + (comparison_object.instance_of?(self.class) && + comparison_object.id == id) end # Delegates to == @@ -1630,7 +1632,7 @@ MSG # Returns the contents of the record as a nicely formatted string. def inspect attributes_as_nice_string = self.class.column_names.collect { |name| - if has_attribute?(name) || new_record? + if has_attribute?(name) || !persisted? "#{name}: #{attribute_for_inspect(name)}" end }.compact.join(", ") diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index b6f87a57b8..bf626301f1 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -109,7 +109,7 @@ module ActiveRecord def destroy #:nodoc: return super unless locking_enabled? - unless new_record? + if persisted? lock_col = self.class.locking_column previous_value = send(lock_col).to_i diff --git a/activerecord/lib/active_record/locking/pessimistic.rb b/activerecord/lib/active_record/locking/pessimistic.rb index 9ad6a2baf7..d900831e13 100644 --- a/activerecord/lib/active_record/locking/pessimistic.rb +++ b/activerecord/lib/active_record/locking/pessimistic.rb @@ -47,7 +47,7 @@ module ActiveRecord # or pass true for "FOR UPDATE" (the default, an exclusive row lock). Returns # the locked record. def lock!(lock = true) - reload(:lock => lock) unless new_record? + reload(:lock => lock) if persisted? self end end diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 707c1a05be..f905b7026b 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -4,7 +4,8 @@ module ActiveRecord # Returns true if this object hasn't been saved yet -- that is, a record # for the object doesn't exist in the data store yet; otherwise, returns false. def new_record? - @new_record + @persisted = false unless defined?(@persisted) + !@persisted end # Returns true if this object has been destroyed, otherwise returns false. @@ -15,7 +16,8 @@ module ActiveRecord # Returns if the record is persisted, i.e. it's not a new record and it was # not destroyed. def persisted? - !(new_record? || destroyed?) + @persisted = false unless defined?(@persisted) + !!@persisted && !destroyed? end # Saves the model. @@ -94,7 +96,7 @@ module ActiveRecord 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.instance_variable_set("@persisted", persisted?) became.instance_variable_set("@destroyed", destroyed?) became end @@ -240,7 +242,7 @@ module ActiveRecord private def create_or_update raise ReadOnlyRecord if readonly? - result = new_record? ? create : update + result = persisted? ? update : create result != false end @@ -269,7 +271,7 @@ module ActiveRecord self.id ||= new_id - @new_record = false + @persisted = true id end diff --git a/activerecord/lib/active_record/session_store.rb b/activerecord/lib/active_record/session_store.rb index 3fc596e02a..a64c5f80c9 100644 --- a/activerecord/lib/active_record/session_store.rb +++ b/activerecord/lib/active_record/session_store.rb @@ -228,7 +228,7 @@ module ActiveRecord @session_id = attributes[:session_id] @data = attributes[:data] @marshaled_data = attributes[:marshaled_data] - @new_record = @marshaled_data.nil? + @persisted = !@marshaled_data.nil? end # Lazy-unmarshal session state. @@ -252,8 +252,8 @@ module ActiveRecord marshaled_data = self.class.marshal(data) connect = connection - if @new_record - @new_record = false + unless @persisted + @persisted = true connect.update <<-end_sql, 'Create session' INSERT INTO #{table_name} ( #{connect.quote_column_name(session_id_column)}, @@ -272,7 +272,7 @@ module ActiveRecord end def destroy - return if @new_record + return unless @persisted connect = connection connect.delete <<-end_sql, 'Destroy session' diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index ab737f0f88..851ec62381 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -242,7 +242,7 @@ module ActiveRecord with_transaction_returning_status { super } end - # Reset id and @new_record if the transaction rolls back. + # Reset id and @persisted if the transaction rolls back. def rollback_active_record_state! remember_transaction_record_state yield @@ -297,9 +297,10 @@ module ActiveRecord # Save the new record state and id of a record so it can be restored later if a transaction fails. def remember_transaction_record_state #:nodoc @_start_transaction_state ||= {} - unless @_start_transaction_state.include?(:new_record) + unless @_start_transaction_state.include?(:persisted) @_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key) - @_start_transaction_state[:new_record] = @new_record + @persisted = false unless defined?(@persisted) + @_start_transaction_state[:persisted] = @persisted end unless @_start_transaction_state.include?(:destroyed) @_start_transaction_state[:destroyed] = @destroyed @@ -323,7 +324,7 @@ module ActiveRecord restore_state = remove_instance_variable(:@_start_transaction_state) if restore_state @attributes = @attributes.dup if @attributes.frozen? - @new_record = restore_state[:new_record] + @persisted = restore_state[:persisted] @destroyed = restore_state[:destroyed] if restore_state[:id] self.id = restore_state[:id] @@ -345,11 +346,11 @@ module ActiveRecord def transaction_include_action?(action) #:nodoc case action when :create - transaction_record_state(:new_record) + transaction_record_state(:new_record) || !transaction_record_state(:persisted) when :destroy destroyed? when :update - !(transaction_record_state(:new_record) || destroyed?) + !(transaction_record_state(:new_record) || !transaction_record_state(:persisted) || destroyed?) end end end diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index f367315b22..ee45fcdf35 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -51,7 +51,7 @@ 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) + context ||= (persisted? ? :update : :create) output = super(context) errors.empty? && output end diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index a25558bd80..3eba7510ac 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -31,7 +31,7 @@ module ActiveRecord relation = relation.where(scope_item => scope_value) end - unless record.new_record? + if record.persisted? # TODO : This should be in Arel relation = relation.where("#{record.class.quoted_table_name}.#{record.class.primary_key} <> ?", record.send(:id)) end -- cgit v1.2.3 From 994a1c2a4747efcca3c6278c119096d93f793da1 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 9 Nov 2010 14:03:09 -0200 Subject: Execute less operations --- activerecord/lib/active_record/associations/association_collection.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 398c41f3e9..71d8c2d3c8 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -545,8 +545,7 @@ module ActiveRecord def fetch_first_or_last_using_find?(args) args.first.kind_of?(Hash) || !(loaded? || !@owner.persisted? || @reflection.options[:finder_sql] || - @target.any? { |record| !record.persisted? } || args.first.kind_of?(Integer)) - # TODO - would prefer @target.none? { |r| r.persisted? } + !@target.all? { |record| record.persisted? } || args.first.kind_of?(Integer)) end def include_in_memory?(record) -- cgit v1.2.3 From 75dfd95618310a9522a73d8126b35351c8189042 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 9 Nov 2010 16:00:02 -0200 Subject: Don't check if persisted is defined just initialize it properly --- activerecord/lib/active_record/base.rb | 2 +- activerecord/lib/active_record/persistence.rb | 2 -- activerecord/lib/active_record/transactions.rb | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 3e295eeaae..f588475bbf 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1368,7 +1368,7 @@ MSG def initialize(attributes = nil) @attributes = attributes_from_column_definition @attributes_cache = {} - @persited = false + @persisted = false @readonly = false @destroyed = false @marked_for_destruction = false diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index f905b7026b..390e09d826 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -4,7 +4,6 @@ module ActiveRecord # Returns true if this object hasn't been saved yet -- that is, a record # for the object doesn't exist in the data store yet; otherwise, returns false. def new_record? - @persisted = false unless defined?(@persisted) !@persisted end @@ -16,7 +15,6 @@ module ActiveRecord # Returns if the record is persisted, i.e. it's not a new record and it was # not destroyed. def persisted? - @persisted = false unless defined?(@persisted) !!@persisted && !destroyed? end diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 851ec62381..8c94d1a2bc 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -299,7 +299,6 @@ module ActiveRecord @_start_transaction_state ||= {} unless @_start_transaction_state.include?(:persisted) @_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key) - @persisted = false unless defined?(@persisted) @_start_transaction_state[:persisted] = @persisted end unless @_start_transaction_state.include?(:destroyed) -- cgit v1.2.3 From 88a559d4148789474c6dbd216725c69fa18200a9 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 9 Nov 2010 16:53:03 -0200 Subject: Double negation of an already boolean value produces the same result --- activerecord/lib/active_record/persistence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 390e09d826..00d49dd0f6 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -15,7 +15,7 @@ module ActiveRecord # Returns if the record is persisted, i.e. it's not a new record and it was # not destroyed. def persisted? - !!@persisted && !destroyed? + @persisted && !destroyed? end # Saves the model. -- cgit v1.2.3 From 9c9f416d398c47dd2297fa5cc4025e0800d32212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 11 Nov 2010 14:17:14 +0100 Subject: Allow AR Session Store to be renewed --- activerecord/lib/active_record/session_store.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/session_store.rb b/activerecord/lib/active_record/session_store.rb index a64c5f80c9..ba99800fb2 100644 --- a/activerecord/lib/active_record/session_store.rb +++ b/activerecord/lib/active_record/session_store.rb @@ -321,6 +321,7 @@ module ActiveRecord if sid = current_session_id(env) Base.silence do get_session_model(env, sid).destroy + env[SESSION_RECORD_KEY] = nil end end -- cgit v1.2.3 From 894fd288596b005148c3882e6431f6453d198101 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 11 Nov 2010 15:23:37 -0200 Subject: Fixed bug in active_record/nested_attributes where an empty string id caused an ActiveRecord::RecordNotFound error. Found by [Ben Tillman] [#5638 state:committed] Signed-off-by: Santiago Pastorino --- activerecord/lib/active_record/nested_attributes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index aca91c907d..bbce6df98f 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -323,7 +323,7 @@ module ActiveRecord (options[:update_only] || record.id.to_s == attributes['id'].to_s) assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes) - elsif attributes['id'] + elsif not attributes['id'].blank? raise_nested_attributes_record_not_found(association_name, attributes['id']) elsif !reject_new_record?(association_name, attributes) -- cgit v1.2.3 From 296e5c38f1b501d39c247f5b7daccedded4e497b Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 11 Nov 2010 15:24:49 -0200 Subject: Use ! instead of not --- activerecord/lib/active_record/nested_attributes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index bbce6df98f..fb48459b6f 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -323,7 +323,7 @@ module ActiveRecord (options[:update_only] || record.id.to_s == attributes['id'].to_s) assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes) - elsif not attributes['id'].blank? + elsif !attributes['id'].blank? raise_nested_attributes_record_not_found(association_name, attributes['id']) elsif !reject_new_record?(association_name, attributes) -- cgit v1.2.3 From de2933e1a062f0752512eb0ec60f7217f4890f8c Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Fri, 12 Nov 2010 01:37:12 +0800 Subject: STI type is now updated when calling AR::Base.becomes on subclasses [#5953 state:resolved] --- activerecord/lib/active_record/persistence.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 00d49dd0f6..594a2214bb 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -96,6 +96,7 @@ module ActiveRecord became.instance_variable_set("@attributes_cache", @attributes_cache) became.instance_variable_set("@persisted", persisted?) became.instance_variable_set("@destroyed", destroyed?) + became.type = klass.name unless self.class.descends_from_active_record? became end -- cgit v1.2.3 From b734e192a70d63b4420a96a00e4f181d27e30129 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 11 Nov 2010 18:59:54 -0200 Subject: present? is better here --- activerecord/lib/active_record/nested_attributes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index fb48459b6f..0c3392263a 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -323,7 +323,7 @@ module ActiveRecord (options[:update_only] || record.id.to_s == attributes['id'].to_s) assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes) - elsif !attributes['id'].blank? + elsif attributes['id'].present? raise_nested_attributes_record_not_found(association_name, attributes['id']) elsif !reject_new_record?(association_name, attributes) -- cgit v1.2.3 From 983976ec5484b92d1b46305a2e3bd59bf98a3853 Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Sat, 13 Nov 2010 06:29:30 +0800 Subject: Finder gives a little bit more info on the lookup column (primary key) --- activerecord/lib/active_record/relation/finder_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index e9e9c85122..4192456447 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -300,7 +300,7 @@ module ActiveRecord unless record conditions = arel.where_sql conditions = " [#{conditions}]" if conditions - raise RecordNotFound, "Couldn't find #{@klass.name} with ID=#{id}#{conditions}" + raise RecordNotFound, "Couldn't find #{@klass.name} with #{@klass.primary_key}=#{id}#{conditions}" end record -- cgit v1.2.3 From 93c9f4a942c622ecbe8d46ab4838bd072aef9fc0 Mon Sep 17 00:00:00 2001 From: Jason Cheow Date: Sun, 14 Nov 2010 13:55:36 +0800 Subject: Fix bug where size of through association is not correct after adding a has_many association (occurs only before main object has been reloaded). [#5968 state:committed] Signed-off-by: Santiago Pastorino --- .../lib/active_record/associations/has_many_through_association.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activerecord/lib') 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 51770cff0f..0a5858f36e 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -69,7 +69,6 @@ module ActiveRecord 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 bc556a21e2d5722f42dfac810c04d453076e09fb Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sun, 14 Nov 2010 15:34:07 -0200 Subject: Remove unused var --- .../lib/active_record/associations/has_many_through_association.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') 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 0a5858f36e..79c229d9c4 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -68,7 +68,7 @@ module ActiveRecord end through_association = @owner.send(@reflection.through_reflection.name) - through_record = through_association.create!(construct_join_attributes(record)) + through_association.create!(construct_join_attributes(record)) end # TODO - add dependent option support -- cgit v1.2.3 From 7c5c1a07c03ec03536636c26e09b80b29a59beed Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sun, 14 Nov 2010 21:54:24 -0500 Subject: if association is already loaded and if a find operation is performed on the already loaded association list with an empty hash then do not perform another sql [#5972 state:resolved] --- activerecord/lib/active_record/associations/association_collection.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 71d8c2d3c8..5b34ac907c 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -75,6 +75,7 @@ module ActiveRecord find(:first, *args) else load_target unless loaded? + args = args[1..-1] if args.first.kind_of?(Hash) && args.first.empty? @target.first(*args) end end @@ -544,7 +545,7 @@ module ActiveRecord end def fetch_first_or_last_using_find?(args) - args.first.kind_of?(Hash) || !(loaded? || !@owner.persisted? || @reflection.options[:finder_sql] || + (args.first.kind_of?(Hash) && !args.first.empty?) || !(loaded? || !@owner.persisted? || @reflection.options[:finder_sql] || !@target.all? { |record| record.persisted? } || args.first.kind_of?(Integer)) end -- cgit v1.2.3 From dcdfc84f55ea1a7880a30f63b6517745310d24eb Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 15 Nov 2010 14:35:28 -0800 Subject: use quoted id of single AR::Base objects in predicates --- activerecord/lib/active_record/relation/predicate_builder.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index c5428dccd6..2eda404490 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -25,6 +25,8 @@ module ActiveRecord attribute.in(values) when Range, Arel::Relation attribute.in(value) + when ActiveRecord::Base + attribute.eq(value.quoted_id) else attribute.eq(value) end -- cgit v1.2.3 From ace84a003cb48f60ca478c05c1fd8a57d37663cf Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 15 Nov 2010 20:24:58 -0800 Subject: support finding by a ruby class [#5979 state:resolved] --- activerecord/lib/active_record/relation/predicate_builder.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 2eda404490..32c7d08daa 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -27,6 +27,9 @@ module ActiveRecord attribute.in(value) when ActiveRecord::Base attribute.eq(value.quoted_id) + when Class + # FIXME: I think we need to deprecate this behavior + attribute.eq(value.name) else attribute.eq(value) end -- cgit v1.2.3 From 4718d097ffe3af965f3ea7218156050507eabe4f Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 16 Nov 2010 12:36:47 -0200 Subject: Models should be equals even after destroyed [#5978 state:committed] --- activerecord/lib/active_record/base.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f588475bbf..658c6d7b45 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1585,9 +1585,9 @@ MSG # Returns true if the +comparison_object+ is the same object, or is of the same type and has the same id. def ==(comparison_object) comparison_object.equal?(self) || - persisted? && - (comparison_object.instance_of?(self.class) && - comparison_object.id == id) + comparison_object.instance_of?(self.class) && + id.present? && + comparison_object.id == id end # Delegates to == -- cgit v1.2.3 From a820d0afdd4a09e0902d7b7c4d9724f50089d254 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 16 Nov 2010 15:55:43 +0100 Subject: revises RDoc of AR::Base#== --- activerecord/lib/active_record/base.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 658c6d7b45..b35f59d6df 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1582,7 +1582,15 @@ MSG self.class.columns_hash[name.to_s] end - # Returns true if the +comparison_object+ is the same object, or is of the same type and has the same id. + # Returns true if +comparison_object+ is the same exact object, or +comparison_object+ + # is of the same type and +self+ has an ID and it is equal to +comparison_object.id+. + # + # Note that new records are different from any other record by definition, unless the + # other record is the receiver itself. Besides, if you fetch existing records with + # +select+ and leave the ID out, you're on your own, this predicate will return false. + # + # Note also that destroying a record preserves its ID in the model instance, so deleted + # models are still comparable. def ==(comparison_object) comparison_object.equal?(self) || comparison_object.instance_of?(self.class) && -- cgit v1.2.3 From 437ceab139c9aace851b41ce6103d29302750e0c Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Tue, 16 Nov 2010 17:00:01 +0100 Subject: Create directory before copying migrations if it does not exist --- activerecord/lib/active_record/migration.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index a4c09b654a..2c306d233a 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -387,6 +387,8 @@ module ActiveRecord def copy(destination, sources, options = {}) copied = [] + FileUtils.mkdir_p(destination) unless File.exists?(destination) + destination_migrations = ActiveRecord::Migrator.migrations(destination) last = destination_migrations.last sources.each do |name, path| -- cgit v1.2.3 From a5cdf0b9eb860c4370ae5fde231e1b61f71b6b65 Mon Sep 17 00:00:00 2001 From: Alexandru Catighera Date: Mon, 15 Nov 2010 21:33:21 -0500 Subject: Fix ActiveRecord calculations when grouped by multiple fields --- .../lib/active_record/relation/calculations.rb | 32 ++++++++++++---------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 6bf698fe97..fe5c85d24a 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -208,14 +208,19 @@ module ActiveRecord end def execute_grouped_calculation(operation, column_name, distinct) #:nodoc: - group_attr = @group_values.first - association = @klass.reflect_on_association(group_attr.to_sym) - associated = association && association.macro == :belongs_to # only count belongs_to associations - group_field = associated ? association.primary_key_name : group_attr - group_alias = column_alias_for(group_field) - group_column = column_for(group_field) + group_attr = @group_values + association = @klass.reflect_on_association(group_attr.first.to_sym) + associated = group_attr.size == 1 && association && association.macro == :belongs_to # only count belongs_to associations + group_fields = Array(associated ? association.primary_key_name : group_attr) + group_aliases = [] + group_columns = {} + + group_fields.each do |field| + group_aliases << column_alias_for(field) + group_columns[column_alias_for(field)] = column_for(field) + end - group = @klass.connection.adapter_name == 'FrontBase' ? group_alias : group_field + group = @klass.connection.adapter_name == 'FrontBase' ? group_aliases : group_fields if operation == 'count' && column_name == :all aggregate_alias = 'count_all' @@ -223,22 +228,21 @@ module ActiveRecord aggregate_alias = column_alias_for(operation, column_name) end - relation = except(:group).group(group) - relation.select_values = [ - operation_over_aggregate_column(aggregate_column(column_name), operation, distinct).as(aggregate_alias), - "#{group_field} AS #{group_alias}" - ] + relation = except(:group).group(group.join(',')) + relation.select_values = [ operation_over_aggregate_column(aggregate_column(column_name), operation, distinct).as(aggregate_alias) ] + group_fields.each_index{ |i| relation.select_values << "#{group_fields[i]} AS #{group_aliases[i]}" } calculated_data = @klass.connection.select_all(relation.to_sql) if association - key_ids = calculated_data.collect { |row| row[group_alias] } + key_ids = calculated_data.collect { |row| row[group_aliases.first] } key_records = association.klass.base_class.find(key_ids) key_records = Hash[key_records.map { |r| [r.id, r] }] end ActiveSupport::OrderedHash[calculated_data.map do |row| - key = type_cast_calculated_value(row[group_alias], group_column) + key = group_aliases.map{|group_alias| type_cast_calculated_value(row[group_alias], group_columns[group_alias])} + key = key.first if key.size == 1 key = key_records[key] if associated [key, type_cast_calculated_value(row[aggregate_alias], column_for(column_name), operation)] end] -- cgit v1.2.3 From 7ebd36d1c4f958ceaf10cf7899936caeb173ac50 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Nov 2010 11:43:11 -0800 Subject: refactor to reduce method calls --- .../lib/active_record/relation/calculations.rb | 29 ++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index fe5c85d24a..c8adaddfca 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -212,13 +212,10 @@ module ActiveRecord association = @klass.reflect_on_association(group_attr.first.to_sym) associated = group_attr.size == 1 && association && association.macro == :belongs_to # only count belongs_to associations group_fields = Array(associated ? association.primary_key_name : group_attr) - group_aliases = [] - group_columns = {} - - group_fields.each do |field| - group_aliases << column_alias_for(field) - group_columns[column_alias_for(field)] = column_for(field) - end + group_aliases = group_fields.map { |field| column_alias_for(field) } + group_columns = group_aliases.zip(group_fields).map { |aliaz,field| + [aliaz, column_for(field)] + } group = @klass.connection.adapter_name == 'FrontBase' ? group_aliases : group_fields @@ -228,9 +225,19 @@ module ActiveRecord aggregate_alias = column_alias_for(operation, column_name) end + select_values = [ + operation_over_aggregate_column( + aggregate_column(column_name), + operation, + distinct).as(aggregate_alias) + ] + + select_values.concat group_fields.zip(group_aliases).map { |field,aliaz| + "#{field} AS #{aliaz}" + } + relation = except(:group).group(group.join(',')) - relation.select_values = [ operation_over_aggregate_column(aggregate_column(column_name), operation, distinct).as(aggregate_alias) ] - group_fields.each_index{ |i| relation.select_values << "#{group_fields[i]} AS #{group_aliases[i]}" } + relation.select_values = select_values calculated_data = @klass.connection.select_all(relation.to_sql) @@ -241,7 +248,9 @@ module ActiveRecord end ActiveSupport::OrderedHash[calculated_data.map do |row| - key = group_aliases.map{|group_alias| type_cast_calculated_value(row[group_alias], group_columns[group_alias])} + key = group_columns.map { |aliaz, column| + type_cast_calculated_value(row[aliaz], column) + } key = key.first if key.size == 1 key = key_records[key] if associated [key, type_cast_calculated_value(row[aggregate_alias], column_for(column_name), operation)] -- cgit v1.2.3 From 59ba800698ac9dcea1df9e40bb03335ddb4f5156 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Nov 2010 13:34:18 -0800 Subject: refactoring uniq method --- .../lib/active_record/associations/association_collection.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 5b34ac907c..7cdd91e9b2 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -332,13 +332,10 @@ module ActiveRecord end def uniq(collection = self) - seen = Set.new - collection.map do |record| - unless seen.include?(record.id) - seen << record.id - record - end - end.compact + seen = {} + collection.find_all do |record| + seen[record.id] = true unless seen.key?(record.id) + end end # Replace this collection with +other_array+ -- cgit v1.2.3 From c801f233df9d20c59d9756a5279365603dc5cbbd Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Nov 2010 13:43:44 -0800 Subject: reloading an association will properly set attributes of instantiated objects. Thanks Brian Palmer [#5802 state:resolved] --- activerecord/lib/active_record/associations/association_collection.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 7cdd91e9b2..600f59026e 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -379,7 +379,9 @@ module ActiveRecord if i @target.delete_at(i).tap do |t| keys = ["id"] + t.changes.keys + (f.attribute_names - t.attribute_names) - t.attributes = f.attributes.except(*keys) + f.attributes.except(*keys).each do |k,v| + t.send("#{k}=", v) + end end else f -- cgit v1.2.3 From 5f608fc7c4fa3ac793f1eb1b1f418033dfb13049 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Nov 2010 13:52:57 -0800 Subject: removing space errors --- .../lib/active_record/associations/association_collection.rb | 6 +++--- activerecord/lib/active_record/associations/association_proxy.rb | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 600f59026e..34247deb02 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -182,7 +182,7 @@ module ActiveRecord unless options.blank? raise ArgumentError, "If finder_sql/counter_sql is used then options cannot be passed" end - + @reflection.klass.count_by_sql(custom_counter_sql) else @@ -435,10 +435,10 @@ module ActiveRecord # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ counter_sql = @reflection.options[:finder_sql].sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } end - + interpolate_sql(counter_sql) end - + def custom_finder_sql interpolate_sql(@reflection.options[:finder_sql]) end diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 09bba213ce..7cd04a1ad5 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -211,12 +211,12 @@ module ActiveRecord :create => construct_create_scope } end - + # Implemented by subclasses def construct_find_scope raise NotImplementedError end - + # Implemented by (some) subclasses def construct_create_scope {} -- cgit v1.2.3 From 05320e3788a1be19956e7caa822484b816e11366 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Nov 2010 14:19:14 -0800 Subject: use unless instead of if ! --- activerecord/lib/active_record/associations/association_collection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 34247deb02..98507ad1d1 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -372,7 +372,7 @@ module ActiveRecord def load_target if @owner.persisted? || foreign_key_present begin - if !loaded? + unless loaded? if @target.is_a?(Array) && @target.any? @target = find_target.map do |f| i = @target.index(f) -- cgit v1.2.3 From 08c64bbd390d1ba6b44d29c5892693e219d20c5f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Nov 2010 14:28:09 -0800 Subject: super automatically passes on the implicit block --- .../lib/active_record/associations/association_collection.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 98507ad1d1..6090376bb8 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -408,11 +408,7 @@ module ActiveRecord end if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) - if block_given? - super { |*block_args| yield(*block_args) } - else - super - end + super elsif @reflection.klass.scopes[method] @_named_scopes_cache ||= {} @_named_scopes_cache[method] ||= {} -- cgit v1.2.3 From ccd2f3ede585fb2f2d830e661dbb8b8538de2dc5 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 16 Nov 2010 15:11:46 -0800 Subject: Update the version.rb files to include a PRE part --- activerecord/lib/active_record/version.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/version.rb b/activerecord/lib/active_record/version.rb index 89eba15be1..0667be7d23 100644 --- a/activerecord/lib/active_record/version.rb +++ b/activerecord/lib/active_record/version.rb @@ -3,8 +3,8 @@ module ActiveRecord MAJOR = 3 MINOR = 1 TINY = 0 - BUILD = "beta" + PRE = "beta" - STRING = [MAJOR, MINOR, TINY, BUILD].join('.') + STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.') end end -- cgit v1.2.3