From 3da29f647bad5e79c90721ac23658940abddd27c Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 16 Jan 2010 01:42:01 +0530 Subject: Remove AR#scope() method --- activerecord/lib/active_record/base.rb | 33 ++++++++---------------------- activerecord/lib/active_record/relation.rb | 18 ++++++++-------- 2 files changed, 17 insertions(+), 34 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 7e4ee4b840..2eb8700bd7 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -642,7 +642,6 @@ module ActiveRecord #:nodoc: # end def find(*args) options = args.extract_options! - set_readonly_option!(options) relation = construct_finder_arel(options, current_scoped_methods) @@ -815,7 +814,7 @@ module ActiveRecord #:nodoc: # # 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 @@ module ActiveRecord #:nodoc: # Both calls delete the affected posts all at once with a single DELETE statement. If you need to destroy dependent # associations or call your before_* 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 @@ module ActiveRecord #:nodoc: 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 @@ module ActiveRecord #: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 @@ module ActiveRecord #: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 @@ module ActiveRecord #:nodoc: @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 diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 8a86d2e60a..85bf878416 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -142,6 +142,15 @@ module ActiveRecord @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 @@ module ActiveRecord @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 -- cgit v1.2.3