Skip to content
Browse files

Remove AR#scope() method

  • Loading branch information...
1 parent 3eaf525 commit 3da29f647bad5e79c90721ac23658940abddd27c @lifo lifo committed Jan 16, 2010
Showing with 17 additions and 34 deletions.
  1. +8 −25 activerecord/lib/active_record/base.rb
  2. +9 −9 activerecord/lib/active_record/relation.rb
View
33 activerecord/lib/active_record/base.rb
@@ -642,7 +642,6 @@ def colorize_logging(*args)
# end
def find(*args)
options = args.extract_options!
- set_readonly_option!(options)
relation = construct_finder_arel(options, current_scoped_methods)
@@ -815,7 +814,7 @@ def update(id, attributes)
# # Delete multiple rows
# Todo.delete([2,3,4])
def delete(id_or_array)
- active_relation.where(construct_conditions(nil, scope(:find))).delete(id_or_array)
+ active_relation.where(construct_conditions(nil, current_scoped_methods)).delete(id_or_array)
end
# Destroy an object (or multiple objects) that has the given id, the object is instantiated first,
@@ -938,7 +937,7 @@ def destroy_all(conditions = nil)
# Both calls delete the affected posts all at once with a single DELETE statement. If you need to destroy dependent
# associations or call your <tt>before_*</tt> or +after_destroy+ callbacks, use the +destroy_all+ method instead.
def delete_all(conditions = nil)
- active_relation.where(construct_conditions(conditions, scope(:find))).delete_all
+ active_relation.where(construct_conditions(conditions, current_scoped_methods)).delete_all
end
# Returns the result of an SQL statement that should only include a COUNT(*) in the SELECT part.
@@ -1842,7 +1841,6 @@ def with_scope(method_scoping = {}, action = :merge, &block)
if f = method_scoping[:find]
f.assert_valid_keys(VALID_FIND_OPTIONS)
- set_readonly_option! f
end
relation = construct_finder_arel(method_scoping[:find] || {})
@@ -1913,14 +1911,6 @@ def scoped?(method, key = nil) #:nodoc:
end
end
- # Retrieve the scope for the given method and optional key.
- def scope(method, key = nil) #:nodoc:
- case method
- when :create
- current_scoped_methods.send(:scope_for_create) if current_scoped_methods
- end
- end
-
def scoped_methods #:nodoc:
Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping.dup
end
@@ -2129,18 +2119,6 @@ def validate_find_options(options) #:nodoc:
options.assert_valid_keys(VALID_FIND_OPTIONS)
end
- def set_readonly_option!(options) #:nodoc:
- # Inherit :readonly from finder scope if set. Otherwise,
- # if :joins is not blank then :readonly defaults to true.
- unless options.has_key?(:readonly)
- if scoped_readonly = scope(:find, :readonly)
- options[:readonly] = scoped_readonly
- elsif !options[:joins].blank? && !options[:select]
- options[:readonly] = true
- end
- end
- end
-
def encode_quoted_value(value) #:nodoc:
quoted_value = connection.quote(value)
quoted_value = "'#{quoted_value[1..-2].gsub(/\'/, "\\\\'")}'" if quoted_value.include?("\\\'") # (for ruby mode) "
@@ -2159,7 +2137,12 @@ def initialize(attributes = nil)
@new_record = true
ensure_proper_type
self.attributes = attributes unless attributes.nil?
- self.class.send(:scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create)
+
+ if scope = self.class.send(:current_scoped_methods)
+ create_with = scope.scope_for_create
+ create_with.each { |att,value| self.send("#{att}=", value) } if create_with
+ end
+
result = yield self if block_given?
_run_initialize_callbacks
result
View
18 activerecord/lib/active_record/relation.rb
@@ -142,6 +142,15 @@ def to_sql
@to_sql ||= arel.to_sql
end
+ def scope_for_create
+ @scope_for_create ||= begin
+ @create_with_value || wheres.inject({}) do |hash, where|
+ hash[where.operand1.name] = where.operand2.value if where.is_a?(Arel::Predicates::Equality)
+ hash
+ end
+ end
+ end
+
protected
def method_missing(method, *args, &block)
@@ -167,15 +176,6 @@ def with_create_scope
@klass.send(:with_scope, :create => scope_for_create, :find => {}) { yield }
end
- def scope_for_create
- @scope_for_create ||= begin
- @create_with_value || wheres.inject({}) do |hash, where|
- hash[where.operand1.name] = where.operand2.value if where.is_a?(Arel::Predicates::Equality)
- hash
- end
- end
- end
-
def where_clause(join_string = " AND ")
arel.send(:where_clauses).join(join_string)
end

0 comments on commit 3da29f6

Please sign in to comment.
Something went wrong with that request. Please try again.