2013-04-09 19:20:49 -04:00
|
|
|
require 'active_support/per_thread_registry'
|
|
|
|
|
2011-12-15 15:07:41 -05:00
|
|
|
module ActiveRecord
|
|
|
|
module Scoping
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2011-12-15 15:35:04 -05:00
|
|
|
included do
|
|
|
|
include Default
|
|
|
|
include Named
|
|
|
|
end
|
|
|
|
|
2011-12-15 15:07:41 -05:00
|
|
|
module ClassMethods
|
|
|
|
def current_scope #:nodoc:
|
2013-04-08 14:12:52 -04:00
|
|
|
ScopeRegistry.value_for(:current_scope, base_class.to_s)
|
2011-12-15 15:07:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_scope=(scope) #:nodoc:
|
2013-04-08 14:12:52 -04:00
|
|
|
ScopeRegistry.set_value_for(:current_scope, base_class.to_s, scope)
|
2011-12-15 15:07:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def populate_with_current_scope_attributes
|
|
|
|
return unless self.class.scope_attributes?
|
|
|
|
|
|
|
|
self.class.scope_attributes.each do |att,value|
|
|
|
|
send("#{att}=", value) if respond_to?("#{att}=")
|
|
|
|
end
|
|
|
|
end
|
2013-04-08 02:56:21 -04:00
|
|
|
|
|
|
|
# This class stores the +:current_scope+ and +:ignore_default_scope+ values
|
|
|
|
# for different classes. The registry is stored as a thread local, which is
|
|
|
|
# accessed through +ScopeRegistry.current+.
|
|
|
|
#
|
|
|
|
# This class allows you to store and get the scope values on different
|
|
|
|
# classes and different types of scopes. For example, if you are attempting
|
|
|
|
# to get the current_scope for the +Board+ model, then you would use the
|
|
|
|
# following code:
|
|
|
|
#
|
2013-04-13 11:08:00 -04:00
|
|
|
# registry = ActiveRecord::Scoping::ScopeRegistry
|
2013-04-08 02:56:21 -04:00
|
|
|
# registry.set_value_for(:current_scope, "Board", some_new_scope)
|
|
|
|
#
|
|
|
|
# Now when you run:
|
|
|
|
#
|
|
|
|
# registry.value_for(:current_scope, "Board")
|
|
|
|
#
|
2013-04-08 14:12:52 -04:00
|
|
|
# You will obtain whatever was defined in +some_new_scope+. The +value_for+
|
|
|
|
# and +set_value_for+ methods are delegated to the current +ScopeRegistry+
|
|
|
|
# object, so the above example code can also be called as:
|
|
|
|
#
|
|
|
|
# ActiveRecord::Scoping::ScopeRegistry.set_value_for(:current_scope,
|
|
|
|
# "Board", some_new_scope)
|
2013-04-08 02:56:21 -04:00
|
|
|
class ScopeRegistry # :nodoc:
|
2013-04-09 19:20:49 -04:00
|
|
|
extend ActiveSupport::PerThreadRegistry
|
2013-04-08 14:12:52 -04:00
|
|
|
|
2013-04-08 02:56:21 -04:00
|
|
|
VALID_SCOPE_TYPES = [:current_scope, :ignore_default_scope]
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@registry = Hash.new { |hash, key| hash[key] = {} }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Obtains the value for a given +scope_name+ and +variable_name+.
|
|
|
|
def value_for(scope_type, variable_name)
|
|
|
|
raise_invalid_scope_type!(scope_type)
|
|
|
|
@registry[scope_type][variable_name]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the +value+ for a given +scope_type+ and +variable_name+.
|
|
|
|
def set_value_for(scope_type, variable_name, value)
|
|
|
|
raise_invalid_scope_type!(scope_type)
|
|
|
|
@registry[scope_type][variable_name] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def raise_invalid_scope_type!(scope_type)
|
|
|
|
if !VALID_SCOPE_TYPES.include?(scope_type)
|
|
|
|
raise ArgumentError, "Invalid scope type '#{scope_type}' sent to the registry. Scope types must be included in VALID_SCOPE_TYPES"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-12-15 15:07:41 -05:00
|
|
|
end
|
|
|
|
end
|