Permalink
Browse files

Added counter_sql option for has_many associations [bitsweat]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@68 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 465e0c0 commit ea759cb76161a7a0003c3cd3b38a8a125a99af3c @dhh dhh committed Dec 7, 2004
@@ -1,5 +1,10 @@
*CVS*
+* Added counter_sql option for has_many associations [bitsweat]. Documentation:
+
+ <tt>:counter_sql</tt> - specify a complete SQL statement to fetch the size of the association. If +:finder_sql+ is
+ specified but +:counter_sql+, +:counter_sql+ will be generated by replacing SELECT ... FROM with SELECT COUNT(*) FROM.
+
* Fixed that methods wrapped in callbacks still return their original result #260 [bitsweat]
* Fixed the Inflector to handle the movie/movies pair correctly #261 [Scott Baron]
@@ -166,6 +166,8 @@ module ClassMethods
# May not be set if :dependent is also set.
# * <tt>:finder_sql</tt> - specify a complete SQL statement to fetch the association. This is a good way to go for complex
# associations that depends on multiple tables. Note: When this option is used, +find_in_collection+ is _not_ added.
+ # * <tt>:counter_sql</tt> - specify a complete SQL statement to fetch the size of the association. If +:finder_sql+ is
+ # specified but +:counter_sql+, +:counter_sql+ will be generated by replacing SELECT ... FROM with SELECT COUNT(*) FROM.
#
# Option examples:
# has_many :comments, :order => "posted_on"
@@ -177,7 +179,7 @@ module ClassMethods
# 'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' +
# 'ORDER BY p.first_name'
def has_many(association_id, options = {})
- validate_options([ :foreign_key, :class_name, :exclusively_dependent, :dependent, :conditions, :order, :finder_sql ], options.keys)
+ validate_options([ :foreign_key, :class_name, :exclusively_dependent, :dependent, :conditions, :order, :finder_sql, :counter_sql ], options.keys)
association_name, association_class_name, association_class_primary_key_name =
associate_identification(association_id, options[:class_name], options[:foreign_key])
@@ -7,9 +7,15 @@ def initialize(owner, association_name, association_class_name, association_clas
if options[:finder_sql]
@finder_sql = interpolate_sql(options[:finder_sql])
- @counter_sql = @finder_sql.gsub(/SELECT (.*) FROM/i, "SELECT COUNT(*) FROM")
else
- @finder_sql = "#{@association_class_primary_key_name} = '#{@owner.id}' #{@conditions ? " AND " + interpolate_sql(@conditions) : ""}"
+ @finder_sql = "#{@association_class_primary_key_name} = '#{@owner.id}' #{@conditions ? " AND " + interpolate_sql(@conditions) : ""}"
+ end
+
+ if options[:counter_sql]
+ @counter_sql = interpolate_sql(options[:counter_sql])
+ elsif options[:finder_sql]
+ @counter_sql = options[:counter_sql] = @finder_sql.gsub(/SELECT (.*) FROM/i, "SELECT COUNT(*) FROM")
+ else
@counter_sql = "#{@association_class_primary_key_name} = '#{@owner.id}'#{@conditions ? " AND " + interpolate_sql(@conditions) : ""}"
end
end
@@ -70,21 +76,21 @@ def find_all_records
@association_class.find_all(@finder_sql, @options[:order] ? @options[:order] : nil)
end
end
-
+
def count_records
if has_cached_counter?
@owner.send(:read_attribute, cached_counter_attribute_name)
- elsif @options[:finder_sql]
+ elsif @options[:counter_sql]
@association_class.count_by_sql(@counter_sql)
else
@association_class.count(@counter_sql)
end
end
-
+
def has_cached_counter?
@owner.attribute_present?(cached_counter_attribute_name)
end
-
+
def cached_counter_attribute_name
"#{@association_name}_count"
end
@@ -178,6 +178,11 @@ def test_finding_using_sql
assert_equal 1, Firm.find_first.clients_using_sql.size
end
+ def test_counting_using_sql
+ assert_equal 1, Firm.find_first.clients_using_counter_sql.size
+ assert_equal 0, Firm.find_first.clients_using_zero_counter_sql.size
+ end
+
def test_find_all
assert_equal 2, Firm.find_first.clients.find_all("type = 'Client'").length
assert_equal 1, Firm.find_first.clients.find_all("name = 'Summit'").length
@@ -25,7 +25,7 @@ def setup
def test_has_many_find
assert_equal 2, Firm.find_first.clients.length
end
-
+
def test_has_many_orders
assert_equal "Summit", Firm.find_first.clients.first.name
end
@@ -37,7 +37,7 @@ def test_has_many_class_name
def test_has_many_foreign_key
assert_equal "Microsoft", Firm.find_first.clients_of_firm.first.name
end
-
+
def test_has_many_conditions
assert_equal "Microsoft", Firm.find_first.clients_like_ms.first.name
end
@@ -48,7 +48,11 @@ def test_has_many_sql
assert_equal 1, firm.clients_using_sql_count
assert_equal 1, Firm.find_first.clients_using_sql_count
end
-
+
+ def test_has_many_counter_sql
+ assert_equal 1, Firm.find_first.clients_using_counter_sql_count
+ end
+
def test_has_many_queries
assert Firm.find_first.has_clients?
firm = Firm.find_first
@@ -75,7 +79,7 @@ def test_has_many_dependence_with_transaction_support_on_failure
assert_equal 2, Client.find_all.length
end
-
+
def test_has_one_dependence
firm = Firm.find(1)
assert firm.has_account?
@@ -95,7 +99,7 @@ def test_belongs_to
assert Client.find(3).has_firm?, "Microsoft should have a firm"
# assert !Company.find(1).has_firm?, "37signals shouldn't have a firm"
end
-
+
def test_belongs_to_with_different_class_name
assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
assert Company.find(3).has_firm_with_other_name?, "Microsoft should have a firm"
@@ -106,12 +110,11 @@ def test_belongs_to_with_condition
assert Company.find(3).has_firm_with_condition?, "Microsoft should have a firm"
end
-
def test_belongs_to_equality
assert Company.find(3).firm?(Company.find(1)), "Microsoft should have 37signals as firm"
assert_raises(RuntimeError) { !Company.find(3).firm?(Company.find(3)) } # "Summit shouldn't have itself as firm"
end
-
+
def test_has_one
assert @signals37.account?(Account.find(1))
assert_equal Account.find(1).credit_limit, @signals37.account.credit_limit
@@ -128,20 +131,20 @@ def test_has_many_dependence_on_account
@signals37.destroy
assert_equal 1, Account.find_all.length
end
-
+
def test_find_in
assert_equal Client.find(2).name, @signals37.find_in_clients(2).name
assert_raises(ActiveRecord::RecordNotFound) { @signals37.find_in_clients(6) }
end
-
+
def test_force_reload
firm = Firm.new
firm.save
firm.clients.each {|c|} # forcing to load all clients
assert firm.clients.empty?, "New firm shouldn't have client objects"
assert !firm.has_clients?, "New firm shouldn't have clients"
assert_equal 0, firm.clients_count, "New firm should have 0 clients"
-
+
client = Client.new("firm_id" => firm.id)
client.save
@@ -153,7 +156,7 @@ def test_force_reload
assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response"
assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count"
end
-
+
def test_included_in_collection
assert @signals37.clients.include?(Client.find(2))
end
@@ -167,35 +170,35 @@ def test_build_to_collection
assert new_client.firm?(@signals37)
assert_equal 2, @signals37.clients_of_firm_count(true)
end
-
+
def test_create_in_collection
assert_equal @signals37.create_in_clients_of_firm("name" => "Another Client"), @signals37.clients_of_firm(true).last
end
-
+
def test_succesful_build_association
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save
-
+
account = firm.build_account("credit_limit" => 1000)
assert account.save
assert_equal account, firm.account
end
-
+
def test_failing_build_association
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save
-
+
account = firm.build_account
assert !account.save
assert_equal "can't be empty", account.errors.on("credit_limit")
end
-
+
def test_create_association
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save
assert_equal firm.create_account("credit_limit" => 1000), firm.account
end
-
+
def test_has_and_belongs_to_many
david = Developer.find(1)
assert david.has_projects?
@@ -212,15 +215,15 @@ def test_has_and_belongs_to_many_removing
active_record = Project.find(1)
david.remove_projects(active_record)
-
+
assert_equal 1, david.projects_count
- assert_equal 1, active_record.developers_count
+ assert_equal 1, active_record.developers_count
end
def test_has_and_belongs_to_many_zero
david = Developer.find(1)
david.remove_projects(Project.find_all)
-
+
assert_equal 0, david.projects_count
assert !david.has_projects?
end
@@ -230,29 +233,29 @@ def test_has_and_belongs_to_many_adding
action_controller = Project.find(2)
jamis.add_projects(action_controller)
-
+
assert_equal 2, jamis.projects_count
- assert_equal 2, action_controller.developers_count
+ assert_equal 2, action_controller.developers_count
end
def test_has_and_belongs_to_many_adding_from_the_project
jamis = Developer.find(2)
action_controller = Project.find(2)
action_controller.add_developers(jamis)
-
+
assert_equal 2, jamis.projects_count
assert_equal 2, action_controller.developers_count
end
-
+
def test_has_and_belongs_to_many_adding_a_collection
aridridel = Developer.new("name" => "Aridridel")
aridridel.save
-
+
aridridel.add_projects([ Project.find(1), Project.find(2) ])
assert_equal 2, aridridel.projects_count
end
-
+
def test_belongs_to_counter
topic = Topic.create("title" => "Apple", "content" => "hello world")
assert_equal 0, topic.send(:read_attribute, "replies_count"), "No replies yet"
@@ -263,14 +266,14 @@ def test_belongs_to_counter
reply.destroy
assert_equal 0, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply deleted"
end
-
+
def test_natural_assignment_of_has_one
apple = Firm.create("name" => "Apple")
citibank = Account.create("credit_limit" => 10)
apple.account = citibank
assert_equal apple.id, citibank.firm_id
end
-
+
def test_natural_assignment_of_belongs_to
apple = Firm.create("name" => "Apple")
citibank = Account.create("credit_limit" => 10)
@@ -280,34 +283,33 @@ def test_natural_assignment_of_belongs_to
def test_natural_assignment_of_has_many
apple = Firm.create("name" => "Apple")
- natural = Client.new("name" => "Natural Company")
+ natural = Client.create("name" => "Natural Company")
apple.clients << natural
assert_equal apple.id, natural.firm_id
assert_equal Client.find(natural.id), Firm.find(apple.id).clients.find { |c| c.id == natural.id }
apple.clients.delete natural
assert_nil Firm.find(apple.id).clients.find { |c| c.id == natural.id }
end
-
def test_natural_adding_of_has_and_belongs_to_many
rails = Project.create("name" => "Rails")
ap = Project.create("name" => "Action Pack")
john = Developer.create("name" => "John")
mike = Developer.create("name" => "Mike")
- rails.developers << john
- rails.developers << mike
-
+ rails.developers << john
+ rails.developers << mike
+
assert_equal Developer.find(john.id), Project.find(rails.id).developers.find { |d| d.id == john.id }
assert_equal Developer.find(mike.id), Project.find(rails.id).developers.find { |d| d.id == mike.id }
assert_equal Project.find(rails.id), Developer.find(mike.id).projects.find { |p| p.id == rails.id }
assert_equal Project.find(rails.id), Developer.find(john.id).projects.find { |p| p.id == rails.id }
ap.developers << john
assert_equal Developer.find(john.id), Project.find(ap.id).developers.find { |d| d.id == john.id }
assert_equal Project.find(ap.id), Developer.find(john.id).projects.find { |p| p.id == ap.id }
-
+
ap.developers.delete john
assert_nil Project.find(ap.id).developers.find { |d| d.id == john.id }
- assert_nil Developer.find(john.id).projects.find { |p| p.id == ap.id }
+ assert_nil Developer.find(john.id).projects.find { |p| p.id == ap.id }
end
def test_storing_in_pstore
@@ -9,6 +9,12 @@ class Firm < Company
has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
+ has_many :clients_using_counter_sql, :class_name => "Client",
+ :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}',
+ :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = #{id}'
+ has_many :clients_using_zero_counter_sql, :class_name => "Client",
+ :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}',
+ :counter_sql => 'SELECT 0 FROM companies WHERE client_of = #{id}'
has_one :account, :dependent => true
end

0 comments on commit ea759cb

Please sign in to comment.