aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorYehuda Katz <wycats@gmail.com>2009-01-30 10:53:14 -0800
committerYehuda Katz <wycats@gmail.com>2009-01-30 10:53:19 -0800
commitda10673e32718d6a0619bd0f4b4d3d796db86a1a (patch)
tree7bdb7868b0be65daec06ba729f68deccfe61a8bb /activerecord
parentb8fadd708b9850a77e1f64038763fffcff502499 (diff)
parented0e5640879fd42c00fc5900e0355a0ea1dcf2ad (diff)
downloadrails-da10673e32718d6a0619bd0f4b4d3d796db86a1a.tar.gz
rails-da10673e32718d6a0619bd0f4b4d3d796db86a1a.tar.bz2
rails-da10673e32718d6a0619bd0f4b4d3d796db86a1a.zip
Sync 'rails/rails/master'
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG8
-rw-r--r--activerecord/MIT-LICENSE2
-rw-r--r--activerecord/Rakefile8
-rw-r--r--activerecord/lib/active_record.rb2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb4
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb12
-rwxr-xr-xactiverecord/lib/active_record/base.rb151
-rw-r--r--activerecord/lib/active_record/calculations.rb24
-rw-r--r--activerecord/lib/active_record/callbacks.rb7
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql_adapter.rb7
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb2
-rw-r--r--activerecord/lib/active_record/dirty.rb4
-rw-r--r--activerecord/lib/active_record/locale/en.yml2
-rw-r--r--activerecord/lib/active_record/named_scope.rb2
-rw-r--r--activerecord/lib/active_record/reflection.rb8
-rw-r--r--activerecord/lib/active_record/validations.rb2
-rw-r--r--activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb11
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb3
-rwxr-xr-xactiverecord/test/cases/base_test.rb7
-rw-r--r--activerecord/test/cases/callbacks_test.rb44
-rw-r--r--activerecord/test/cases/connection_test_mysql.rb26
-rw-r--r--activerecord/test/cases/copy_table_test_sqlite.rb11
-rw-r--r--activerecord/test/cases/dirty_test.rb26
-rw-r--r--activerecord/test/cases/helper.rb2
-rw-r--r--activerecord/test/cases/json_serialization_test.rb2
-rw-r--r--activerecord/test/cases/method_scoping_test.rb10
-rw-r--r--activerecord/test/cases/named_scope_test.rb20
-rw-r--r--activerecord/test/connections/jdbc_jdbcderby/connection.rb18
-rw-r--r--activerecord/test/connections/jdbc_jdbch2/connection.rb18
-rw-r--r--activerecord/test/connections/jdbc_jdbchsqldb/connection.rb18
-rw-r--r--activerecord/test/connections/jdbc_jdbcmysql/connection.rb26
-rw-r--r--activerecord/test/connections/jdbc_jdbcpostgresql/connection.rb26
-rw-r--r--activerecord/test/connections/jdbc_jdbcsqlite3/connection.rb25
-rw-r--r--activerecord/test/models/post.rb6
-rw-r--r--activerecord/test/models/topic.rb2
-rw-r--r--activerecord/test/schema/schema.rb6
36 files changed, 462 insertions, 90 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 35172ae110..507e37ac3b 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,9 @@
*2.3.0/3.0*
+* Make after_save callbacks fire only if the record was successfully saved. #1735 [Michael Lovitt]
+
+ Previously the callbacks would fire if a before_save cancelled saving.
+
* Support nested transactions using database savepoints. #383 [Jonathan Viney, Hongli Lai]
* Added dynamic scopes ala dynamic finders #1648 [Yaroslav Markin]
@@ -5250,7 +5254,7 @@ in effect. Added :readonly finder constraint. Calling an association collectio
end
This will assume that settings is a text column and will now YAMLize any object put in that attribute. You can also specify
- an optional :class_name option that'll raise an exception if a serialized object is retrieved as a descendent of a class not in
+ an optional :class_name option that'll raise an exception if a serialized object is retrieved as a descendant of a class not in
the hierarchy. Example:
class User < ActiveRecord::Base
@@ -5746,7 +5750,7 @@ _Misc_
*0.8.2*
* Added inheritable callback queues that can ensure that certain callback methods or inline fragments are
- run throughout the entire inheritance hierarchy. Regardless of whether a descendent overwrites the callback
+ run throughout the entire inheritance hierarchy. Regardless of whether a descendant overwrites the callback
method:
class Topic < ActiveRecord::Base
diff --git a/activerecord/MIT-LICENSE b/activerecord/MIT-LICENSE
index 93be57f683..e6df48772f 100644
--- a/activerecord/MIT-LICENSE
+++ b/activerecord/MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2004-2008 David Heinemeier Hansson
+Copyright (c) 2004-2009 David Heinemeier Hansson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/activerecord/Rakefile b/activerecord/Rakefile
index f47674d5b7..1c7e2603ee 100644
--- a/activerecord/Rakefile
+++ b/activerecord/Rakefile
@@ -32,9 +32,13 @@ task :default => :test
desc 'Run mysql, sqlite, and postgresql tests'
task :test => %w(test_mysql test_sqlite3 test_postgresql)
-for adapter in %w( mysql postgresql sqlite sqlite3 firebird db2 oracle sybase openbase frontbase )
+for adapter in %w( mysql postgresql sqlite sqlite3 firebird db2 oracle sybase openbase frontbase jdbcmysql jdbcpostgresql jdbcsqlite3 jdbcderby jdbch2 jdbchsqldb )
Rake::TestTask.new("test_#{adapter}") { |t|
- t.libs << "test" << "test/connections/native_#{adapter}"
+ if adapter =~ /jdbc/
+ t.libs << "test" << "test/connections/jdbc_#{adapter}"
+ else
+ t.libs << "test" << "test/connections/native_#{adapter}"
+ end
adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z]+/]
t.test_files=Dir.glob( "test/cases/**/*_test{,_#{adapter_short}}.rb" ).sort
t.verbose = true
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb
index 390c005785..e1265b7e1e 100644
--- a/activerecord/lib/active_record.rb
+++ b/activerecord/lib/active_record.rb
@@ -1,5 +1,5 @@
#--
-# Copyright (c) 2004-2008 David Heinemeier Hansson
+# Copyright (c) 2004-2009 David Heinemeier Hansson
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 86616abf52..8b51a38f48 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1531,14 +1531,14 @@ module ActiveRecord
association = send(reflection.name)
association.destroy unless association.nil?
end
- before_destroy method_name
+ after_destroy method_name
when :delete
method_name = "belongs_to_dependent_delete_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.delete unless association.nil?
end
- before_destroy method_name
+ after_destroy method_name
else
raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{reflection.options[:dependent].inspect})"
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 3d689098b5..a5cc3bf091 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
@@ -9,6 +9,14 @@ module ActiveRecord
create_record(attributes) { |record| insert_record(record, true) }
end
+ def columns
+ @reflection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns")
+ end
+
+ def reset_column_information
+ @reflection.reset_column_information
+ end
+
protected
def construct_find_options!(options)
options[:joins] = @join_sql
@@ -32,8 +40,6 @@ module ActiveRecord
if @reflection.options[:insert_sql]
@owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record))
else
- columns = @owner.connection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns")
-
attributes = columns.inject({}) do |attrs, column|
case column.name.to_s
when @reflection.primary_key_name.to_s
@@ -103,7 +109,7 @@ module ActiveRecord
# clause has been explicitly defined. Otherwise you can get broken records back, if, for example, the join column also has
# an id column. This will then overwrite the id column of the records coming back.
def finding_with_ambiguous_select?(select_clause)
- !select_clause && @owner.connection.columns(@reflection.options[:join_table], "Join Table Columns").size != 2
+ !select_clause && columns.size != 2
end
private
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index cca012ed55..0efccb66ee 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -327,7 +327,7 @@ module ActiveRecord #:nodoc:
# User.find(user.id).preferences # => { "background" => "black", "display" => large }
#
# You can also specify a class option as the second parameter that'll raise an exception if a serialized object is retrieved as a
- # descendent of a class not in the hierarchy. Example:
+ # descendant of a class not in the hierarchy. Example:
#
# class User < ActiveRecord::Base
# serialize :preferences, Hash
@@ -544,8 +544,9 @@ module ActiveRecord #:nodoc:
# * <tt>:having</tt> - Combined with +:group+ this can be used to filter the records that a <tt>GROUP BY</tt> returns. Uses the <tt>HAVING</tt> SQL-clause.
# * <tt>:limit</tt> - An integer determining the limit on the number of rows that should be returned.
# * <tt>:offset</tt> - An integer determining the offset from where the rows should be fetched. So at 5, it would skip rows 0 through 4.
- # * <tt>:joins</tt> - Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id" (rarely needed)
- # or named associations in the same form used for the <tt>:include</tt> option, which will perform an <tt>INNER JOIN</tt> on the associated table(s).
+ # * <tt>:joins</tt> - Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id" (rarely needed),
+ # named associations in the same form used for the <tt>:include</tt> option, which will perform an <tt>INNER JOIN</tt> on the associated table(s),
+ # or an array containing a mixture of both strings and named associations.
# If the value is a string, then the records will be returned read-only since they will have attributes that do not correspond to the table's columns.
# Pass <tt>:readonly => false</tt> to override.
# * <tt>:include</tt> - Names associations that should be loaded alongside. The symbols named refer
@@ -755,25 +756,26 @@ module ActiveRecord #:nodoc:
end
end
- # Delete an object (or multiple objects) where the +id+ given matches the primary_key. A SQL +DELETE+ command
- # is executed on the database which means that no callbacks are fired off running this. This is an efficient method
- # of deleting records that don't need cleaning up after or other actions to be taken.
+ # Deletes the row with a primary key matching the +id+ argument, using a
+ # SQL +DELETE+ statement, and returns the number of rows deleted. Active
+ # Record objects are not instantiated, so the object's callbacks are not
+ # executed, including any <tt>:dependent</tt> association options or
+ # Observer methods.
#
- # Objects are _not_ instantiated with this method, and so +:dependent+ rules
- # defined on associations are not honered.
+ # You can delete multiple rows at once by passing an Array of <tt>id</tt>s.
#
- # ==== Parameters
- #
- # * +id+ - Can be either an Integer or an Array of Integers.
+ # Note: Although it is often much faster than the alternative,
+ # <tt>#destroy</tt>, skipping callbacks might bypass business logic in
+ # your application that ensures referential integrity or performs other
+ # essential jobs.
#
# ==== Examples
#
- # # Delete a single object
+ # # Delete a single row
# Todo.delete(1)
#
- # # Delete multiple objects
- # todos = [1,2,3]
- # Todo.delete(todos)
+ # # Delete multiple rows
+ # Todo.delete([2,3,4])
def delete(id)
delete_all([ "#{connection.quote_column_name(primary_key)} IN (?)", id ])
end
@@ -849,25 +851,32 @@ module ActiveRecord #:nodoc:
connection.update(sql, "#{name} Update")
end
- # Destroys the records matching +conditions+ by instantiating each record and calling their +destroy+ method.
- # This means at least 2*N database queries to destroy N records, so avoid +destroy_all+ if you are deleting
- # many records. If you want to simply delete records without worrying about dependent associations or
- # callbacks, use the much faster +delete_all+ method instead.
+ # Destroys the records matching +conditions+ by instantiating each
+ # record and calling its +destroy+ method. Each object's callbacks are
+ # executed (including <tt>:dependent</tt> association options and
+ # +before_destroy+/+after_destroy+ Observer methods). Returns the
+ # collection of objects that were destroyed; each will be frozen, to
+ # reflect that no changes should be made (since they can't be
+ # persisted).
+ #
+ # Note: Instantiation, callback execution, and deletion of each
+ # record can be time consuming when you're removing many records at
+ # once. It generates at least one SQL +DELETE+ query per record (or
+ # possibly more, to enforce your callbacks). If you want to delete many
+ # rows quickly, without concern for their associations or callbacks, use
+ # +delete_all+ instead.
#
# ==== Parameters
#
- # * +conditions+ - Conditions are specified the same way as with +find+ method.
+ # * +conditions+ - A string, array, or hash that specifies which records
+ # to destroy. If omitted, all records are destroyed. See the
+ # Conditions section in the introduction to ActiveRecord::Base for
+ # more information.
#
- # ==== Example
+ # ==== Examples
#
# Person.destroy_all("last_login < '2004-04-04'")
- #
- # This loads and destroys each person one by one, including its dependent associations and before_ and
- # after_destroy callbacks.
- #
- # +conditions+ can be anything that +find+ also accepts:
- #
- # Person.destroy_all(:last_login => 6.hours.ago)
+ # Person.destroy_all(:status => "inactive")
def destroy_all(conditions = nil)
find(:all, :conditions => conditions).each { |object| object.destroy }
end
@@ -918,7 +927,7 @@ module ActiveRecord #:nodoc:
#
# ==== Parameters
#
- # * +id+ - The id of the object you wish to update a counter on.
+ # * +id+ - The id of the object you wish to update a counter on or an Array of ids.
# * +counters+ - An Array of Hashes containing the names of the fields
# to update as keys and the amount to update the field by as values.
#
@@ -932,12 +941,27 @@ module ActiveRecord #:nodoc:
# # SET comment_count = comment_count - 1,
# # action_count = action_count + 1
# # WHERE id = 5
+ #
+ # # For the Posts with id of 10 and 15, increment the comment_count by 1
+ # Post.update_counters [10, 15], :comment_count => 1
+ # # Executes the following SQL:
+ # # UPDATE posts
+ # # SET comment_count = comment_count + 1,
+ # # WHERE id IN (10, 15)
def update_counters(id, counters)
updates = counters.inject([]) { |list, (counter_name, increment)|
sign = increment < 0 ? "-" : "+"
list << "#{connection.quote_column_name(counter_name)} = COALESCE(#{connection.quote_column_name(counter_name)}, 0) #{sign} #{increment.abs}"
}.join(", ")
- update_all(updates, "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}")
+
+ if id.is_a?(Array)
+ ids_list = id.map {|i| quote_value(i)}.join(', ')
+ condition = "IN (#{ids_list})"
+ else
+ condition = "= #{quote_value(id)}"
+ end
+
+ update_all(updates, "#{connection.quote_column_name(primary_key)} #{condition}")
end
# Increment a number field by one, usually representing a count.
@@ -1691,7 +1715,7 @@ module ActiveRecord #:nodoc:
end
join
end
- joins.flatten.uniq
+ joins.flatten.map{|j| j.strip}.uniq
else
joins.collect{|j| safe_to_array(j)}.flatten.uniq
end
@@ -1802,15 +1826,13 @@ module ActiveRecord #:nodoc:
table_name
end
- # Enables dynamic finders like find_by_user_name(user_name) and find_by_user_name_and_password(user_name, password) that are turned into
- # find(:first, :conditions => ["user_name = ?", user_name]) and find(:first, :conditions => ["user_name = ? AND password = ?", user_name, password])
- # respectively. Also works for find(:all) by using find_all_by_amount(50) that is turned into find(:all, :conditions => ["amount = ?", 50]).
- #
- # It's even possible to use all the additional parameters to find. For example, the full interface for find_all_by_amount
- # is actually find_all_by_amount(amount, options).
+ # Enables dynamic finders like <tt>find_by_user_name(user_name)</tt> and <tt>find_by_user_name_and_password(user_name, password)</tt>
+ # that are turned into <tt>find(:first, :conditions => ["user_name = ?", user_name])</tt> and
+ # <tt>find(:first, :conditions => ["user_name = ? AND password = ?", user_name, password])</tt> respectively. Also works for
+ # <tt>find(:all)</tt> by using <tt>find_all_by_amount(50)</tt> that is turned into <tt>find(:all, :conditions => ["amount = ?", 50])</tt>.
#
- # This also enables you to initialize a record if it is not found, such as find_or_initialize_by_amount(amount)
- # or find_or_create_by_user_and_password(user, password).
+ # It's even possible to use all the additional parameters to +find+. For example, the full interface for +find_all_by_amount+
+ # is actually <tt>find_all_by_amount(amount, options)</tt>.
#
# Also enables dynamic scopes like scoped_by_user_name(user_name) and scoped_by_user_name_and_password(user_name, password) that
# are turned into scoped(:conditions => ["user_name = ?", user_name]) and scoped(:conditions => ["user_name = ? AND password = ?", user_name, password])
@@ -2032,7 +2054,11 @@ module ActiveRecord #:nodoc:
# end
#
# In nested scopings, all previous parameters are overwritten by the innermost rule, with the exception of
- # <tt>:conditions</tt> and <tt>:include</tt> options in <tt>:find</tt>, which are merged.
+ # <tt>:conditions</tt>, <tt>:include</tt>, and <tt>:joins</tt> options in <tt>:find</tt>, which are merged.
+ #
+ # <tt>:joins</tt> options are uniqued so multiple scopes can join in the same table without table aliasing
+ # problems. If you need to join multiple tables, but still want one of the tables to be uniqued, use the
+ # array of strings format for your joins.
#
# class Article < ActiveRecord::Base
# def self.find_with_scope
@@ -2086,7 +2112,11 @@ module ActiveRecord #:nodoc:
(hash[method].keys + params.keys).uniq.each do |key|
merge = hash[method][key] && params[key] # merge if both scopes have the same key
if key == :conditions && merge
- hash[method][key] = merge_conditions(params[key], hash[method][key])
+ if params[key].is_a?(Hash) && hash[method][key].is_a?(Hash)
+ hash[method][key] = merge_conditions(hash[method][key].deep_merge(params[key]))
+ else
+ hash[method][key] = merge_conditions(params[key], hash[method][key])
+ end
elsif key == :include && merge
hash[method][key] = merge_includes(hash[method][key], params[key]).uniq
elsif key == :joins && merge
@@ -2096,7 +2126,7 @@ module ActiveRecord #:nodoc:
end
end
else
- hash[method] = params.merge(hash[method])
+ hash[method] = hash[method].merge(params)
end
else
hash[method] = params
@@ -2156,7 +2186,7 @@ module ActiveRecord #:nodoc:
scoped_methods.last
end
- # Returns the class type of the record using the current module as a prefix. So descendents of
+ # Returns the class type of the record using the current module as a prefix. So descendants of
# MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
def compute_type(type_name)
modularized_name = type_name_with_module(type_name)
@@ -2169,7 +2199,8 @@ module ActiveRecord #:nodoc:
end
end
- # Returns the class descending directly from Active Record in the inheritance hierarchy.
+ # Returns the class descending directly from ActiveRecord::Base or an
+ # abstract class, if any, in the inheritance hierarchy.
def class_of_active_record_descendant(klass)
if klass.superclass == Base || klass.superclass.abstract_class?
klass
@@ -2518,14 +2549,16 @@ module ActiveRecord #:nodoc:
create_or_update || raise(RecordNotSaved)
end
- # Deletes the record in the database and freezes this instance to reflect that no changes should
- # be made (since they can't be persisted).
+ # Deletes the record in the database and freezes this instance to
+ # reflect that no changes should be made (since they can't be
+ # persisted). Returns the frozen instance.
+ #
+ # The row is simply removed with a SQL +DELETE+ statement on the
+ # record's primary key, and no callbacks are executed.
#
- # Unlike #destroy, this method doesn't run any +before_delete+ and +after_delete+
- # callbacks, nor will it enforce any association +:dependent+ rules.
- #
- # In addition to deleting this record, any defined +before_delete+ and +after_delete+
- # callbacks are run, and +:dependent+ rules defined on associations are run.
+ # To enforce the object's +before_destroy+ and +after_destroy+
+ # callbacks, Observer methods, or any <tt>:dependent</tt> association
+ # options, use <tt>#destroy</tt>.
def delete
self.class.delete(id) unless new_record?
freeze
@@ -2726,7 +2759,19 @@ module ActiveRecord #:nodoc:
end
end
- # Format attributes nicely for inspect.
+ # Returns an <tt>#inspect</tt>-like string for the value of the
+ # attribute +attr_name+. String attributes are elided after 50
+ # characters, and Date and Time attributes are returned in the
+ # <tt>:db</tt> format. Other attributes return the value of
+ # <tt>#inspect</tt> without modification.
+ #
+ # person = Person.create!(:name => "David Heinemeier Hansson " * 3)
+ #
+ # person.attribute_for_inspect(:name)
+ # # => '"David Heinemeier Hansson David Heinemeier Hansson D..."'
+ #
+ # person.attribute_for_inspect(:created_at)
+ # # => '"2009-01-12 04:48:57"'
def attribute_for_inspect(attr_name)
value = read_attribute(attr_name)
@@ -2855,7 +2900,7 @@ module ActiveRecord #:nodoc:
id
end
- # Sets the attribute used for single table inheritance to this class name if this is not the ActiveRecord::Base descendent.
+ # Sets the attribute used for single table inheritance to this class name if this is not the ActiveRecord::Base descendant.
# Considering the hierarchy Reply < Message < ActiveRecord::Base, this makes it possible to do Reply.new without having to
# set <tt>Reply[Reply.inheritance_column] = "Reply"</tt> yourself. No such attribute would be set for objects of the
# Message class in that example.
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index 65512d534a..b239c03284 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -48,30 +48,38 @@ module ActiveRecord
calculate(:count, *construct_count_options_from_args(*args))
end
- # Calculates the average value on a given column. The value is returned as a float. See +calculate+ for examples with options.
+ # Calculates the average value on a given column. The value is returned as
+ # a float, or +nil+ if there's no row. See +calculate+ for examples with
+ # options.
#
- # Person.average('age')
+ # Person.average('age') # => 35.8
def average(column_name, options = {})
calculate(:avg, column_name, options)
end
- # Calculates the minimum value on a given column. The value is returned with the same data type of the column. See +calculate+ for examples with options.
+ # Calculates the minimum value on a given column. The value is returned
+ # with the same data type of the column, or +nil+ if there's no row. See
+ # +calculate+ for examples with options.
#
- # Person.minimum('age')
+ # Person.minimum('age') # => 7
def minimum(column_name, options = {})
calculate(:min, column_name, options)
end
- # Calculates the maximum value on a given column. The value is returned with the same data type of the column. See +calculate+ for examples with options.
+ # Calculates the maximum value on a given column. The value is returned
+ # with the same data type of the column, or +nil+ if there's no row. See
+ # +calculate+ for examples with options.
#
- # Person.maximum('age')
+ # Person.maximum('age') # => 93
def maximum(column_name, options = {})
calculate(:max, column_name, options)
end
- # Calculates the sum of values on a given column. The value is returned with the same data type of the column. See +calculate+ for examples with options.
+ # Calculates the sum of values on a given column. The value is returned
+ # with the same data type of the column, 0 if there's no row. See
+ # +calculate+ for examples with options.
#
- # Person.sum('age')
+ # Person.sum('age') # => 4562
def sum(column_name, options = {})
calculate(:sum, column_name, options)
end
diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb
index 42bfe34505..88958f4583 100644
--- a/activerecord/lib/active_record/callbacks.rb
+++ b/activerecord/lib/active_record/callbacks.rb
@@ -77,7 +77,7 @@ module ActiveRecord
#
# In that case, <tt>Reply#destroy</tt> would only run +destroy_readers+ and _not_ +destroy_author+. So, use the callback macros when
# you want to ensure that a certain callback is called for the entire hierarchy, and use the regular overwriteable methods
- # when you want to leave it up to each descendent to decide whether they want to call +super+ and trigger the inherited callbacks.
+ # when you want to leave it up to each descendant to decide whether they want to call +super+ and trigger the inherited callbacks.
#
# *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the callbacks before specifying the
# associations. Otherwise, you might trigger the loading of a child before the parent has registered the callbacks and they won't
@@ -219,8 +219,9 @@ module ActiveRecord
def after_save() end
def create_or_update_with_callbacks #:nodoc:
return false if callback(:before_save) == false
- result = create_or_update_without_callbacks
- callback(:after_save)
+ if result = create_or_update_without_callbacks
+ callback(:after_save)
+ end
result
end
private :create_or_update_with_callbacks
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index b2345fd571..9300df28ee 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -152,6 +152,7 @@ module ActiveRecord
# * <tt>:password</tt> - Defaults to nothing.
# * <tt>:database</tt> - The name of the database. No default, must be provided.
# * <tt>:encoding</tt> - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
+ # * <tt>:reconnect</tt> - Defaults to false (See MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).
# * <tt>:sslca</tt> - Necessary to use MySQL with an SSL connection.
# * <tt>:sslkey</tt> - Necessary to use MySQL with an SSL connection.
# * <tt>:sslcert</tt> - Necessary to use MySQL with an SSL connection.
@@ -563,8 +564,6 @@ module ActiveRecord
private
def connect
- @connection.reconnect = true if @connection.respond_to?(:reconnect=)
-
encoding = @config[:encoding]
if encoding
@connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil
@@ -575,6 +574,10 @@ module ActiveRecord
end
@connection.real_connect(*@connection_options)
+
+ # reconnect must be set after real_connect is called, because real_connect sets it to false internally
+ @connection.reconnect = !!@config[:reconnect] if @connection.respond_to?(:reconnect=)
+
configure_connection
end
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
index 9387cf8827..5390f49f04 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -306,7 +306,7 @@ module ActiveRecord
end
def copy_table(from, to, options = {}) #:nodoc:
- options = options.merge(:id => !columns(from).detect{|c| c.name == 'id'}.nil?)
+ options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
create_table(to, options) do |definition|
@definition = definition
columns(from).each do |column|
diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb
index 4c899f58e5..4a2510aa63 100644
--- a/activerecord/lib/active_record/dirty.rb
+++ b/activerecord/lib/active_record/dirty.rb
@@ -151,8 +151,8 @@ module ActiveRecord
def field_changed?(attr, old, value)
if column = column_for_attribute(attr)
- if column.type == :integer && column.null && (old.nil? || old == 0) && value.blank?
- # For nullable integer columns, NULL gets stored in database for blank (i.e. '') values.
+ if column.number? && column.null && (old.nil? || old == 0) && value.blank?
+ # For nullable numeric columns, NULL gets stored in database for blank (i.e. '') values.
# Hence we don't record it as a change if the value changes from nil to ''.
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
# be typecast back to 0 (''.to_i => 0)
diff --git a/activerecord/lib/active_record/locale/en.yml b/activerecord/lib/active_record/locale/en.yml
index 7e205435f7..bf8a71d236 100644
--- a/activerecord/lib/active_record/locale/en.yml
+++ b/activerecord/lib/active_record/locale/en.yml
@@ -37,7 +37,7 @@ en:
# blank: "This is a custom blank message for User login"
# Will define custom blank validation message for User model and
# custom blank validation message for login attribute of User model.
- models:
+ #models:
# Translate model names. Used in Model.human_name().
#models:
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index 83043c2c22..989b2a1ec5 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -39,7 +39,7 @@ module ActiveRecord
# Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt> returns the number of garments
# for which these criteria obtain. Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
#
- # All \scopes are available as class methods on the ActiveRecord::Base descendent upon which the \scopes were defined. But they are also available to
+ # All \scopes are available as class methods on the ActiveRecord::Base descendant upon which the \scopes were defined. But they are also available to
# <tt>has_many</tt> associations. If,
#
# class Person < ActiveRecord::Base
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index dbff4f24d6..1937abdc83 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -198,6 +198,14 @@ module ActiveRecord
end
end
+ def columns(tbl_name, log_msg)
+ @columns ||= klass.connection.columns(tbl_name, log_msg)
+ end
+
+ def reset_column_information
+ @columns = nil
+ end
+
def check_validity!
end
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 6a9690ba85..6d750accb0 100644
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -904,7 +904,7 @@ module ActiveRecord
configuration.update(attr_names.extract_options!)
validates_each(attr_names, configuration) do |record, attr_name, value|
- unless (value.is_a?(Array) ? value : [value]).inject(true) { |v, r| (r.nil? || r.valid?) && v }
+ unless (value.is_a?(Array) ? value : [value]).collect { |r| r.nil? || r.valid? }.all?
record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value)
end
end
diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
index 2f08e09d43..1e3b423471 100644
--- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -775,4 +775,15 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
end
end
end
+
+ def test_caching_of_columns
+ david = Developer.find(1)
+ # clear cache possibly created by other tests
+ david.projects.reset_column_information
+ assert_queries(0) { david.projects.columns; david.projects.columns }
+ # and again to verify that reset_column_information clears the cache correctly
+ david.projects.reset_column_information
+ assert_queries(0) { david.projects.columns; david.projects.columns }
+ end
+
end
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index a5ae5cd8ec..428fb50013 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -698,7 +698,8 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
authors(:david).destroy
end
- assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids[authors(:david).id]
+ assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_id)
+ assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_extra_id)
end
def test_invalid_belongs_to_dependent_option_raises_exception
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 0f03dae829..973bb567bd 100755
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -639,6 +639,13 @@ class BasicsTest < ActiveRecord::TestCase
category.reload
assert_not_nil category.categorizations_count
assert_equal 4, category.categorizations_count
+
+ category_2 = categories(:technology)
+ count_1, count_2 = (category.categorizations_count || 0), (category_2.categorizations_count || 0)
+ Category.update_counters([category.id, category_2.id], "categorizations_count" => 2)
+ category.reload; category_2.reload
+ assert_equal count_1 + 2, category.categorizations_count
+ assert_equal count_2 + 2, category_2.categorizations_count
end
def test_update_all
diff --git a/activerecord/test/cases/callbacks_test.rb b/activerecord/test/cases/callbacks_test.rb
index 11830a2ef6..33b1ea034d 100644
--- a/activerecord/test/cases/callbacks_test.rb
+++ b/activerecord/test/cases/callbacks_test.rb
@@ -121,9 +121,19 @@ end
class CallbackCancellationDeveloper < ActiveRecord::Base
set_table_name 'developers'
- def before_create
- false
- end
+
+ attr_reader :after_save_called, :after_create_called, :after_update_called, :after_destroy_called
+ attr_accessor :cancel_before_save, :cancel_before_create, :cancel_before_update, :cancel_before_destroy
+
+ def before_save; !@cancel_before_save; end
+ def before_create; !@cancel_before_create; end
+ def before_update; !@cancel_before_update; end
+ def before_destroy; !@cancel_before_destroy; end
+
+ def after_save; @after_save_called = true; end
+ def after_update; @after_update_called = true; end
+ def after_create; @after_create_called = true; end
+ def after_destroy; @after_destroy_called = true; end
end
class CallbacksTest < ActiveRecord::TestCase
@@ -349,19 +359,47 @@ class CallbacksTest < ActiveRecord::TestCase
assert !david.valid?
assert !david.save
assert_raises(ActiveRecord::RecordInvalid) { david.save! }
+
+ someone = CallbackCancellationDeveloper.find(1)
+ someone.cancel_before_save = true
+ assert someone.valid?
+ assert !someone.save
+ assert_save_callbacks_not_called(someone)
end
def test_before_create_returning_false
someone = CallbackCancellationDeveloper.new
+ someone.cancel_before_create = true
assert someone.valid?
assert !someone.save
+ assert_save_callbacks_not_called(someone)
+ end
+
+ def test_before_update_returning_false
+ someone = CallbackCancellationDeveloper.find(1)
+ someone.cancel_before_update = true
+ assert someone.valid?
+ assert !someone.save
+ assert_save_callbacks_not_called(someone)
end
def test_before_destroy_returning_false
david = ImmutableDeveloper.find(1)
assert !david.destroy
assert_not_nil ImmutableDeveloper.find_by_id(1)
+
+ someone = CallbackCancellationDeveloper.find(1)
+ someone.cancel_before_destroy = true
+ assert !someone.destroy
+ assert !someone.after_destroy_called
+ end
+
+ def assert_save_callbacks_not_called(someone)
+ assert !someone.after_save_called
+ assert !someone.after_create_called
+ assert !someone.after_update_called
end
+ private :assert_save_callbacks_not_called
def test_zzz_callback_returning_false # must be run last since we modify CallbackDeveloper
david = CallbackDeveloper.find(1)
diff --git a/activerecord/test/cases/connection_test_mysql.rb b/activerecord/test/cases/connection_test_mysql.rb
index 40ddcf5420..f79ee2f1f7 100644
--- a/activerecord/test/cases/connection_test_mysql.rb
+++ b/activerecord/test/cases/connection_test_mysql.rb
@@ -2,9 +2,24 @@ require "cases/helper"
class MysqlConnectionTest < ActiveRecord::TestCase
def setup
+ super
@connection = ActiveRecord::Base.connection
end
+ def test_mysql_reconnect_attribute_after_connection_with_reconnect_true
+ run_without_connection do |orig_connection|
+ ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => true}))
+ assert ActiveRecord::Base.connection.raw_connection.reconnect
+ end
+ end
+
+ def test_mysql_reconnect_attribute_after_connection_with_reconnect_false
+ run_without_connection do |orig_connection|
+ ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => false}))
+ assert !ActiveRecord::Base.connection.raw_connection.reconnect
+ end
+ end
+
def test_no_automatic_reconnection_after_timeout
assert @connection.active?
@connection.update('set @@wait_timeout=1')
@@ -27,4 +42,15 @@ class MysqlConnectionTest < ActiveRecord::TestCase
@connection.verify!
assert @connection.active?
end
+
+ private
+
+ def run_without_connection
+ original_connection = ActiveRecord::Base.remove_connection
+ begin
+ yield original_connection
+ ensure
+ ActiveRecord::Base.establish_connection(original_connection)
+ end
+ end
end
diff --git a/activerecord/test/cases/copy_table_test_sqlite.rb b/activerecord/test/cases/copy_table_test_sqlite.rb
index f0cfb67866..72bd7e2dab 100644
--- a/activerecord/test/cases/copy_table_test_sqlite.rb
+++ b/activerecord/test/cases/copy_table_test_sqlite.rb
@@ -46,6 +46,17 @@ class CopyTableTest < ActiveRecord::TestCase
test_copy_table('developers_projects', 'programmers_projects')
end
+ def test_copy_table_with_id_col_that_is_not_primary_key
+ test_copy_table('goofy_string_id', 'goofy_string_id2') do |from, to, options|
+ original_id = @connection.columns('goofy_string_id').detect{|col| col.name == 'id' }
+ copied_id = @connection.columns('goofy_string_id2').detect{|col| col.name == 'id' }
+ assert_equal original_id.type, copied_id.type
+ assert_equal original_id.sql_type, copied_id.sql_type
+ assert_equal original_id.limit, copied_id.limit
+ assert_equal original_id.primary, copied_id.primary
+ end
+ end
+
protected
def copy_table(from, to, options = {})
@connection.copy_table(from, to, {:temporary => true}.merge(options))
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index 10cdbdc622..1c9e281cc0 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -21,6 +21,10 @@ private
end
end
+class NumericData < ActiveRecord::Base
+ self.table_name = 'numeric_data'
+end
+
class DirtyTest < ActiveRecord::TestCase
def test_attribute_changes
# New record - no changes.
@@ -58,7 +62,7 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal parrot.name_change, parrot.title_change
end
- def test_nullable_integer_not_marked_as_changed_if_new_value_is_blank
+ def test_nullable_number_not_marked_as_changed_if_new_value_is_blank
pirate = Pirate.new
["", nil].each do |value|
@@ -68,6 +72,26 @@ class DirtyTest < ActiveRecord::TestCase
end
end
+ def test_nullable_decimal_not_marked_as_changed_if_new_value_is_blank
+ numeric_data = NumericData.new
+
+ ["", nil].each do |value|
+ numeric_data.bank_balance = value
+ assert !numeric_data.bank_balance_changed?
+ assert_nil numeric_data.bank_balance_change
+ end
+ end
+
+ def test_nullable_float_not_marked_as_changed_if_new_value_is_blank
+ numeric_data = NumericData.new
+
+ ["", nil].each do |value|
+ numeric_data.temperature = value
+ assert !numeric_data.temperature_changed?
+ assert_nil numeric_data.temperature_change
+ end
+ end
+
def test_nullable_integer_zero_to_string_zero_not_marked_as_changed
pirate = Pirate.new
pirate.parrot_id = 0
diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb
index ccd04b67f6..24ce35e2e2 100644
--- a/activerecord/test/cases/helper.rb
+++ b/activerecord/test/cases/helper.rb
@@ -34,7 +34,7 @@ rescue LoadError
end
ActiveRecord::Base.connection.class.class_eval do
- IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/]
+ IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /SHOW FIELDS/]
def execute_with_query_record(sql, name = nil, &block)
$queries_executed ||= []
diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb
index 3446e5e7f0..975acde096 100644
--- a/activerecord/test/cases/json_serialization_test.rb
+++ b/activerecord/test/cases/json_serialization_test.rb
@@ -200,6 +200,6 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
2 => @mary
}
- assert_equal %({1: {"name": "David"}}), authors_hash.to_json(:only => [1, :name])
+ assert_equal %({"1": {"name": "David"}}), authors_hash.to_json(:only => [1, :name])
end
end
diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb
index 80a06116ad..71e2ce8790 100644
--- a/activerecord/test/cases/method_scoping_test.rb
+++ b/activerecord/test/cases/method_scoping_test.rb
@@ -186,6 +186,16 @@ class MethodScopingTest < ActiveRecord::TestCase
assert_equal authors(:david).attributes, scoped_authors.first.attributes
end
+ def test_scoped_find_strips_spaces_from_string_joins_and_eliminates_duplicate_string_joins
+ scoped_authors = Author.with_scope(:find => { :joins => ' INNER JOIN posts ON posts.author_id = authors.id '}) do
+ Author.find(:all, :select => 'DISTINCT authors.*', :joins => ['INNER JOIN posts ON posts.author_id = authors.id'], :conditions => 'posts.id = 1')
+ end
+ assert scoped_authors.include?(authors(:david))
+ assert !scoped_authors.include?(authors(:mary))
+ assert_equal 1, scoped_authors.size
+ assert_equal authors(:david).attributes, scoped_authors.first.attributes
+ end
+
def test_scoped_count_include
# with the include, will retrieve only developers for the given project
Developer.with_scope(:find => { :include => :projects }) do
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb
index bab842cf66..e1e27fa130 100644
--- a/activerecord/test/cases/named_scope_test.rb
+++ b/activerecord/test/cases/named_scope_test.rb
@@ -277,6 +277,26 @@ class NamedScopeTest < ActiveRecord::TestCase
post = Post.find(1)
assert_equal post.comments.size, Post.scoped(:joins => join).scoped(:joins => join, :conditions => "posts.id = #{post.id}").size
end
+
+ def test_chanining_should_use_latest_conditions_when_creating
+ post1 = Topic.rejected.approved.new
+ assert post1.approved?
+
+ post2 = Topic.approved.rejected.new
+ assert ! post2.approved?
+ end
+
+ def test_chanining_should_use_latest_conditions_when_searching
+ # Normal hash conditions
+ assert_equal Topic.all(:conditions => {:approved => true}), Topic.rejected.approved.all
+ assert_equal Topic.all(:conditions => {:approved => false}), Topic.approved.rejected.all
+
+ # Nested hash conditions with same keys
+ assert_equal [posts(:sti_comments)], Post.with_special_comments.with_very_special_comments.all
+
+ # Nested hash conditions with different keys
+ assert_equal [posts(:sti_comments)], Post.with_special_comments.with_post(4).all.uniq
+ end
end
class DynamicScopeMatchTest < ActiveRecord::TestCase
diff --git a/activerecord/test/connections/jdbc_jdbcderby/connection.rb b/activerecord/test/connections/jdbc_jdbcderby/connection.rb
new file mode 100644
index 0000000000..222ef5db38
--- /dev/null
+++ b/activerecord/test/connections/jdbc_jdbcderby/connection.rb
@@ -0,0 +1,18 @@
+print "Using Derby via JRuby, activerecord-jdbc-adapter and activerecord-jdbcderby-adapter\n"
+require_dependency 'models/course'
+require 'logger'
+ActiveRecord::Base.logger = Logger.new("debug.log")
+
+ActiveRecord::Base.configurations = {
+ 'arunit' => {
+ :adapter => 'jdbcderby',
+ :database => 'activerecord_unittest'
+ },
+ 'arunit2' => {
+ :adapter => 'jdbcderby',
+ :database => 'activerecord_unittest2'
+ }
+}
+
+ActiveRecord::Base.establish_connection 'arunit'
+Course.establish_connection 'arunit2'
diff --git a/activerecord/test/connections/jdbc_jdbch2/connection.rb b/activerecord/test/connections/jdbc_jdbch2/connection.rb
new file mode 100644
index 0000000000..9d2875e8e7
--- /dev/null
+++ b/activerecord/test/connections/jdbc_jdbch2/connection.rb
@@ -0,0 +1,18 @@
+print "Using H2 via JRuby, activerecord-jdbc-adapter and activerecord-jdbch2-adapter\n"
+require_dependency 'models/course'
+require 'logger'
+ActiveRecord::Base.logger = Logger.new("debug.log")
+
+ActiveRecord::Base.configurations = {
+ 'arunit' => {
+ :adapter => 'jdbch2',
+ :database => 'activerecord_unittest'
+ },
+ 'arunit2' => {
+ :adapter => 'jdbch2',
+ :database => 'activerecord_unittest2'
+ }
+}
+
+ActiveRecord::Base.establish_connection 'arunit'
+Course.establish_connection 'arunit2'
diff --git a/activerecord/test/connections/jdbc_jdbchsqldb/connection.rb b/activerecord/test/connections/jdbc_jdbchsqldb/connection.rb
new file mode 100644
index 0000000000..fa943c2c76
--- /dev/null
+++ b/activerecord/test/connections/jdbc_jdbchsqldb/connection.rb
@@ -0,0 +1,18 @@
+print "Using HSQLDB via JRuby, activerecord-jdbc-adapter and activerecord-jdbchsqldb-adapter\n"
+require_dependency 'models/course'
+require 'logger'
+ActiveRecord::Base.logger = Logger.new("debug.log")
+
+ActiveRecord::Base.configurations = {
+ 'arunit' => {
+ :adapter => 'jdbchsqldb',
+ :database => 'activerecord_unittest'
+ },
+ 'arunit2' => {
+ :adapter => 'jdbchsqldb',
+ :database => 'activerecord_unittest2'
+ }
+}
+
+ActiveRecord::Base.establish_connection 'arunit'
+Course.establish_connection 'arunit2'
diff --git a/activerecord/test/connections/jdbc_jdbcmysql/connection.rb b/activerecord/test/connections/jdbc_jdbcmysql/connection.rb
new file mode 100644
index 0000000000..e2517a50eb
--- /dev/null
+++ b/activerecord/test/connections/jdbc_jdbcmysql/connection.rb
@@ -0,0 +1,26 @@
+print "Using MySQL via JRuby, activerecord-jdbc-adapter and activerecord-jdbcmysql-adapter\n"
+require_dependency 'models/course'
+require 'logger'
+
+ActiveRecord::Base.logger = Logger.new("debug.log")
+
+# GRANT ALL PRIVILEGES ON activerecord_unittest.* to 'rails'@'localhost';
+# GRANT ALL PRIVILEGES ON activerecord_unittest2.* to 'rails'@'localhost';
+
+ActiveRecord::Base.configurations = {
+ 'arunit' => {
+ :adapter => 'jdbcmysql',
+ :username => 'rails',
+ :encoding => 'utf8',
+ :database => 'activerecord_unittest',
+ },
+ 'arunit2' => {
+ :adapter => 'jdbcmysql',
+ :username => 'rails',
+ :database => 'activerecord_unittest2'
+ }
+}
+
+ActiveRecord::Base.establish_connection 'arunit'
+Course.establish_connection 'arunit2'
+
diff --git a/activerecord/test/connections/jdbc_jdbcpostgresql/connection.rb b/activerecord/test/connections/jdbc_jdbcpostgresql/connection.rb
new file mode 100644
index 0000000000..0685da4433
--- /dev/null
+++ b/activerecord/test/connections/jdbc_jdbcpostgresql/connection.rb
@@ -0,0 +1,26 @@
+print "Using Postgrsql via JRuby, activerecord-jdbc-adapter and activerecord-postgresql-adapter\n"
+require_dependency 'models/course'
+require 'logger'
+
+ActiveRecord::Base.logger = Logger.new("debug.log")
+
+# createuser rails --createdb --no-superuser --no-createrole
+# createdb -O rails activerecord_unittest
+# createdb -O rails activerecord_unittest2
+
+ActiveRecord::Base.configurations = {
+ 'arunit' => {
+ :adapter => 'jdbcpostgresql',
+ :username => ENV['USER'] || 'rails',
+ :database => 'activerecord_unittest'
+ },
+ 'arunit2' => {
+ :adapter => 'jdbcpostgresql',
+ :username => ENV['USER'] || 'rails',
+ :database => 'activerecord_unittest2'
+ }
+}
+
+ActiveRecord::Base.establish_connection 'arunit'
+Course.establish_connection 'arunit2'
+
diff --git a/activerecord/test/connections/jdbc_jdbcsqlite3/connection.rb b/activerecord/test/connections/jdbc_jdbcsqlite3/connection.rb
new file mode 100644
index 0000000000..26d4676ff3
--- /dev/null
+++ b/activerecord/test/connections/jdbc_jdbcsqlite3/connection.rb
@@ -0,0 +1,25 @@
+print "Using SQLite3 via JRuby, activerecord-jdbc-adapter and activerecord-jdbcsqlite3-adapter\n"
+require_dependency 'models/course'
+require 'logger'
+ActiveRecord::Base.logger = Logger.new("debug.log")
+
+class SqliteError < StandardError
+end
+
+BASE_DIR = FIXTURES_ROOT
+sqlite_test_db = "#{BASE_DIR}/fixture_database.sqlite3"
+sqlite_test_db2 = "#{BASE_DIR}/fixture_database_2.sqlite3"
+
+def make_connection(clazz, db_file)
+ ActiveRecord::Base.configurations = { clazz.name => { :adapter => 'jdbcsqlite3', :database => db_file, :timeout => 5000 } }
+ unless File.exist?(db_file)
+ puts "SQLite3 database not found at #{db_file}. Rebuilding it."
+ sqlite_command = %Q{sqlite3 "#{db_file}" "create table a (a integer); drop table a;"}
+ puts "Executing '#{sqlite_command}'"
+ raise SqliteError.new("Seems that there is no sqlite3 executable available") unless system(sqlite_command)
+ end
+ clazz.establish_connection(clazz.name)
+end
+
+make_connection(ActiveRecord::Base, sqlite_test_db)
+make_connection(Course, sqlite_test_db2)
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index e0d8be676a..388fff8fba 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -17,6 +17,12 @@ class Post < ActiveRecord::Base
has_one :last_comment, :class_name => 'Comment', :order => 'id desc'
+ named_scope :with_special_comments, :joins => :comments, :conditions => {:comments => {:type => 'SpecialComment'} }
+ named_scope :with_very_special_comments, :joins => :comments, :conditions => {:comments => {:type => 'VerySpecialComment'} }
+ named_scope :with_post, lambda {|post_id|
+ { :joins => :comments, :conditions => {:comments => {:post_id => post_id} } }
+ }
+
has_many :comments, :order => "body" do
def find_most_recent
find(:first, :order => "id DESC")
diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb
index 39ca1bf42a..08bb24ed03 100644
--- a/activerecord/test/models/topic.rb
+++ b/activerecord/test/models/topic.rb
@@ -4,6 +4,8 @@ class Topic < ActiveRecord::Base
{ :conditions => ['written_on < ?', time] }
}
named_scope :approved, :conditions => {:approved => true}
+ named_scope :rejected, :conditions => {:approved => false}
+
named_scope :by_lifo, :conditions => {:author_name => 'lifo'}
named_scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}}
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 8199cb8fc7..d44faf04cc 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -154,6 +154,11 @@ ActiveRecord::Schema.define do
t.string :name
end
+ create_table :goofy_string_id, :force => true, :id => false do |t|
+ t.string :id, :null => false
+ t.string :info
+ end
+
create_table :items, :force => true do |t|
t.column :name, :integer
end
@@ -252,6 +257,7 @@ ActiveRecord::Schema.define do
t.decimal :world_population, :precision => 10, :scale => 0
t.decimal :my_house_population, :precision => 2, :scale => 0
t.decimal :decimal_number_with_default, :precision => 3, :scale => 2, :default => 2.78
+ t.float :temperature
end
create_table :orders, :force => true do |t|