aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_view/helpers/prototype_helper.rb8
-rw-r--r--activerecord/CHANGELOG12
-rwxr-xr-xactiverecord/lib/active_record/associations.rb37
-rw-r--r--activerecord/lib/active_record/associations/association_proxy.rb11
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb17
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb2
-rwxr-xr-xactiverecord/test/associations_test.rb12
-rwxr-xr-xactiverecord/test/fixtures/company.rb1
-rw-r--r--activerecord/test/fixtures/project.rb1
11 files changed, 82 insertions, 25 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index f9da70771d..5e1c6f58a9 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added form_remote_for (form_for meets form_remote_tag) [DHH]
+
* Update to script.aculo.us 1.5.0_rc6
* More robust relative url root discovery for SCGI compatibility. This solves the 'SCGI routes problem' -- you no longer need to prefix all your routes with the name of the SCGI mountpoint. #3070 [Dave Ringoen]
diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb
index 647b05779a..a0f3a6b42d 100644
--- a/actionpack/lib/action_view/helpers/prototype_helper.rb
+++ b/actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -169,7 +169,13 @@ module ActionView
tag("form", options[:html], true)
end
-
+
+ def form_remote_for(object_name, object, options = {}, &proc)
+ concat(form_remote_tag(options), proc.binding)
+ fields_for(object_name, object, &proc)
+ concat(end_form_tag, proc.binding)
+ end
+
# Works like form_remote_tag, but uses form_for semantics.
def form_remote_for(object_name, object, options = {}, &proc)
concat(form_remote_tag(options), proc.binding)
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 394d9ddf1e..1ffb8480cb 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,17 @@
*SVN*
+* Added option inheritance for find calls on has_and_belongs_to_many and has_many assosociations [DHH]. Example:
+
+ class Post
+ has_many :recent_comments, :class_name => "Comment", :limit => 10, :include => :author
+ end
+
+ post.recent_comments.find(:all) # Uses LIMIT 10 and includes authors
+ post.recent_comments.find(:all, :limit => nil) # Uses no limit but include authors
+ post.recent_comments.find(:all, :limit => nil, :include => nil) # Uses no limit and doesn't include authors
+
+* Added option to specify :group, :limit, :offset, and :select options from find on has_and_belongs_to_many and has_many assosociations [DHH]
+
* MySQL: fixes for the bundled mysql.rb driver. #3160 [Justin Forder]
* SQLServer: fix obscure optimistic locking bug. #3068 [kajism@yahoo.com]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index c50750352f..07dc28ddff 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -131,9 +131,7 @@ module ActiveRecord
# class Account < ActiveRecord::Base
# has_many :people do
# def find_or_create_by_name(name)
- # first_name, *last_name = name.split
- # last_name = last_name.join " "
- #
+ # first_name, last_name = name.split(" ", 2)
# find_or_create_by_first_name_and_last_name(first_name, last_name)
# end
# end
@@ -147,9 +145,7 @@ module ActiveRecord
#
# module FindOrCreateByNameExtension
# def find_or_create_by_name(name)
- # first_name, *last_name = name.split
- # last_name = last_name.join " "
- #
+ # first_name, last_name = name.split(" ", 2)
# find_or_create_by_first_name_and_last_name(first_name, last_name)
# end
# end
@@ -330,6 +326,11 @@ module ActiveRecord
# specified but +:counter_sql+, +:counter_sql+ will be generated by replacing SELECT ... FROM with SELECT COUNT(*) FROM.
# * <tt>:extend</tt> - specify a named module for extending the proxy, see "Association extensions".
# * <tt>:include</tt> - specify second-order associations that should be eager loaded when the collection is loaded.
+ # * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY 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 the first 4 rows.
+ # * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
+ # include the joined columns.
#
# Option examples:
# has_many :comments, :order => "posted_on"
@@ -591,6 +592,11 @@ module ActiveRecord
# with a manual one
# * <tt>:extend</tt> - anonymous module for extending the proxy, see "Association extensions".
# * <tt>:include</tt> - specify second-order associations that should be eager loaded when the collection is loaded.
+ # * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY 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 the first 4 rows.
+ # * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
+ # include the joined columns.
#
# Option examples:
# has_and_belongs_to_many :projects
@@ -872,10 +878,13 @@ module ActiveRecord
def create_has_many_reflection(association_id, options, &extension)
options.assert_valid_keys(
- :foreign_key, :class_name, :exclusively_dependent, :dependent,
- :conditions, :order, :include, :finder_sql, :counter_sql,
- :before_add, :after_add, :before_remove, :after_remove, :extend,
- :group, :as, :through
+ :class_name, :table_name, :foreign_key,
+ :exclusively_dependent, :dependent,
+ :select, :conditions, :include, :order, :group, :limit, :offset,
+ :as, :through,
+ :finder_sql, :counter_sql,
+ :before_add, :after_add, :before_remove, :after_remove,
+ :extend
)
options[:extend] = create_extension_module(association_id, extension) if block_given?
@@ -916,9 +925,11 @@ module ActiveRecord
def create_has_and_belongs_to_many_reflection(association_id, options, &extension)
options.assert_valid_keys(
- :class_name, :table_name, :foreign_key, :association_foreign_key, :conditions, :include,
- :join_table, :finder_sql, :delete_sql, :insert_sql, :order, :uniq, :before_add, :after_add,
- :before_remove, :after_remove, :extend
+ :class_name, :table_name, :join_table, :foreign_key, :association_foreign_key,
+ :select, :conditions, :include, :order, :group, :limit, :offset,
+ :finder_sql, :delete_sql, :insert_sql, :uniq,
+ :before_add, :after_add, :before_remove, :after_remove,
+ :extend
)
options[:extend] = create_extension_module(association_id, extension) if block_given?
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb
index 75f9184aa2..c62c042ab2 100644
--- a/activerecord/lib/active_record/associations/association_proxy.rb
+++ b/activerecord/lib/active_record/associations/association_proxy.rb
@@ -73,6 +73,17 @@ module ActiveRecord
def extract_options_from_args!(args)
@owner.send(:extract_options_from_args!, args)
end
+
+ def merge_options_from_reflection!(options)
+ options.reverse_merge!(
+ :group => @reflection.options[:group],
+ :limit => @reflection.options[:limit],
+ :offset => @reflection.options[:offset],
+ :joins => @reflection.options[:joins],
+ :include => @reflection.options[:include],
+ :select => @reflection.options[:select]
+ )
+ end
private
def method_missing(method, *args, &block)
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 417b0905f4..5b203dcc57 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
@@ -49,6 +49,8 @@ module ActiveRecord
options[:order] = @reflection.options[:order]
end
+ merge_options_from_reflection!(options)
+
# Pass through args exactly as we received them.
args << options
@reflection.klass.find(*args)
@@ -88,7 +90,7 @@ module ActiveRecord
if @reflection.options[:finder_sql]
records = @reflection.klass.find_by_sql(@finder_sql)
else
- records = find(:all, :include => @reflection.options[:include])
+ records = find(:all)
end
@reflection.options[:uniq] ? uniq(records) : records
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index f4a08420b7..5a29ffaa30 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -77,6 +77,8 @@ module ActiveRecord
options[:order] = @reflection.options[:order]
end
+ merge_options_from_reflection!(options)
+
# Pass through args exactly as we received them.
args << options
@reflection.klass.find(*args)
@@ -107,14 +109,7 @@ module ActiveRecord
if @reflection.options[:finder_sql]
@reflection.klass.find_by_sql(@finder_sql)
else
- @reflection.klass.find(:all,
- :conditions => @finder_sql,
- :order => @reflection.options[:order],
- :limit => @reflection.options[:limit],
- :joins => @reflection.options[:joins],
- :include => @reflection.options[:include],
- :group => @reflection.options[:group]
- )
+ find(:all)
end
end
@@ -129,6 +124,10 @@ module ActiveRecord
@target = [] and loaded if count == 0
+ if @reflection.options[:limit]
+ count = [ @reflection.options[:limit], count ].min
+ end
+
return count
end
@@ -171,7 +170,7 @@ module ActiveRecord
"#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +
"#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = '#{ActiveRecord::Base.send(:class_name_of_active_record_descendant, @owner.class).to_s}'"
@finder_sql << " AND (#{interpolate_sql(@conditions)})" if @conditions
-
+
else
@finder_sql = "#{@reflection.klass.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}"
@finder_sql << " AND (#{interpolate_sql(@conditions)})" if @conditions
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 9ecd6f059e..f6ac03a3e6 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -16,6 +16,8 @@ module ActiveRecord
options[:order] = @reflection.options[:order]
end
+ merge_options_from_reflection!(options)
+
# Pass through args exactly as we received them.
args << options
@reflection.klass.find(*args)
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 735f012afe..e088432da8 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -281,6 +281,12 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_equal 2, Firm.find(:first).clients.length
end
+ def test_find_many_with_merged_options
+ assert_equal 1, companies(:first_firm).limited_clients.size
+ assert_equal 1, companies(:first_firm).limited_clients.find(:all).size
+ assert_equal 2, companies(:first_firm).limited_clients.find(:all, :limit => nil).size
+ end
+
def test_triple_equality
assert !(Array === Firm.find(:first).clients)
assert Firm.find(:first).clients === Array
@@ -670,7 +676,6 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_equal num_accounts - 1, Account.count
end
-
def test_depends_and_nullify
num_accounts = Account.count
num_companies = Company.count
@@ -1342,6 +1347,11 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id.to_s), "SQL find"
end
+ def test_find_with_merged_options
+ assert_equal 1, projects(:active_record).limited_developers.size
+ assert_equal 1, projects(:active_record).limited_developers.find(:all).size
+ assert_equal 2, projects(:active_record).limited_developers.find(:all, :limit => nil).size
+ end
def test_new_with_values_in_collection
jamis = DeveloperForProjectWithAfterCreateHook.find_by_name('Jamis')
diff --git a/activerecord/test/fixtures/company.rb b/activerecord/test/fixtures/company.rb
index 6d33d4e1a8..d4a68900fe 100755
--- a/activerecord/test/fixtures/company.rb
+++ b/activerecord/test/fixtures/company.rb
@@ -14,6 +14,7 @@ class Firm < Company
has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
has_many :dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => true
has_many :exclusively_dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :exclusively_dependent => true
+ has_many :limited_clients, :class_name => "Client", :order => "id", :limit => 1
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",
diff --git a/activerecord/test/fixtures/project.rb b/activerecord/test/fixtures/project.rb
index f8519f1b8c..c1aa4145f9 100644
--- a/activerecord/test/fixtures/project.rb
+++ b/activerecord/test/fixtures/project.rb
@@ -1,5 +1,6 @@
class Project < ActiveRecord::Base
has_and_belongs_to_many :developers, :uniq => true
+ has_and_belongs_to_many :limited_developers, :class_name => "Developer", :limit => 1
has_and_belongs_to_many :developers_named_david, :class_name => "Developer", :conditions => "name = 'David'", :uniq => true
has_and_belongs_to_many :salaried_developers, :class_name => "Developer", :conditions => "salary > 0"
has_and_belongs_to_many :developers_with_finder_sql, :class_name => "Developer", :finder_sql => 'SELECT t.*, j.* FROM developers_projects j, developers t WHERE t.id = j.developer_id AND j.project_id = #{id}'