aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorMikel Lindsaar <raasdnil@gmail.com>2010-01-23 19:45:25 +1100
committerMikel Lindsaar <raasdnil@gmail.com>2010-01-23 19:45:25 +1100
commit1ea84c38ea5b40642f3dcdb5ffca1c6fc9cc604d (patch)
tree750e10199be244bb5676ea615dbf49dee38b7fd4 /activerecord
parentbd24c75fc619fa80af03662cc4dd18fc9b70cafb (diff)
parent8ff2fb6f3aa6140f5a8bd018d5919a8a1e707cda (diff)
downloadrails-1ea84c38ea5b40642f3dcdb5ffca1c6fc9cc604d.tar.gz
rails-1ea84c38ea5b40642f3dcdb5ffca1c6fc9cc604d.tar.bz2
rails-1ea84c38ea5b40642f3dcdb5ffca1c6fc9cc604d.zip
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/base.rb5
-rw-r--r--activerecord/lib/active_record/relation.rb4
-rw-r--r--activerecord/test/cases/method_scoping_test.rb8
-rw-r--r--activerecord/test/cases/named_scope_test.rb9
-rw-r--r--activerecord/test/cases/relations_test.rb5
-rw-r--r--activerecord/test/models/post.rb5
6 files changed, 32 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index f1b2b3b979..12feef4849 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1236,7 +1236,7 @@ module ActiveRecord #:nodoc:
end
def construct_finder_arel(options = {}, scope = nil)
- relation = unscoped.apply_finder_options(options)
+ relation = options.is_a?(Hash) ? unscoped.apply_finder_options(options) : unscoped.merge(options)
relation = scope.merge(relation) if scope
relation
end
@@ -1450,7 +1450,8 @@ module ActiveRecord #:nodoc:
end
def scoped_methods #:nodoc:
- Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping.dup
+ key = :"#{self}_scoped_methods"
+ Thread.current[key] = Thread.current[key].presence || self.default_scoping.dup
end
def current_scoped_methods #:nodoc:
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 04b85119cb..1a96cdad17 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -32,7 +32,7 @@ module ActiveRecord
end
def respond_to?(method, include_private = false)
- return true if arel.respond_to?(method, include_private) || Array.method_defined?(method)
+ return true if arel.respond_to?(method, include_private) || Array.method_defined?(method) || @klass.respond_to?(method, include_private)
if match = DynamicFinderMatch.match(method)
return true if @klass.send(:all_attributes_exists?, match.attribute_names)
@@ -301,6 +301,8 @@ module ActiveRecord
def method_missing(method, *args, &block)
if Array.method_defined?(method)
to_a.send(method, *args, &block)
+ elsif @klass.respond_to?(method)
+ @klass.send(:with_scope, self) { @klass.send(method, *args, &block) }
elsif arel.respond_to?(method)
arel.send(method, *args, &block)
elsif match = DynamicFinderMatch.match(method)
diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb
index fbd1adf088..1081aa40a9 100644
--- a/activerecord/test/cases/method_scoping_test.rb
+++ b/activerecord/test/cases/method_scoping_test.rb
@@ -588,7 +588,7 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase
end
class DefaultScopingTest < ActiveRecord::TestCase
- fixtures :developers
+ fixtures :developers, :posts
def test_default_scope
expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary }
@@ -657,6 +657,12 @@ class DefaultScopingTest < ActiveRecord::TestCase
received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary }
assert_equal expected, received
end
+
+ def test_default_scope_using_relation
+ posts = PostWithComment.scoped
+ assert_equal 2, posts.count
+ assert_equal posts(:thinking), posts.first
+ end
end
=begin
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb
index 2c34ab787d..894d96346e 100644
--- a/activerecord/test/cases/named_scope_test.rb
+++ b/activerecord/test/cases/named_scope_test.rb
@@ -380,6 +380,15 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_deprecated('named_scope has been deprecated') { Topic.named_scope :deprecated_named_scope }
end
+ def test_named_scopes_on_relations
+ # Topic.replied
+ approved_topics = Topic.scoped.approved.order('id DESC')
+ assert_equal topics(:fourth), approved_topics.first
+
+ replied_approved_topics = approved_topics.replied
+ assert_equal topics(:third), replied_approved_topics.first
+ end
+
def test_index_on_named_scope
approved = Topic.approved.order('id ASC')
assert_equal topics(:second), approved[0]
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index d34c9b4895..1e345399f5 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -164,6 +164,11 @@ class RelationTest < ActiveRecord::TestCase
end
end
+ def test_respond_to_class_methods_and_named_scopes
+ assert DeveloperOrderedBySalary.scoped.respond_to?(:all_ordered_by_name)
+ assert Topic.scoped.respond_to?(:by_lifo)
+ end
+
def test_find_with_readonly_option
Developer.scoped.each { |d| assert !d.readonly? }
Developer.scoped.readonly.each { |d| assert d.readonly? }
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index f48b35486c..704313649a 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -100,3 +100,8 @@ end
class SubStiPost < StiPost
self.table_name = Post.table_name
end
+
+class PostWithComment < ActiveRecord::Base
+ self.table_name = 'posts'
+ default_scope where("posts.comments_count > 0").order("posts.comments_count ASC")
+end