aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-17 23:22:11 +0530
committerPratik Naik <pratiknaik@gmail.com>2010-01-17 23:22:11 +0530
commitdca3de3bc766175f49b56202246d3625c58fd763 (patch)
tree2f6ed2fed53a8fc57b6e716b59ee2c6ebaa6f40f /activerecord/lib
parentf0cde5be541e1f3877a15fb5d39c87a487a14381 (diff)
downloadrails-dca3de3bc766175f49b56202246d3625c58fd763.tar.gz
rails-dca3de3bc766175f49b56202246d3625c58fd763.tar.bz2
rails-dca3de3bc766175f49b56202246d3625c58fd763.zip
Make relations work as scopes
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/named_scope.rb26
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb10
2 files changed, 28 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index 5a1cb7e769..30e75534dd 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -106,7 +106,7 @@ module ActiveRecord
scopes[name] = lambda do |parent_scope, *args|
Scope.new(parent_scope, case options
- when Hash
+ when Hash, Relation
options
when Proc
options.call(*args)
@@ -132,13 +132,21 @@ module ActiveRecord
delegate :scopes, :with_scope, :scoped_methods, :unscoped, :to => :proxy_scope
def initialize(proxy_scope, options, &block)
- options ||= {}
- [options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
extend Module.new(&block) if block_given?
+
+ options ||= {}
+ if options.is_a?(Hash)
+ Array.wrap(options[:extend]).each {|extension| extend extension }
+ @proxy_options = options.except(:extend)
+ else
+ @proxy_options = options
+ end
+
unless Scope === proxy_scope
@current_scoped_methods_when_defined = proxy_scope.send(:current_scoped_methods)
end
- @proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
+
+ @proxy_scope = proxy_scope
end
def reload
@@ -193,7 +201,13 @@ module ActiveRecord
protected
def relation
- @relation ||= unscoped.apply_finder_options(proxy_options)
+ @relation ||= begin
+ if proxy_options.is_a?(Hash)
+ unscoped.apply_finder_options(proxy_options)
+ else
+ unscoped.merge(proxy_options)
+ end
+ end
end
def proxy_found
@@ -201,6 +215,7 @@ module ActiveRecord
end
private
+
def method_missing(method, *args, &block)
if scopes.include?(method)
scopes[method].call(self, *args)
@@ -221,6 +236,7 @@ module ActiveRecord
def load_found
@found = find(:all)
end
+
end
end
end
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index 953ea5ea1c..2979f4b82d 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -19,7 +19,10 @@ module ActiveRecord
raise ArgumentError, "Cannot merge a #{r.klass.name}(##{r.klass.object_id}) relation with #{@klass.name}(##{@klass.object_id}) relation"
end
- merged_relation = spawn.eager_load(r.eager_load_values).preload(r.preload_values).includes(r.includes_values)
+ merged_relation = spawn
+ return merged_relation unless r
+
+ merged_relation = merged_relation.eager_load(r.eager_load_values).preload(r.preload_values).includes(r.includes_values)
merged_relation.readonly_value = r.readonly_value unless r.readonly_value.nil?
merged_relation.limit_value = r.limit_value if r.limit_value.present?
@@ -94,9 +97,10 @@ module ActiveRecord
:order, :select, :readonly, :group, :having, :from, :lock ]
def apply_finder_options(options)
- options.assert_valid_keys(VALID_FIND_OPTIONS)
-
relation = spawn
+ return relation unless options
+
+ options.assert_valid_keys(VALID_FIND_OPTIONS)
relation = relation.joins(options[:joins]).
where(options[:conditions]).