Permalink
Browse files

r2915@asus: jeremy | 2005-11-06 05:02:53 -0800

 Rename Base.constrain to Base.with_scope so it doesn't conflict with existing concept of database constraints.  Make scoping more robust: uniform method => parameters, validated method names and supported finder parameters, raise exception on nested scopes.


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2888 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 6c55727 commit 390e6d246ceb76ead8dbcf7b87591f51fc316082 @jeremy jeremy committed Nov 6, 2005
View
@@ -1,5 +1,14 @@
*SVN*
+* Rename Base.constrain to Base.with_scope so it doesn't conflict with existing concept of database constraints. Make scoping more robust: uniform method => parameters, validated method names and supported finder parameters, raise exception on nested scopes. [Jeremy Kemper] Example:
+
+ Comment.with_scope(:find => { :conditions => 'active=true' }, :create => { :post_id => 5 }) do
+ # Find where name = ? and active=true
+ Comment.find :all, :conditions => ['name = ?', name]
+ # Create comment associated with :post_id
+ Comment.create :body => "Hello world"
+ end
+
* Fixed that SQL Server should ignore :size declarations on anything but integer and string in the agnostic schema representation #2756 [Ryan Tomayko]
* Added constrain scoping for creates using a hash of attributes bound to the :creation key [DHH]. Example:
@@ -80,7 +80,7 @@ def method_missing(method, *args, &block)
if @target.respond_to?(method) || (!@association_class.respond_to?(method) && Class.respond_to?(method))
super
else
- @association_class.constrain(:conditions => @finder_sql, :joins => @join_sql, :readonly => false) do
+ @association_class.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
@association_class.send(method, *args, &block)
end
end
@@ -89,11 +89,16 @@ def method_missing(method, *args, &block)
if @target.respond_to?(method) || (!@association_class.respond_to?(method) && Class.respond_to?(method))
super
else
- @association_class.constrain(
+ @association_class.with_scope(
+ :find => {
:conditions => @finder_sql,
:joins => @join_sql,
- :readonly => false,
- :creation => { @association_class_primary_key_name => @owner.id }) do
+ :readonly => false
+ },
+ :create => {
+ @association_class_primary_key_name => @owner.id
+ }
+ ) do
@association_class.send(method, *args, &block)
end
end
@@ -394,11 +394,11 @@ class << self # Class methods
def find(*args)
options = extract_options_from_args!(args)
- # Inherit :readonly from scope_constraints if set. Otherwise,
+ # Inherit :readonly from finder scope if set. Otherwise,
# if :joins is not blank then :readonly defaults to true.
unless options.has_key?(:readonly)
- if scope_constraints.has_key?(:readonly)
- options[:readonly] = scope_constraints[:readonly]
+ if scoped?(:find, :readonly)
+ options[:readonly] = scope(:find, :readonly)
elsif !options[:joins].blank?
options[:readonly] = true
end
@@ -460,7 +460,7 @@ def create(attributes = nil)
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr) }
else
- attributes.reverse_merge!(scope_constraints[:creation]) if scope_constraints[:creation]
+ attributes.reverse_merge!(scope(:create)) if scoped?(:create)
object = new(attributes)
object.save
@@ -839,23 +839,37 @@ def silence
ensure
logger.level = old_logger_level if logger
end
-
- # Add constraints to all queries to the same model in the given block.
- # Currently supported constraints are <tt>:conditions</tt>, <tt>:joins</tt>,
- # <tt>:offset</tt>, and <tt>:limit</tt>
+
+ # Scope parameters to method calls within the block. Takes a hash of method_name => parameters hash.
+ # method_name may be :find or :create.
+ # :find parameters may include the <tt>:conditions</tt>, <tt>:joins</tt>,
+ # <tt>:offset</tt>, <tt>:limit</tt>, and <tt>:readonly</tt> options.
+ # :create parameters are an attributes hash.
#
- # Article.constrain(:conditions => "blog_id = 1") do
+ # Article.with_scope(:find => { :conditions => "blog_id = 1" }, :create => { :blog_id => 1 }) do
# Article.find(1) # => SELECT * from articles WHERE blog_id = 1 AND id = 1
+ # a = Article.create(1)
+ # a.blog_id == 1
# end
- def constrain(options = {})
- options = options.dup
- options[:readonly] = true if !options[:joins].blank? && !options.has_key?(:readonly)
+ def with_scope(method_scoping = {})
+ # Dup first and second level of hash (method and params).
+ method_scoping = method_scoping.inject({}) do |hash, (method, params)|
+ hash[method] = params.dup
+ hash
+ end
+
+ method_scoping.assert_valid_keys [:find, :create]
+ if f = method_scoping[:find]
+ f.assert_valid_keys [:conditions, :joins, :offset, :limit, :readonly]
+ f[:readonly] = true if !f[:joins].blank? && !f.has_key?(:readonly)
+ end
- self.scope_constraints = options
+ raise ArgumentError, "Nested scopes are not yet supported: #{scoped_methods.inspect}" unless scoped_methods.nil?
- yield if block_given?
+ self.scoped_methods = method_scoping
+ yield
ensure
- self.scope_constraints = nil
+ self.scoped_methods = nil
end
# Overwrite the default class equality method to provide support for association proxies.
@@ -918,23 +932,23 @@ def construct_finder_sql(options)
end
def add_limit!(sql, options)
- options[:limit] ||= scope_constraints[:limit] if scope_constraints[:limit]
- options[:offset] ||= scope_constraints[:offset] if scope_constraints[:offset]
+ options[:limit] ||= scope(:find, :limit)
+ options[:offset] ||= scope(:find, :offset)
connection.add_limit_offset!(sql, options)
end
-
+
def add_joins!(sql, options)
- join = scope_constraints[:joins] || options[:joins]
+ join = scope(:find, :joins) || options[:joins]
sql << " #{join} " if join
end
-
+
# Adds a sanitized version of +conditions+ to the +sql+ string. Note that the passed-in +sql+ string is changed.
def add_conditions!(sql, conditions)
- condition_segments = [scope_constraints[:conditions]]
- condition_segments << sanitize_sql(conditions) unless conditions.nil?
- condition_segments << type_condition unless descends_from_active_record?
- condition_segments.compact!
- sql << "WHERE (#{condition_segments.join(") AND (")}) " unless condition_segments.empty?
+ segments = [scope(:find, :conditions)]
+ segments << sanitize_sql(conditions) unless conditions.nil?
+ segments << type_condition unless descends_from_active_record?
+ segments.compact!
+ sql << "WHERE (#{segments.join(") AND (")}) " unless segments.empty?
end
def type_condition
@@ -1050,29 +1064,37 @@ def subclasses
@@subclasses[self] ||= []
@@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses }
end
-
- def scope_constraints
+
+ # Test whether the given method and optional key are scoped.
+ def scoped?(method, key = nil)
+ scoped_methods and scoped_methods.has_key?(method) and (key.nil? or scope(method).has_key?(key))
+ end
+
+ # Retrieve the scope for the given method and optional key.
+ def scope(method, key = nil)
+ if scoped_methods and scope = scoped_methods[method]
+ key ? scope[key] : scope
+ end
+ end
+
+ def scoped_methods
if allow_concurrency
- Thread.current[:constraints] ||= {}
- Thread.current[:constraints][self] ||= {}
+ Thread.current[:scoped_methods] ||= {}
+ Thread.current[:scoped_methods][self] ||= nil
else
- @scope_constraints ||= {}
+ @scoped_methods ||= nil
end
end
- # backwards compatibility
- alias_method :scope_constrains, :scope_constraints
- def scope_constraints=(value)
+ def scoped_methods=(value)
if allow_concurrency
- Thread.current[:constraints] ||= {}
- Thread.current[:constraints][self] = value
+ Thread.current[:scoped_methods] ||= {}
+ Thread.current[:scoped_methods][self] = value
else
- @scope_constraints = value
+ @scoped_methods = value
end
end
- # backwards compatibility
- alias_method :scope_constrains=, :scope_constraints=
-
+
# Returns the class type of the record using the current module as a prefix. So descendents of
# MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
def compute_type(type_name)
@@ -1729,4 +1751,4 @@ def clone_attribute_value(reader_method, attribute_name)
value
end
end
-end
+end
@@ -1030,17 +1030,17 @@ def test_interpolate_sql
assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar} baz') }
end
- def test_constrain_conditions
- developers = Developer.constrain(:conditions => 'salary > 90000') do
+ def test_scoped_find_conditions
+ developers = Developer.with_scope(:find => { :conditions => 'salary > 90000' }) do
Developer.find(:all, :conditions => 'id < 5')
end
david = Developer.find(1)
assert !developers.include?(david) # David's salary is less than 90,000
assert_equal 3, developers.size
end
- def test_constrain_limit_offset
- developers = Developer.constrain(:limit => 3, :offset => 2) do
+ def test_scoped_find_limit_offset
+ developers = Developer.with_scope(:find => { :limit => 3, :offset => 2 }) do
Developer.find(:all, :order => 'id')
end
david = Developer.find(1)
@@ -1049,7 +1049,7 @@ def test_constrain_limit_offset
assert !developers.include?(jamis) # Jamis has id 2
assert_equal 3, developers.size
- # Now test without constraints to make sure we get the whole thing
+ # Test without scoped find conditions to ensure we get the whole thing
developers = Developer.find(:all, :order => 'id')
assert_equal 10, developers.size
end
@@ -8,35 +8,35 @@ class ConditionsScopingTest < Test::Unit::TestCase
fixtures :developers, :comments, :posts
def test_set_conditions
- Developer.constrain(:conditions => 'just a test...') do
- assert_equal 'just a test...', Thread.current[:constraints][Developer][:conditions]
+ Developer.with_scope(:find => { :conditions => 'just a test...' }) do
+ assert_equal 'just a test...', Thread.current[:scoped_methods][Developer][:find][:conditions]
end
end
def test_scoped_find
- Developer.constrain(:conditions => "name = 'David'") do
+ Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
assert_nothing_raised { Developer.find(1) }
end
end
def test_scoped_find_first
- Developer.constrain(:conditions => "salary = 100000") do
+ Developer.with_scope(:find => { :conditions => "salary = 100000" }) do
assert_equal Developer.find(10), Developer.find(:first, :order => 'name')
end
end
def test_scoped_find_all
- Developer.constrain(:conditions => "name = 'David'") do
+ Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
assert_equal [Developer.find(1)], Developer.find(:all)
end
end
def test_scoped_count
- Developer.constrain(:conditions => "name = 'David'") do
+ Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
assert_equal 1, Developer.count
end
- Developer.constrain(:conditions => 'salary = 100000') do
+ Developer.with_scope(:find => { :conditions => 'salary = 100000' }) do
assert_equal 8, Developer.count
assert_equal 1, Developer.count("name LIKE 'fixture_1%'")
end
@@ -45,21 +45,36 @@ def test_scoped_count
def test_scoped_create
new_comment = nil
- VerySpecialComment.constrain(:creation => { :post_id => 1 }) do
- assert_equal({ :post_id => 1 }, Thread.current[:constraints][VerySpecialComment][:creation])
+ VerySpecialComment.with_scope(:create => { :post_id => 1 }) do
+ assert_equal({ :post_id => 1 }, Thread.current[:scoped_methods][VerySpecialComment][:create])
new_comment = VerySpecialComment.create :body => "Wonderful world"
end
-
+
assert Post.find(1).comments.include?(new_comment)
end
- def test_immutable_constraint
+ def test_immutable_scope
options = { :conditions => "name = 'David'" }
- Developer.constrain(options) do
+ Developer.with_scope(:find => options) do
assert_equal %w(David), Developer.find(:all).map { |d| d.name }
options[:conditions] = "name != 'David'"
assert_equal %w(David), Developer.find(:all).map { |d| d.name }
end
+
+ scope = { :find => { :conditions => "name = 'David'" }}
+ Developer.with_scope(scope) do
+ assert_equal %w(David), Developer.find(:all).map { |d| d.name }
+ scope[:find][:conditions] = "name != 'David'"
+ assert_equal %w(David), Developer.find(:all).map { |d| d.name }
+ end
+ end
+
+ def test_raise_on_nested_scope
+ Developer.with_scope(:find => { :conditions => '1=1' }) do
+ assert_raise(ArgumentError) do
+ Developer.with_scope(:find => { :conditions => '2=2' }) { }
+ end
+ end
end
end
@@ -84,7 +99,12 @@ def test_forwarding_to_dynamic_finders
assert_equal 4, Comment.find_all_by_type('Comment').size
assert_equal 2, @welcome.comments.find_all_by_type('Comment').size
end
-
+
+ def test_raise_on_nested_scope
+ Comment.with_scope(:find => { :conditions => '1=1' }) do
+ assert_raise(ArgumentError) { @welcome.comments.what_are_you }
+ end
+ end
end
@@ -106,6 +126,11 @@ def test_forwarding_to_dynamic_finders
assert_equal 2, @welcome.categories.find_all_by_type('Category').size
end
+ def test_raise_on_nested_scope
+ Category.with_scope(:find => { :conditions => '1=1' }) do
+ assert_raise(ArgumentError) { @welcome.categories.what_are_you }
+ end
+ end
end
Oops, something went wrong.

0 comments on commit 390e6d2

Please sign in to comment.