aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/association.rb6
-rw-r--r--activerecord/lib/active_record/associations/builder/association.rb21
-rw-r--r--activerecord/lib/active_record/associations/builder/collection_association.rb8
3 files changed, 22 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index e75003f261..624942d08a 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -82,7 +82,7 @@ module ActiveRecord
end
def scoped
- target_scope.merge(association_scope)
+ target_scope.merge(association_scope).merge(reflection_scope)
end
# The scope for this association.
@@ -101,6 +101,10 @@ module ActiveRecord
@association_scope = nil
end
+ def reflection_scope
+ reflection.scope && klass.instance_exec(&reflection.scope)
+ end
+
# Set the inverse association, if possible
def set_inverse_instance(record)
if record && invertible_for?(record)
diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb
index 9a6896dd55..f1a9f254b1 100644
--- a/activerecord/lib/active_record/associations/builder/association.rb
+++ b/activerecord/lib/active_record/associations/builder/association.rb
@@ -6,14 +6,23 @@ module ActiveRecord::Associations::Builder
# Set by subclasses
class_attribute :macro
- attr_reader :model, :name, :options, :reflection
+ attr_reader :model, :name, :scope, :options, :reflection
- def self.build(model, name, options)
- new(model, name, options).build
+ def self.build(*args, &block)
+ new(*args, &block).build
end
- def initialize(model, name, options)
- @model, @name, @options = model, name, options
+ def initialize(model, name, scope, options)
+ @model = model
+ @name = name
+
+ if options
+ @scope = scope
+ @options = options
+ else
+ @scope = nil
+ @options = scope
+ end
end
def mixin
@@ -22,7 +31,7 @@ module ActiveRecord::Associations::Builder
def build
validate_options
- reflection = model.create_reflection(self.class.macro, name, options, model)
+ reflection = model.create_reflection(self.class.macro, name, scope, options, model)
define_accessors
reflection
end
diff --git a/activerecord/lib/active_record/associations/builder/collection_association.rb b/activerecord/lib/active_record/associations/builder/collection_association.rb
index 768f70b6c9..50836b496f 100644
--- a/activerecord/lib/active_record/associations/builder/collection_association.rb
+++ b/activerecord/lib/active_record/associations/builder/collection_association.rb
@@ -9,12 +9,8 @@ module ActiveRecord::Associations::Builder
attr_reader :block_extension
- def self.build(model, name, options, &extension)
- new(model, name, options, &extension).build
- end
-
- def initialize(model, name, options, &extension)
- super(model, name, options)
+ def initialize(*args, &extension)
+ super(*args)
@block_extension = extension
end