From 7b7cedcb8d53110492e7d51405986f3e8e899fa4 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Sun, 20 Jun 2010 15:51:49 -0300 Subject: Don't waste time building relations if there are no values presents. [#4860 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/relation/query_methods.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 50e94134f5..60fa839df2 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -130,14 +130,14 @@ module ActiveRecord end end - arel = arel.having(*@having_values.uniq.select{|h| h.present?}) + arel = arel.having(*@having_values.uniq.select{|h| h.present?}) if @having_values.present? arel = arel.take(@limit_value) if @limit_value.present? arel = arel.skip(@offset_value) if @offset_value.present? - arel = arel.group(*@group_values.uniq.select{|g| g.present?}) + arel = arel.group(*@group_values.uniq.select{|g| g.present?}) if @group_values.present? - arel = arel.order(*@order_values.uniq.select{|o| o.present?}.map(&:to_s)) + arel = arel.order(*@order_values.uniq.select{|o| o.present?}.map(&:to_s)) if @order_values.present? selects = @select_values.uniq @@ -150,7 +150,7 @@ module ActiveRecord arel = arel.project(@klass.quoted_table_name + '.*') end - arel = @from_value.present? ? arel.from(@from_value) : arel.from(@klass.quoted_table_name) + arel = arel.from(@from_value) if @from_value.present? case @lock_value when TrueClass -- cgit v1.2.3 From 26392c4ac57e27c63984d47c6326c13f502d5786 Mon Sep 17 00:00:00 2001 From: Jeroen van Dijk Date: Wed, 19 May 2010 15:25:46 +0100 Subject: Make ActiveModel::Errors#add_on_blank and #add_on_empty accept an options hash and make various Validators pass their (filtered) options. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes it possible to pass additional options through Validators to message generation. E.g. plugin authors want to add validates_presence_of :foo, :format => "some format". Also, cleanup the :default vs :message options confusion in ActiveModel validation message generation. Also, deprecate ActiveModel::Errors#add_on_blank(attributes, custom_message) in favor of ActiveModel::Errors#add_on_blank(attributes, options). Also, refactoring of ActiveModel and ActiveRecord Validation tests. Test are a lot more DRY now. Better test coverage as well now. The first four points were reapplied from an older patch of Sven Fuchs which didn't apply cleanly anymore and was not complete yet. Signed-off-by: José Valim --- activerecord/lib/active_record/validations/associated.rb | 2 +- activerecord/lib/active_record/validations/uniqueness.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/validations/associated.rb b/activerecord/lib/active_record/validations/associated.rb index e41635134c..0b0f5682aa 100644 --- a/activerecord/lib/active_record/validations/associated.rb +++ b/activerecord/lib/active_record/validations/associated.rb @@ -3,7 +3,7 @@ module ActiveRecord class AssociatedValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) return if (value.is_a?(Array) ? value : [value]).collect{ |r| r.nil? || r.valid? }.all? - record.errors.add(attribute, :invalid, :default => options[:message], :value => value) + record.errors.add(attribute, :invalid, options.merge(:value => value)) end end diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 6283bdd0d6..1c9ecc7b1b 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -32,7 +32,7 @@ module ActiveRecord end if relation.exists? - record.errors.add(attribute, :taken, :default => options[:message], :value => value) + record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value)) end end -- cgit v1.2.3 From 7df105b1e604846f5dfe184128f99499da477bbe Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Tue, 22 Jun 2010 10:46:12 -0300 Subject: Fix order method to accept relation attributes. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/relation/query_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 60fa839df2..43032ba9d8 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -137,7 +137,7 @@ module ActiveRecord arel = arel.group(*@group_values.uniq.select{|g| g.present?}) if @group_values.present? - arel = arel.order(*@order_values.uniq.select{|o| o.present?}.map(&:to_s)) if @order_values.present? + arel = arel.order(*@order_values.uniq.select{|o| o.present?}) if @order_values.present? selects = @select_values.uniq -- cgit v1.2.3 From d0df7f1196ee633a0cea7b5b8cf8b65a8269df19 Mon Sep 17 00:00:00 2001 From: James Harton Date: Mon, 21 Jun 2010 14:21:54 +1200 Subject: Fix small bug where ActiveRecord::PredicateBuilder#build_from_hash didn't test for Arel::Relation as right hand value. [#4917 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/relation/predicate_builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (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 d0efa2189d..d853fd63d1 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -28,7 +28,7 @@ module ActiveRecord when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Relation values = value.to_a attribute.in(values) - when Range + when Range, Arel::Relation attribute.in(value) else attribute.eq(value) -- cgit v1.2.3 From 62c4e4d3856b38ee9869f4ad6342e712788c8635 Mon Sep 17 00:00:00 2001 From: Edgars Beigarts Date: Tue, 22 Jun 2010 11:48:11 +0300 Subject: Fix connection reloading in development mode. [#4929 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../active_record/connection_adapters/abstract/connection_pool.rb | 4 +++- .../lib/active_record/connection_adapters/abstract_adapter.rb | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') 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 979ed52f4a..c2d79a421d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -144,7 +144,9 @@ module ActiveRecord @connections.each do |conn| conn.disconnect! if conn.requires_reloading? end - @connections = [] + @connections.delete_if do |conn| + conn.requires_reloading? + end end # Verify active connections and remove and disconnect connections diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 4ee9fee4a9..4567539566 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -142,9 +142,10 @@ module ActiveRecord # this should be overridden by concrete adapters end - # Returns true if its safe to reload the connection between requests for development mode. + # Returns true if its required to reload the connection between requests for development mode. + # This is not the case for Ruby/MySQL and it's not necessary for any adapters except SQLite. def requires_reloading? - true + false end # Checks whether the connection to the database is still active (i.e. not stale). -- cgit v1.2.3 From e639536ea80e94f5d72493267c8aec21d305cf74 Mon Sep 17 00:00:00 2001 From: Jeff Dean Date: Wed, 9 Jun 2010 00:13:24 -0400 Subject: remove_column should raise an ArgumentError when no columns are passed [#4803 state:resolved] Signed-off-by: Michael Koziarski --- .../lib/active_record/connection_adapters/abstract/schema_statements.rb | 1 + activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb | 1 + 2 files changed, 2 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index d3499cea72..638e5d7236 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -205,6 +205,7 @@ module ActiveRecord # remove_column(:suppliers, :qualification) # remove_columns(:suppliers, :qualification, :experience) def remove_column(table_name, *column_names) + raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_names.empty? column_names.flatten.each do |column_name| execute "ALTER TABLE #{quote_table_name(table_name)} DROP #{quote_column_name(column_name)}" end diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index deb62e3802..ad6314c530 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -246,6 +246,7 @@ module ActiveRecord end def remove_column(table_name, *column_names) #:nodoc: + raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_names.empty? column_names.flatten.each do |column_name| alter_table(table_name) do |definition| definition.columns.delete(definition[column_name]) -- cgit v1.2.3 From d132dd33520ba61f7bfa9ba6fdd1b7b2bebd27f3 Mon Sep 17 00:00:00 2001 From: Paul Gillard Date: Fri, 18 Jun 2010 00:02:31 +0100 Subject: Don't clone associations [#4894 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cloning an active record object should be shallow in that it should copy attributes but not associations. This was no longer true as a result of #3164. Signed-off-by: José Valim --- activerecord/lib/active_record/base.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 3f81ca7555..3a7db97a6a 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1433,6 +1433,7 @@ module ActiveRecord #:nodoc: end clear_aggregation_cache + clear_association_cache @attributes_cache = {} @new_record = true ensure_proper_type -- cgit v1.2.3 From 7008911222826eef07a338bf4cab27b83fe90ce1 Mon Sep 17 00:00:00 2001 From: "Mohammed Siddick.E" Date: Wed, 23 Jun 2010 12:33:34 +0530 Subject: Patch for Namespace problem in Scaffold. [#4763 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../lib/rails/generators/active_record/model/model_generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index 539c2517ee..960c29c49c 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -22,7 +22,7 @@ module ActiveRecord def create_module_file return if class_path.empty? - template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") + template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke end hook_for :test_framework -- cgit v1.2.3 From eb04408a20628a49296e0859425940b39a83ec63 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 23 Jun 2010 10:05:30 -0400 Subject: ActiveRecord's relation object should respond to to_json and to_yaml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#4547 state:resolved] Signed-off-by: José Valim --- activerecord/lib/active_record/relation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 66970a5ea1..a342b1fe6f 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -10,7 +10,7 @@ module ActiveRecord include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches - delegate :length, :collect, :map, :each, :all?, :include?, :to => :to_a + delegate :to_json, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to => :to_a delegate :insert, :to => :arel attr_reader :table, :klass -- cgit v1.2.3 From 0bf3baa6b3d216c6340f8d3b5d0a3ebc093e969a Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 23 Jun 2010 11:54:38 -0400 Subject: adding fix for to_xml for ActiveRecord relation object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/relation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index a342b1fe6f..a51b317dc3 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -10,7 +10,7 @@ module ActiveRecord include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches - delegate :to_json, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to => :to_a + delegate :to_xml, :to_json, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to => :to_a delegate :insert, :to => :arel attr_reader :table, :klass -- cgit v1.2.3 From 49f52c3d910c8f183afc3a54ea2ae9667f23085e Mon Sep 17 00:00:00 2001 From: Michael Lovitt Date: Tue, 22 Jun 2010 09:55:50 -0400 Subject: Sessions should not be created until written to and session data should be destroyed on reset. [#4938] Signed-off-by: Jeremy Kemper --- activerecord/lib/active_record/session_store.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/session_store.rb b/activerecord/lib/active_record/session_store.rb index f712a2c94f..b88d550086 100644 --- a/activerecord/lib/active_record/session_store.rb +++ b/activerecord/lib/active_record/session_store.rb @@ -318,6 +318,14 @@ module ActiveRecord sid end + def destroy(env) + if sid = current_session_id(env) + Base.silence do + get_session_model(env, sid).destroy + end + end + end + def get_session_model(env, sid) if env[ENV_SESSION_OPTIONS_KEY][:id].nil? env[SESSION_RECORD_KEY] = find_session(sid) -- cgit v1.2.3 From 4a0a640d33e1c729d38c6091bb1394fbda059b5c Mon Sep 17 00:00:00 2001 From: Paul Gillard Date: Wed, 23 Jun 2010 21:36:02 +0100 Subject: Remove incorrect comment regarding #initialize_copy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comment added in #3164 is incorrect after reading http://dev.rubyonrails.org/ticket/7191. Signed-off-by: José Valim --- activerecord/lib/active_record/base.rb | 8 -------- 1 file changed, 8 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 3a7db97a6a..7a262ad465 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1413,14 +1413,6 @@ module ActiveRecord #:nodoc: # as it copies the object's attributes only, not its associations. The extent of a "deep" clone is # application specific and is therefore left to the application to implement according to its need. def initialize_copy(other) - # Think the assertion which fails if the after_initialize callback goes at the end of the method is wrong. The - # deleted clone method called new which therefore called the after_initialize callback. It then went on to copy - # over the attributes. But if it's copying the attributes afterwards then it hasn't finished initializing right? - # For example in the test suite the topic model's after_initialize method sets the author_email_address to - # test@test.com. I would have thought this would mean that all cloned models would have an author email address - # of test@test.com. However the test_clone test method seems to test that this is not the case. As a result the - # after_initialize callback has to be run *before* the copying of the attributes rather than afterwards in order - # for all tests to pass. This makes no sense to me. callback(:after_initialize) if respond_to_without_attributes?(:after_initialize) cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast) cloned_attributes.delete(self.class.primary_key) -- cgit v1.2.3 From 6788db824ab732b13493a9d702dd8fb89fa153c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 24 Jun 2010 13:23:43 +0200 Subject: Move Rails::LogSubscriber to ActiveSupport::LogSubscriber, allowing frameworks like ActiveRecord and ActiveResource to log outsude Rails::Application [#4816 state:resolved] --- activerecord/lib/active_record/base.rb | 1 + activerecord/lib/active_record/log_subscriber.rb | 32 ++++++++++++++++++++++ activerecord/lib/active_record/railtie.rb | 3 -- .../lib/active_record/railties/log_subscriber.rb | 32 ---------------------- 4 files changed, 33 insertions(+), 35 deletions(-) create mode 100644 activerecord/lib/active_record/log_subscriber.rb delete mode 100644 activerecord/lib/active_record/railties/log_subscriber.rb (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 7a262ad465..16cf501bd5 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -20,6 +20,7 @@ require 'active_support/core_ext/object/duplicable' require 'active_support/core_ext/object/blank' require 'arel' require 'active_record/errors' +require 'active_record/log_subscriber' module ActiveRecord #:nodoc: # = Active Record diff --git a/activerecord/lib/active_record/log_subscriber.rb b/activerecord/lib/active_record/log_subscriber.rb new file mode 100644 index 0000000000..71065f9908 --- /dev/null +++ b/activerecord/lib/active_record/log_subscriber.rb @@ -0,0 +1,32 @@ +module ActiveRecord + class LogSubscriber < ActiveSupport::LogSubscriber + def initialize + super + @odd_or_even = false + end + + def sql(event) + name = '%s (%.1fms)' % [event.payload[:name], event.duration] + sql = event.payload[:sql].squeeze(' ') + + if odd? + name = color(name, :cyan, true) + sql = color(sql, nil, true) + else + name = color(name, :magenta, true) + end + + debug " #{name} #{sql}" + end + + def odd? + @odd_or_even = !@odd_or_even + end + + def logger + ActiveRecord::Base.logger + end + end +end + +ActiveRecord::LogSubscriber.attach_to :active_record \ No newline at end of file diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 36df878e1b..2808e199fe 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -26,9 +26,6 @@ module ActiveRecord load "active_record/railties/databases.rake" end - require "active_record/railties/log_subscriber" - log_subscriber :active_record, ActiveRecord::Railties::LogSubscriber.new - initializer "active_record.initialize_timezone" do ActiveSupport.on_load(:active_record) do self.time_zone_aware_attributes = true diff --git a/activerecord/lib/active_record/railties/log_subscriber.rb b/activerecord/lib/active_record/railties/log_subscriber.rb deleted file mode 100644 index 31b98bb6ed..0000000000 --- a/activerecord/lib/active_record/railties/log_subscriber.rb +++ /dev/null @@ -1,32 +0,0 @@ -module ActiveRecord - module Railties - class LogSubscriber < Rails::LogSubscriber - def initialize - super - @odd_or_even = false - end - - def sql(event) - name = '%s (%.1fms)' % [event.payload[:name], event.duration] - sql = event.payload[:sql].squeeze(' ') - - if odd? - name = color(name, :cyan, true) - sql = color(sql, nil, true) - else - name = color(name, :magenta, true) - end - - debug " #{name} #{sql}" - end - - def odd? - @odd_or_even = !@odd_or_even - end - - def logger - ActiveRecord::Base.logger - end - end - end -end -- cgit v1.2.3 From fdb7f84eb10c5e59490764a1a259aa00a1fcfe5f Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Thu, 24 Jun 2010 11:17:05 -0300 Subject: Remove deprecated block usage in composed_of. --- activerecord/lib/active_record/aggregations.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/aggregations.rb b/activerecord/lib/active_record/aggregations.rb index 51ffc7542c..c45400d3d9 100644 --- a/activerecord/lib/active_record/aggregations.rb +++ b/activerecord/lib/active_record/aggregations.rb @@ -190,7 +190,7 @@ module ActiveRecord # :constructor => Proc.new { |ip| IPAddr.new(ip, Socket::AF_INET) }, # :converter => Proc.new { |ip| ip.is_a?(Integer) ? IPAddr.new(ip, Socket::AF_INET) : IPAddr.new(ip.to_s) } # - def composed_of(part_id, options = {}, &block) + def composed_of(part_id, options = {}) options.assert_valid_keys(:class_name, :mapping, :allow_nil, :constructor, :converter) name = part_id.id2name @@ -199,9 +199,7 @@ module ActiveRecord mapping = [ mapping ] unless mapping.first.is_a?(Array) allow_nil = options[:allow_nil] || false constructor = options[:constructor] || :new - converter = options[:converter] || block - - ActiveSupport::Deprecation.warn('The conversion block has been deprecated, use the :converter option instead.', caller) if block_given? + converter = options[:converter] reader_method(name, class_name, mapping, allow_nil, constructor) writer_method(name, class_name, mapping, allow_nil, converter) -- cgit v1.2.3 From d9f199e1238a723432a2005c405fc5ae22dea24b Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Thu, 24 Jun 2010 11:40:23 -0300 Subject: Don't define block, just yield if block is given. --- .../active_record/associations/association_collection.rb | 14 +++++++------- 1 file changed, 7 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 f8d46bcb48..186b531ffb 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -26,10 +26,10 @@ module ActiveRecord delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :includes, :from, :lock, :readonly, :having, :to => :scoped - def select(select = nil, &block) + def select(select = nil) if block_given? load_target - @target.select(&block) + @target.select.each { |e| yield e } else scoped.select(select) end @@ -123,7 +123,7 @@ module ActiveRecord end end - # Add +records+ to this association. Returns +self+ so method calls may be chained. + # Add +records+ to this association. Returns +self+ so method calls may be chained. # Since << flattens its argument list and inserts each record, +push+ and +concat+ behave identically. def <<(*records) result = true @@ -168,7 +168,7 @@ module ActiveRecord reset_target! reset_named_scopes_cache! end - + # Calculate sum using SQL, not Enumerable def sum(*args) if block_given? @@ -241,7 +241,7 @@ module ActiveRecord if @reflection.options[:dependent] && @reflection.options[:dependent] == :destroy destroy_all - else + else delete_all end @@ -520,8 +520,8 @@ module ActiveRecord def callbacks_for(callback_name) full_callback_name = "#{callback_name}_for_#{@reflection.name}" @owner.class.read_inheritable_attribute(full_callback_name.to_sym) || [] - end - + end + def ensure_owner_is_not_new if @owner.new_record? raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved" -- cgit v1.2.3 From 4086ecea24446904bac4c69812f219ce7cbfbbba Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Thu, 24 Jun 2010 12:02:00 -0300 Subject: Remove block definition from method, is not needed since yield is used inside. --- activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 2 +- activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 2 +- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 4567539566..be8d1bd76b 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -107,7 +107,7 @@ module ActiveRecord # REFERENTIAL INTEGRITY ==================================== # Override to turn off referential integrity while executing &block. - def disable_referential_integrity(&block) + def disable_referential_integrity yield end diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 7c7bc5e292..aa3626a37e 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -219,7 +219,7 @@ module ActiveRecord # REFERENTIAL INTEGRITY ==================================== - def disable_referential_integrity(&block) #:nodoc: + def disable_referential_integrity #:nodoc: old = select_value("SELECT @@FOREIGN_KEY_CHECKS") begin diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index e84242601b..6fa4c50d6a 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -372,7 +372,7 @@ module ActiveRecord return false end - def disable_referential_integrity(&block) #:nodoc: + def disable_referential_integrity #:nodoc: if supports_disable_referential_integrity?() then execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";")) end -- cgit v1.2.3 From 2c203a94136d5b8681e2b2b55783ef6dde54405f Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Thu, 24 Jun 2010 13:07:39 -0300 Subject: Remove block definitions in finder methods. --- activerecord/lib/active_record/relation/finder_methods.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (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 7a0c9dc612..f39951e16c 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -87,8 +87,8 @@ module ActiveRecord # person.visits += 1 # person.save! # end - def find(*args, &block) - return to_a.find(&block) if block_given? + def find(*args) + return to_a.find { |*block_args| yield(*block_args) } if block_given? options = args.extract_options! @@ -259,8 +259,8 @@ module ActiveRecord record end - def find_with_ids(*ids, &block) - return to_a.find(&block) if block_given? + def find_with_ids(*ids) + return to_a.find { |*block_args| yield(*block_args) } if block_given? expects_array = ids.first.kind_of?(Array) return ids.first if expects_array && ids.first.empty? -- cgit v1.2.3 From 4b5f417e63e4cdd7fd6837c10ebaa9dd4860f55e Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Thu, 24 Jun 2010 13:17:24 -0300 Subject: Only yield block if given. --- activerecord/lib/active_record/relation/query_methods.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 43032ba9d8..e8d0f215f6 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -22,9 +22,9 @@ module ActiveRecord end class_eval <<-CEVAL, __FILE__, __LINE__ + 1 - def select(*args, &block) + def select(*args) if block_given? - to_a.select(&block) + to_a.select { |*block_args| yield(*block_args) } else new_relation = clone value = Array.wrap(args.flatten).reject {|x| x.blank? } -- cgit v1.2.3 From 497a0c3b00cc225215a8744bbf2a7e04c7297b7c Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 23 Jun 2010 21:12:51 -0300 Subject: quotes are not necessary here MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 16cf501bd5..e7b52287a5 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -917,8 +917,8 @@ module ActiveRecord #:nodoc: def instantiate(record) object = find_sti_class(record[inheritance_column]).allocate - object.instance_variable_set(:'@attributes', record) - object.instance_variable_set(:'@attributes_cache', {}) + object.instance_variable_set(:@attributes, record) + object.instance_variable_set(:@attributes_cache, {}) object.instance_variable_set(:@new_record, false) object.instance_variable_set(:@readonly, false) object.instance_variable_set(:@destroyed, false) -- cgit v1.2.3 From 7a7c608a26f03abb1245ff83d4e25040ad09cb44 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 23 Jun 2010 21:44:27 -0300 Subject: Your original TIME ZONE value on PostgreSQL is correctly restored now, after going through options :utc and then going back to :local MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#4950 state:committed] Signed-off-by: José Valim --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index e84242601b..851e6d3718 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -216,7 +216,10 @@ module ActiveRecord super(connection, logger) @connection_parameters, @config = connection_parameters, config + # @local_tz is initialized as nil to avoid warnings when connect tries to use it + @local_tz = nil connect + @local_tz = execute('SHOW TIME ZONE').first["TimeZone"] end # Is this connection alive and ready for queries? @@ -929,9 +932,8 @@ module ActiveRecord # TIMESTAMP WITH ZONE types in 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}'") + elsif @local_tz + execute("SET time zone '#{@local_tz}'") end end -- cgit v1.2.3 From 518b16d9aedf4534b11aab8043d4efd0361fa1fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Strza=C5=82kowski?= Date: Fri, 25 Jun 2010 10:51:43 +0200 Subject: Line break in migration template and nicer code indentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../active_record/migration/templates/migration.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb index d6ab3257a0..edc6a18d5d 100644 --- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb @@ -1,15 +1,17 @@ class <%= migration_class_name %> < ActiveRecord::Migration - def self.up<% attributes.each do |attribute| %> - <%- if migration_action -%> + def self.up +<% attributes.each do |attribute| %> + <%- if migration_action -%> <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end %> - <%- end -%> <%- end -%> +<%- end -%> end - def self.down<% attributes.reverse.each do |attribute| %> - <%- if migration_action -%> + def self.down +<% attributes.reverse.each do |attribute| %> + <%- if migration_action -%> <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end %> - <%- end -%> <%- end -%> +<%- end -%> end end -- cgit v1.2.3 From 3344c011da17cb59f4452101c8e65067d7d30cce Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 25 Jun 2010 13:07:17 -0300 Subject: Avoid a blank line before the add/remove columns --- .../rails/generators/active_record/migration/templates/migration.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb index edc6a18d5d..8ac21c1410 100644 --- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb @@ -1,6 +1,6 @@ class <%= migration_class_name %> < ActiveRecord::Migration def self.up -<% attributes.each do |attribute| %> +<% attributes.each do |attribute| -%> <%- if migration_action -%> <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end %> <%- end -%> @@ -8,7 +8,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration end def self.down -<% attributes.reverse.each do |attribute| %> +<% attributes.reverse.each do |attribute| -%> <%- if migration_action -%> <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end %> <%- end -%> -- cgit v1.2.3 From 82dc16b7d2b6313117a42f7bf69a097b5ab79d1f Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 24 Jun 2010 20:34:01 -0300 Subject: Metaprogramming not needed here --- .../lib/active_record/relation/query_methods.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index e8d0f215f6..adc56fbef0 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -21,18 +21,16 @@ module ActiveRecord CEVAL end - class_eval <<-CEVAL, __FILE__, __LINE__ + 1 - def select(*args) - if block_given? - to_a.select { |*block_args| yield(*block_args) } - else - new_relation = clone - value = Array.wrap(args.flatten).reject {|x| x.blank? } - new_relation.select_values += value if value.present? - new_relation - end + def select(*args) + if block_given? + to_a.select { |*block_args| yield(*block_args) } + else + new_relation = clone + value = Array.wrap(args.flatten).reject {|x| x.blank? } + new_relation.select_values += value if value.present? + new_relation end - CEVAL + end [:where, :having].each do |query_method| class_eval <<-CEVAL, __FILE__, __LINE__ + 1 -- cgit v1.2.3 From 3d8ccb924084ecd341b2d9644e6e0b66903d8432 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 24 Jun 2010 20:36:01 -0300 Subject: Makes a build_select for the select part of build_arel --- .../lib/active_record/relation/query_methods.rb | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index adc56fbef0..1e570a569b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -137,16 +137,7 @@ module ActiveRecord arel = arel.order(*@order_values.uniq.select{|o| o.present?}) if @order_values.present? - selects = @select_values.uniq - - if selects.present? - selects.each do |s| - @implicit_readonly = false - arel = arel.project(s) if s.present? - end - else - arel = arel.project(@klass.quoted_table_name + '.*') - end + arel = build_select(arel, @select_values.uniq) arel = arel.from(@from_value) if @from_value.present? @@ -219,6 +210,18 @@ module ActiveRecord relation.join(custom_joins) end + def build_select(arel, selects) + if selects.present? + @implicit_readonly = false + selects.each do |s| + arel = arel.project(s) if s.present? + end + else + arel = arel.project(@klass.quoted_table_name + '.*') + end + arel + end + def apply_modules(modules) values = Array.wrap(modules) @extensions += values if values.present? -- cgit v1.2.3 From 0ebb5bf6590b8ac62c53538ade7095676baec3d4 Mon Sep 17 00:00:00 2001 From: Neeraj Singh and Santiago Pastorino Date: Thu, 24 Jun 2010 22:52:15 -0300 Subject: Support for multiple selects added [#4841 state:committed] --- activerecord/lib/active_record/relation/query_methods.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 1e570a569b..4dbb30c777 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -213,13 +213,16 @@ module ActiveRecord def build_select(arel, selects) if selects.present? @implicit_readonly = false - selects.each do |s| - arel = arel.project(s) if s.present? + # TODO: fix this ugly hack, we should refactor the callers to get an ARel compatible array. + # Before this change we were passing to ARel the last element only, and ARel is capable of handling an array + if selects.all? { |s| s.is_a?(String) || !s.is_a?(Arel::Expression) } && !(selects.last =~ /^COUNT\(/) + arel.project(*selects) + else + arel.project(selects.last) end else - arel = arel.project(@klass.quoted_table_name + '.*') + arel.project(@klass.quoted_table_name + '.*') end - arel end def apply_modules(modules) -- cgit v1.2.3 From 65aa6a7db16b7387d5a3bf1ad7f5a602804a2f21 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 25 Jun 2010 19:34:51 -0300 Subject: reorder method added to ActiveRelation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#4972 state:committed] Signed-off-by: José Valim --- activerecord/lib/active_record/relation/query_methods.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 4dbb30c777..f7c4c7991b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -21,6 +21,14 @@ module ActiveRecord CEVAL end + def reorder(*args, &block) + new_relation = clone + new_relation.send(:apply_modules, Module.new(&block)) if block_given? + value = Array.wrap(args.flatten).reject {|x| x.blank? } + new_relation.order_values = value if value.present? + new_relation + end + def select(*args) if block_given? to_a.select { |*block_args| yield(*block_args) } -- cgit v1.2.3 From 4464d10e68045e6723d4eb62734213c4296ef339 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 25 Jun 2010 15:49:15 -0700 Subject: index dump should not include full text indexes. Thanks Ken Mayer for the original patch! [#4949 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../active_record/connection_adapters/postgresql_adapter.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index ef58a32074..26d88158d3 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -609,9 +609,7 @@ module ActiveRecord SQL - indexes = [] - - indexes = result.map do |row| + result.map do |row| index_name = row[0] unique = row[1] == 't' indkey = row[2].split(" ") @@ -625,11 +623,8 @@ module ActiveRecord SQL column_names = indkey.map {|attnum| columns[attnum] } - IndexDefinition.new(table_name, index_name, unique, column_names) - - end - - indexes + column_names.compact.empty? ? nil : IndexDefinition.new(table_name, index_name, unique, column_names) + end.compact end # Returns the list of all column definitions for a table. -- cgit v1.2.3 From 11ff3da5f4fe71a8d93180ab9fa69c6190a2e26e Mon Sep 17 00:00:00 2001 From: Andrew White Date: Thu, 18 Mar 2010 16:49:23 +0000 Subject: Add column and index query methods to ActiveRecord::Schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#4219 state:resolved] Signed-off-by: José Valim --- .../abstract/schema_definitions.rb | 10 ++++ .../abstract/schema_statements.rb | 57 ++++++++++++++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 7d58bc2adf..7691b6a788 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -582,6 +582,11 @@ module ActiveRecord @base.add_column(@table_name, column_name, type, options) end + # Checks to see if a column exists. See SchemaStatements#column_exists? + def column_exists?(column_name, type = nil, options = nil) + @base.column_exists?(@table_name, column_name, type, options) + end + # Adds a new index to the table. +column_name+ can be a single Symbol, or # an Array of Symbols. See SchemaStatements#add_index # @@ -596,6 +601,11 @@ module ActiveRecord @base.add_index(@table_name, column_name, options) end + # Checks to see if an index exists. See SchemaStatements#index_exists? + def index_exists?(column_name, options = {}) + @base.index_exists?(@table_name, column_name, options) + end + # Adds timestamps (created_at and updated_at) columns to the table. See SchemaStatements#add_timestamps # ===== Example # t.timestamps diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 638e5d7236..5625dba45f 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -24,10 +24,59 @@ module ActiveRecord # Returns an array of indexes for the given table. # def indexes(table_name, name = nil) end + # Checks to see if an index exists on a table for a given index definition + # + # === Examples + # # Check an index exists + # index_exists?(:suppliers, :company_id) + # + # # Check an index on multiple columns exists + # index_exists?(:suppliers, [:company_id, :company_type]) + # + # # Check a unique index exists + # index_exists?(:suppliers, :company_id, :unique => true) + # + # # Check an index with a custom name exists + # index_exists?(:suppliers, :company_id, :name => "idx_company_id" + def index_exists?(table_name, column_name, options = {}) + column_names = Array.wrap(column_name) + index_name = options.key?(:name) ? options[:name].to_s : index_name(table_name, :column => column_names) + if options[:unique] + indexes(table_name).any?{ |i| i.unique && i.name == index_name } + else + indexes(table_name).any?{ |i| i.name == index_name } + end + end + # Returns an array of Column objects for the table specified by +table_name+. # See the concrete implementation for details on the expected parameter values. def columns(table_name, name = nil) end + # Checks to see if a column exists in a given table. + # + # === Examples + # # Check a column exists + # column_exists?(:suppliers, :name) + # + # # Check a column exists of a particular type + # column_exists?(:suppliers, :name, :string) + # + # # Check a column exists with a specific definition + # column_exists?(:suppliers, :name, :string, :limit => 100) + def column_exists?(table_name, column_name, type = nil, options = nil) + column_name = column_name.to_s + if type + if options + sql_type = type_to_sql(type, options[:limit], options[:precision], options[:scale]) + columns(table_name).any?{ |c| c.name == column_name && c.sql_type == sql_type } + else + columns(table_name).any?{ |c| c.name == column_name && c.type == type } + end + else + columns(table_name).any?{ |c| c.name == column_name } + end + end + # Creates a new table with the name +table_name+. +table_name+ may either # be a String or a Symbol. # @@ -293,7 +342,7 @@ module ActiveRecord @logger.warn("Index name '#{index_name}' on table '#{table_name}' is too long; the limit is #{index_name_length} characters. Skipping.") return end - if index_exists?(table_name, index_name, false) + if index_name_exists?(table_name, index_name, false) @logger.warn("Index name '#{index_name}' on table '#{table_name}' already exists. Skipping.") return end @@ -314,7 +363,7 @@ module ActiveRecord # remove_index :accounts, :name => :by_branch_party def remove_index(table_name, options = {}) index_name = index_name(table_name, options) - unless index_exists?(table_name, index_name, true) + unless index_name_exists?(table_name, index_name, true) @logger.warn("Index name '#{index_name}' on table '#{table_name}' does not exist. Skipping.") return end @@ -351,11 +400,11 @@ module ActiveRecord end end - # Verify the existence of an index. + # Verify the existence of an index with a given name. # # The default argument is returned if the underlying implementation does not define the indexes method, # as there's no way to determine the correct answer in that case. - def index_exists?(table_name, index_name, default) + def index_name_exists?(table_name, index_name, default) return default unless respond_to?(:indexes) indexes(table_name).detect { |i| i.name == index_name } end -- cgit v1.2.3 From 47134a04bbce216f9c31473b1a0c1a077d624692 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 25 Jun 2010 20:31:10 -0300 Subject: blocks removed from all the ActiveRelation query_methods, extend method added instead MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/relation.rb | 9 +++++++++ activerecord/lib/active_record/relation/query_methods.rb | 12 ++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index a51b317dc3..fd0660a138 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -328,6 +328,15 @@ module ActiveRecord to_a.inspect end + def extend(*args, &block) + if block_given? + apply_modules Module.new(&block) + self + else + super + end + end + protected def method_missing(method, *args, &block) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index f7c4c7991b..ff5bce9d8b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -11,9 +11,8 @@ module ActiveRecord next if [:where, :having, :select].include?(query_method) class_eval <<-CEVAL, __FILE__, __LINE__ + 1 - def #{query_method}(*args, &block) + def #{query_method}(*args) new_relation = clone - new_relation.send(:apply_modules, Module.new(&block)) if block_given? value = Array.wrap(args.flatten).reject {|x| x.blank? } new_relation.#{query_method}_values += value if value.present? new_relation @@ -21,9 +20,8 @@ module ActiveRecord CEVAL end - def reorder(*args, &block) + def reorder(*args) new_relation = clone - new_relation.send(:apply_modules, Module.new(&block)) if block_given? value = Array.wrap(args.flatten).reject {|x| x.blank? } new_relation.order_values = value if value.present? new_relation @@ -42,9 +40,8 @@ module ActiveRecord [:where, :having].each do |query_method| class_eval <<-CEVAL, __FILE__, __LINE__ + 1 - def #{query_method}(*args, &block) + def #{query_method}(*args) new_relation = clone - new_relation.send(:apply_modules, Module.new(&block)) if block_given? value = build_where(*args) new_relation.#{query_method}_values += Array.wrap(value) if value.present? new_relation @@ -56,9 +53,8 @@ module ActiveRecord attr_accessor :"#{query_method}_value" class_eval <<-CEVAL, __FILE__, __LINE__ + 1 - def #{query_method}(value = true, &block) + def #{query_method}(value = true) new_relation = clone - new_relation.send(:apply_modules, Module.new(&block)) if block_given? new_relation.#{query_method}_value = value new_relation end -- cgit v1.2.3 From 6e655732226eef6fa04d8fc0c4ee1f0436688c49 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 25 Jun 2010 16:32:09 -0700 Subject: refactoring the postgres adapter index method to avoid inject and use values_at. [#4976 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 26d88158d3..2fe2ae7136 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -615,15 +615,15 @@ module ActiveRecord indkey = row[2].split(" ") oid = row[3] - columns = query(<<-SQL, "Columns for index #{row[0]} on #{table_name}").inject({}) {|attlist, r| attlist[r[1]] = r[0]; attlist} - SELECT a.attname, a.attnum + columns = Hash[query(<<-SQL, "Columns for index #{row[0]} on #{table_name}")] + SELECT a.attnum, a.attname FROM pg_attribute a WHERE a.attrelid = #{oid} AND a.attnum IN (#{indkey.join(",")}) SQL - column_names = indkey.map {|attnum| columns[attnum] } - column_names.compact.empty? ? nil : IndexDefinition.new(table_name, index_name, unique, column_names) + column_names = columns.values_at(*indkey).compact + column_names.empty? ? nil : IndexDefinition.new(table_name, index_name, unique, column_names) end.compact end -- cgit v1.2.3 From 5b91c97763f1183f131e46e51ed33129c6b1edcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 26 Jun 2010 22:37:12 +0200 Subject: Create a little bit less objects in ARel. --- activerecord/lib/active_record/relation/query_methods.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index ff5bce9d8b..caa55929f4 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -12,18 +12,20 @@ module ActiveRecord next if [:where, :having, :select].include?(query_method) class_eval <<-CEVAL, __FILE__, __LINE__ + 1 def #{query_method}(*args) + args.flatten! + args.reject! { |a| a.blank? } new_relation = clone - value = Array.wrap(args.flatten).reject {|x| x.blank? } - new_relation.#{query_method}_values += value if value.present? + new_relation.#{query_method}_values += args if args.present? new_relation end CEVAL end def reorder(*args) + args.flatten! + args.reject! { |a| a.blank? } new_relation = clone - value = Array.wrap(args.flatten).reject {|x| x.blank? } - new_relation.order_values = value if value.present? + new_relation.order_values = args if args.present? new_relation end @@ -31,9 +33,10 @@ module ActiveRecord if block_given? to_a.select { |*block_args| yield(*block_args) } else + args.flatten! + args.reject! { |a| a.blank? } new_relation = clone - value = Array.wrap(args.flatten).reject {|x| x.blank? } - new_relation.select_values += value if value.present? + new_relation.select_values += args if args.present? new_relation end end -- cgit v1.2.3 From 100d2282e306f68d8ce0324128da1506eafc43a2 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 26 Jun 2010 13:34:50 -0700 Subject: adding adapter tests, avoiding private apis, fixing code in 1.9 [#4986 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../lib/active_record/connection_adapters/sqlite3_adapter.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index f295af16f0..0d9a86a1ea 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -6,6 +6,10 @@ module ActiveRecord def self.sqlite3_connection(config) # :nodoc: parse_sqlite_config!(config) + unless 'sqlite3' == config[:adapter] + raise ArgumentError, 'adapter name should be "sqlite3"' + end + unless self.class.const_defined?(:SQLite3) require_library_or_gem(config[:adapter]) end @@ -24,13 +28,13 @@ module ActiveRecord module ConnectionAdapters #:nodoc: class SQLite3Adapter < SQLiteAdapter # :nodoc: - + # Returns the current database encoding format as a string, eg: 'UTF-8' def encoding if @connection.respond_to?(:encoding) - @connection.encoding[0]['encoding'] + @connection.encoding.to_s else - encoding = @connection.send(:get_query_pragma, 'encoding') + encoding = @connection.execute('PRAGMA encoding') encoding[0]['encoding'] end end -- cgit v1.2.3 From a39f2657b1ae8d61c6018766f8d250b1401beace Mon Sep 17 00:00:00 2001 From: Evgeniy Dolzhenko Date: Thu, 27 May 2010 03:22:45 -0700 Subject: Add module_eval missing file_name and line_number args [#4712 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/autosave_association.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index c378e19864..7517896235 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -147,12 +147,12 @@ module ActiveRecord # add_autosave_association_callbacks(reflect_on_association(name)) # end ASSOCIATION_TYPES.each do |type| - module_eval %{ + module_eval <<-CODE, __FILE__, __LINE__ + 1 def #{type}(name, options = {}) super add_autosave_association_callbacks(reflect_on_association(name)) end - } + CODE end # Adds a validate and save callback for the association as specified by -- cgit v1.2.3 From 64fee27a554151eb87b2350c3834bbf8812520c8 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 26 Jun 2010 17:43:40 -0300 Subject: Removes useless flatten --- activerecord/lib/active_record/relation/query_methods.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index caa55929f4..201a4799a4 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -9,10 +9,9 @@ module ActiveRecord (ActiveRecord::Relation::ASSOCIATION_METHODS + ActiveRecord::Relation::MULTI_VALUE_METHODS).each do |query_method| attr_accessor :"#{query_method}_values" - next if [:where, :having, :select].include?(query_method) + next if [:where, :having, :select, :joins].include?(query_method) class_eval <<-CEVAL, __FILE__, __LINE__ + 1 def #{query_method}(*args) - args.flatten! args.reject! { |a| a.blank? } new_relation = clone new_relation.#{query_method}_values += args if args.present? @@ -22,7 +21,6 @@ module ActiveRecord end def reorder(*args) - args.flatten! args.reject! { |a| a.blank? } new_relation = clone new_relation.order_values = args if args.present? @@ -33,7 +31,6 @@ module ActiveRecord if block_given? to_a.select { |*block_args| yield(*block_args) } else - args.flatten! args.reject! { |a| a.blank? } new_relation = clone new_relation.select_values += args if args.present? @@ -41,6 +38,14 @@ module ActiveRecord end end + def joins(*args) + args.flatten! + args.reject! { |a| a.blank? } + new_relation = clone + new_relation.joins_values += args if args.present? + new_relation + end + [:where, :having].each do |query_method| class_eval <<-CEVAL, __FILE__, __LINE__ + 1 def #{query_method}(*args) -- cgit v1.2.3 From 8f358f397f11a87c649f76644690461f0017cbff Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 26 Jun 2010 20:12:23 -0300 Subject: Refactor of column_exists? method and this works with PostgreSQL --- .../connection_adapters/abstract/schema_statements.rb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 5625dba45f..0216a8f4ac 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -63,18 +63,12 @@ module ActiveRecord # # # Check a column exists with a specific definition # column_exists?(:suppliers, :name, :string, :limit => 100) - def column_exists?(table_name, column_name, type = nil, options = nil) - column_name = column_name.to_s - if type - if options - sql_type = type_to_sql(type, options[:limit], options[:precision], options[:scale]) - columns(table_name).any?{ |c| c.name == column_name && c.sql_type == sql_type } - else - columns(table_name).any?{ |c| c.name == column_name && c.type == type } - end - else - columns(table_name).any?{ |c| c.name == column_name } - end + def column_exists?(table_name, column_name, type = nil, options = {}) + columns(table_name).any?{ |c| c.name == column_name.to_s && + (!type || c.type == type) && + (!options[:limit] || c.limit == options[:limit]) && + (!options[:precision] || c.precision == options[:precision]) && + (!options[:scale] || c.scale == options[:scale]) } end # Creates a new table with the name +table_name+. +table_name+ may either -- cgit v1.2.3 From 8d9545389f6a53dcd1ebae28c8b966c296da95f2 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 26 Jun 2010 23:30:43 -0300 Subject: Refactor: metaprogramming here it's confusing and make use of tap Signed-off-by: Jeremy Kemper --- .../lib/active_record/relation/query_methods.rb | 137 +++++++++++---------- 1 file changed, 73 insertions(+), 64 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 201a4799a4..015ca8c24c 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -5,89 +5,98 @@ module ActiveRecord module QueryMethods extend ActiveSupport::Concern - included do - (ActiveRecord::Relation::ASSOCIATION_METHODS + ActiveRecord::Relation::MULTI_VALUE_METHODS).each do |query_method| - attr_accessor :"#{query_method}_values" - - next if [:where, :having, :select, :joins].include?(query_method) - class_eval <<-CEVAL, __FILE__, __LINE__ + 1 - def #{query_method}(*args) - args.reject! { |a| a.blank? } - new_relation = clone - new_relation.#{query_method}_values += args if args.present? - new_relation - end - CEVAL - end + attr_accessor :includes_values, :eager_load_values, :preload_values, + :select_values, :group_values, :order_values, :joins_values, :where_values, :having_values, + :limit_value, :offset_value, :lock_value, :readonly_value, :create_with_value, :from_value - def reorder(*args) - args.reject! { |a| a.blank? } - new_relation = clone - new_relation.order_values = args if args.present? - new_relation - end + def includes(*args) + args.reject! { |a| a.blank? } + clone.tap { |r| r.includes_values += args if args.present? } + end - def select(*args) - if block_given? - to_a.select { |*block_args| yield(*block_args) } - else - args.reject! { |a| a.blank? } - new_relation = clone - new_relation.select_values += args if args.present? - new_relation - end - end + def eager_load(*args) + args.reject! { |a| a.blank? } + clone.tap { |r| r.eager_load_values += args if args.present? } + end - def joins(*args) - args.flatten! + def preload(*args) + args.reject! { |a| a.blank? } + clone.tap { |r| r.preload_values += args if args.present? } + end + + def select(*args) + if block_given? + to_a.select { |*block_args| yield(*block_args) } + else args.reject! { |a| a.blank? } - new_relation = clone - new_relation.joins_values += args if args.present? - new_relation + clone.tap { |r| r.select_values += args if args.present? } end + end - [:where, :having].each do |query_method| - class_eval <<-CEVAL, __FILE__, __LINE__ + 1 - def #{query_method}(*args) - new_relation = clone - value = build_where(*args) - new_relation.#{query_method}_values += Array.wrap(value) if value.present? - new_relation - end - CEVAL - end + def group(*args) + args.reject! { |a| a.blank? } + clone.tap { |r| r.group_values += args if args.present? } + end - ActiveRecord::Relation::SINGLE_VALUE_METHODS.each do |query_method| - attr_accessor :"#{query_method}_value" + def order(*args) + args.reject! { |a| a.blank? } + clone.tap { |r| r.order_values += args if args.present? } + end - class_eval <<-CEVAL, __FILE__, __LINE__ + 1 - def #{query_method}(value = true) - new_relation = clone - new_relation.#{query_method}_value = value - new_relation - end - CEVAL - end + def reorder(*args) + args.reject! { |a| a.blank? } + clone.tap { |r| r.order_values = args if args.present? } end - def extending(*modules) - new_relation = clone - new_relation.send :apply_modules, *modules - new_relation + def joins(*args) + args.flatten! + args.reject! { |a| a.blank? } + clone.tap { |r| r.joins_values += args if args.present? } end - def lock(locks = true, &block) - relation = clone - relation.send(:apply_modules, Module.new(&block)) if block_given? + def where(*args) + value = build_where(*args) + clone.tap { |r| r.where_values += Array.wrap(value) if value.present? } + end + + def having(*args) + value = build_where(*args) + clone.tap { |r| r.having_values += Array.wrap(value) if value.present? } + end + + def limit(value = true) + clone.tap { |r| r.limit_value = value } + end + + def offset(value = true) + clone.tap { |r| r.offset_value = value } + end + def lock(locks = true) case locks when String, TrueClass, NilClass - clone.tap {|new_relation| new_relation.lock_value = locks || true } + clone.tap { |r| r.lock_value = locks || true } else - clone.tap {|new_relation| new_relation.lock_value = false } + clone.tap { |r| r.lock_value = false } end end + def readonly(value = true) + clone.tap { |r| r.readonly_value = value } + end + + def create_with(value = true) + clone.tap { |r| r.create_with_value = value } + end + + def from(value = true) + clone.tap { |r| r.from_value = value } + end + + def extending(*modules) + clone.tap { |r| r.send :apply_modules, *modules } + end + def reverse_order order_clause = arel.send(:order_clauses).join(', ') relation = except(:order) -- cgit v1.2.3 From abd568bf1c48e9082c0be7407eca1155c5fe0599 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 26 Jun 2010 17:06:48 -0700 Subject: removing useless code. [#4988 state:resolved] Signed-off-by: Jeremy Kemper --- .../connection_adapters/sqlite_adapter.rb | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index ad6314c530..1927585c49 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -151,7 +151,7 @@ module ActiveRecord # DATABASE STATEMENTS ====================================== def execute(sql, name = nil) #:nodoc: - catch_schema_changes { log(sql, name) { @connection.execute(sql) } } + log(sql, name) { @connection.execute(sql) } end def update_sql(sql, name = nil) #:nodoc: @@ -176,15 +176,15 @@ module ActiveRecord end def begin_db_transaction #:nodoc: - catch_schema_changes { @connection.transaction } + @connection.transaction end def commit_db_transaction #:nodoc: - catch_schema_changes { @connection.commit } + @connection.commit end def rollback_db_transaction #:nodoc: - catch_schema_changes { @connection.rollback } + @connection.rollback end # SCHEMA STATEMENTS ======================================== @@ -391,17 +391,6 @@ module ActiveRecord end end - def catch_schema_changes - return yield - rescue ActiveRecord::StatementInvalid => exception - if exception.message =~ /database schema has changed/ - reconnect! - retry - else - raise - end - end - def sqlite_version @sqlite_version ||= SQLiteAdapter::Version.new(select_value('select sqlite_version(*)')) end -- cgit v1.2.3 From 0c0b0aa0f223523331afdc157fb3992a121bf497 Mon Sep 17 00:00:00 2001 From: George Montana Harkin Date: Sun, 27 Jun 2010 19:05:51 +0530 Subject: Fixes #2415 by creating a new instance of the Model when saving attributes to that model and the associated attributes already exist. Tests included. [#2415 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/nested_attributes.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 12a75f5d16..c0302136ea 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -296,7 +296,9 @@ module ActiveRecord assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) elsif attributes['id'] - raise_nested_attributes_record_not_found(association_name, attributes['id']) + existing_record = self.class.reflect_on_association(association_name).klass.find(attributes['id']) + assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]) + self.send(association_name.to_s+'=', existing_record) elsif !reject_new_record?(association_name, attributes) method = "build_#{association_name}" @@ -366,11 +368,16 @@ module ActiveRecord unless reject_new_record?(association_name, attributes) association.build(attributes.except(*UNASSIGNABLE_KEYS)) end + + elsif existing_records.count == 0 #Existing record but not yet associated + existing_record = self.class.reflect_on_association(association_name).klass.find(attributes['id']) + association.send(:add_record_to_target_with_callbacks, existing_record) unless association.loaded? + assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]) + elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s } association.send(:add_record_to_target_with_callbacks, existing_record) unless association.loaded? assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]) - else - raise_nested_attributes_record_not_found(association_name, attributes['id']) + end end end @@ -390,7 +397,7 @@ module ActiveRecord ConnectionAdapters::Column.value_to_boolean(hash['_destroy']) end - # Determines if a new record should be build by checking for + # Determines if a new record should be built by checking for # has_destroy_flag? or if a :reject_if proc exists for this # association and evaluates to +true+. def reject_new_record?(association_name, attributes) @@ -406,9 +413,5 @@ module ActiveRecord end end - def raise_nested_attributes_record_not_found(association_name, record_id) - reflection = self.class.reflect_on_association(association_name) - raise RecordNotFound, "Couldn't find #{reflection.klass.name} with ID=#{record_id} for #{self.class.name} with ID=#{id}" - end end end -- cgit v1.2.3