pax_global_header00006660000000000000000000000064125066624470014526gustar00rootroot0000000000000052 comment=419f0397d5afb1adf09489e342c6220b71faf4a0 activerecord-deprecated_finders-1.0.4/000077500000000000000000000000001250666244700200125ustar00rootroot00000000000000activerecord-deprecated_finders-1.0.4/.gitignore000066400000000000000000000002241250666244700220000ustar00rootroot00000000000000*.gem *.rbc .bundle .config .yardoc *.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp activerecord-deprecated_finders-1.0.4/.travis.yml000066400000000000000000000011241250666244700221210ustar00rootroot00000000000000language: ruby rvm: - 1.9.3 - 2.0.0 - 2.1 - 2.2 - ruby-head gemfile: - gemfiles/Gemfile-4-0 - gemfiles/Gemfile-4-1 - gemfiles/Gemfile-4-2 - gemfiles/Gemfile-4-0-stable - gemfiles/Gemfile-4-1-stable - gemfiles/Gemfile-4-2-stable - Gemfile notifications: email: false campfire: on_success: change on_failure: always rooms: - secure: "DwWWfmRXWSYoelIo3W6H99FrLM2V9ugFoAu+xBo42n2pNV44lU+8T9O+zWkV\nwD3Lz+7URB/5IRPcEel/KYaRYNJl71YUBgtlcvjM7Xl/7YGLYFAcPT2RXfKA\nctvckk/5NehmSawnLhhvMz2gKtIx/fIwCVqxUnRqqYE0eEQaONA=" before_install: - gem install bundler activerecord-deprecated_finders-1.0.4/Gemfile000066400000000000000000000001141250666244700213010ustar00rootroot00000000000000source 'https://rubygems.org' gemspec gem 'rails', '>= 4.0.0.beta', '< 5' activerecord-deprecated_finders-1.0.4/LICENSE000066400000000000000000000020541250666244700210200ustar00rootroot00000000000000Copyright (c) 2012 Jon Leighton MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.activerecord-deprecated_finders-1.0.4/README.md000066400000000000000000000035661250666244700213030ustar00rootroot00000000000000# Active Record Deprecated Finders This gem is a dependency of Rails 4.0 to provide deprecated finder functionality. It will be removed as a dependency in Rails 4.1, but users can manually include it in their Gemfile and it will continue to be maintained until Rails 5. ```ruby gem 'activerecord-deprecated_finders', require: 'active_record/deprecated_finders' ``` This gem is used to extract and deprecate old-style finder option hashes in Active Record: ```ruby Post.find(:all, conditions: { published_on: 2.weeks.ago }, limit: 5) ``` as well as the following dynamic finders: * `find_all_by_...` * `find_last_by_...` * `scoped_by_...` * `find_or_initialize_by_...` * `find_or_create_by_...` Note that `find(primary_key)`, `find_by...`, and `find_by...!` are not deprecated. To avoid reliance on this gem, you'll need to migrate your finder usage. To migrate dynamic finders to Rails 4.1+: * `find_all_by_...` should become `where(...)`. * `find_last_by_...` should become `where(...).last`. * `scoped_by_...` should become `where(...)`. * `find_or_initialize_by_...` should become `find_or_initialize_by(...)`. * `find_or_create_by_...` should become `find_or_create_by(...)`. To migrate old-style finder option hashes and for additional information, please refer to: * [ActiveRecord::FinderMethods][findermethods], [ActiveRecord::Relation][relation], and [ActiveRecord::QueryMethods][querymethods] docs. * Rails Guide: Upgrading Ruby on Rails ([stable][stableguide] / [edge][edgeguide]). [findermethods]: http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html [relation]: http://api.rubyonrails.org/classes/ActiveRecord/Relation.html [querymethods]: http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html [stableguide]: http://guides.rubyonrails.org/upgrading_ruby_on_rails.html [edgeguide]: http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html activerecord-deprecated_finders-1.0.4/Rakefile000066400000000000000000000003141250666244700214550ustar00rootroot00000000000000#!/usr/bin/env rake require "bundler/gem_tasks" require 'rake/testtask' Rake::TestTask.new do |t| t.libs = ["test"] t.pattern = "test/**/*_test.rb" t.ruby_opts = ['-w'] end task :default => :test activerecord-deprecated_finders-1.0.4/activerecord-deprecated_finders.gemspec000066400000000000000000000016361250666244700276470ustar00rootroot00000000000000# -*- encoding: utf-8 -*- require File.expand_path('../lib/active_record/deprecated_finders/version', __FILE__) Gem::Specification.new do |gem| gem.name = "activerecord-deprecated_finders" gem.version = ActiveRecord::DeprecatedFinders::VERSION gem.description = %q{Deprecated finder APIs extracted from Active Record.} gem.summary = %q{This gem contains deprecated finder APIs extracted from Active Record.} gem.homepage = "https://github.com/rails/activerecord-deprecated_finders" gem.license = 'MIT' gem.authors = ["Jon Leighton"] gem.email = ["j@jonathanleighton.com"] gem.files = Dir["README.md", "lib/**/*.rb", "LICENSE"] gem.require_paths = ["lib"] gem.add_development_dependency 'minitest', '>= 3' gem.add_development_dependency 'activerecord', '>= 4.0.0.beta', '< 5' gem.add_development_dependency 'sqlite3', '~> 1.3' end activerecord-deprecated_finders-1.0.4/gemfiles/000077500000000000000000000000001250666244700216055ustar00rootroot00000000000000activerecord-deprecated_finders-1.0.4/gemfiles/Gemfile-4-0000066400000000000000000000001161250666244700233740ustar00rootroot00000000000000source 'https://rubygems.org' gemspec :path => '..' gem 'rails', '~> 4.0.5' activerecord-deprecated_finders-1.0.4/gemfiles/Gemfile-4-0-stable000066400000000000000000000001571250666244700246510ustar00rootroot00000000000000source 'https://rubygems.org' gemspec :path => '..' gem 'rails', github: 'rails/rails', branch: '4-0-stable' activerecord-deprecated_finders-1.0.4/gemfiles/Gemfile-4-1000066400000000000000000000001161250666244700233750ustar00rootroot00000000000000source 'https://rubygems.org' gemspec :path => '..' gem 'rails', '~> 4.1.8' activerecord-deprecated_finders-1.0.4/gemfiles/Gemfile-4-1-stable000066400000000000000000000001571250666244700246520ustar00rootroot00000000000000source 'https://rubygems.org' gemspec :path => '..' gem 'rails', github: 'rails/rails', branch: '4-1-stable' activerecord-deprecated_finders-1.0.4/gemfiles/Gemfile-4-2000066400000000000000000000001161250666244700233760ustar00rootroot00000000000000source 'https://rubygems.org' gemspec :path => '..' gem 'rails', '~> 4.2.1' activerecord-deprecated_finders-1.0.4/gemfiles/Gemfile-4-2-stable000066400000000000000000000001571250666244700246530ustar00rootroot00000000000000source 'https://rubygems.org' gemspec :path => '..' gem 'rails', github: 'rails/rails', branch: '4-2-stable' activerecord-deprecated_finders-1.0.4/lib/000077500000000000000000000000001250666244700205605ustar00rootroot00000000000000activerecord-deprecated_finders-1.0.4/lib/active_record/000077500000000000000000000000001250666244700233715ustar00rootroot00000000000000activerecord-deprecated_finders-1.0.4/lib/active_record/deprecated_finders.rb000066400000000000000000000006571250666244700275400ustar00rootroot00000000000000require 'active_support/lazy_load_hooks' require 'active_record/deprecated_finders/version' ActiveSupport.on_load(:active_record) do require 'active_record/deprecated_finders/base' require 'active_record/deprecated_finders/relation' require 'active_record/deprecated_finders/dynamic_matchers' require 'active_record/deprecated_finders/collection_proxy' require 'active_record/deprecated_finders/association_builder' end activerecord-deprecated_finders-1.0.4/lib/active_record/deprecated_finders/000077500000000000000000000000001250666244700272035ustar00rootroot00000000000000activerecord-deprecated_finders-1.0.4/lib/active_record/deprecated_finders/association_builder.rb000066400000000000000000000051551250666244700335600ustar00rootroot00000000000000require 'active_record/associations/builder/association' require 'active_support/core_ext/module/aliasing' require 'active_support/deprecation' module ActiveRecord::Associations::Builder class DeprecatedOptionsProc attr_reader :options def initialize(options) options[:includes] = options.delete(:include) if options[:include] options[:where] = options.delete(:conditions) if options[:conditions] @options = options end def to_proc options = self.options proc do |owner| if options[:where].respond_to?(:to_proc) context = owner || self where(context.instance_eval(&options[:where])) .merge!(options.except(:where)) else merge(options) end end end def arity 1 end end class Association DEPRECATED_OPTIONS = [:readonly, :order, :limit, :group, :having, :offset, :select, :uniq, :include, :conditions] self.valid_options += [:select, :conditions, :include, :readonly] def initialize_with_deprecated_options(model, name, scope, options) options = scope if scope.is_a?(Hash) deprecated_options = options.slice(*DEPRECATED_OPTIONS) if scope.respond_to?(:call) && !deprecated_options.empty? raise ArgumentError, "Invalid mix of scope block and deprecated finder options on " \ "ActiveRecord association: #{model.name}.#{macro} :#{name}" end if scope.is_a?(Hash) if deprecated_options.empty? scope = nil else ActiveSupport::Deprecation.warn( "The following options in your #{model.name}.#{macro} :#{name} declaration are deprecated: " \ "#{deprecated_options.keys.map(&:inspect).join(',')}. Please use a scope block instead. " \ "For example, the following:\n" \ "\n" \ " has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'\n" \ "\n" \ "should be rewritten as the following:\n" \ "\n" \ " has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'\n" ) scope = DeprecatedOptionsProc.new(deprecated_options) options = options.except(*DEPRECATED_OPTIONS) end end initialize_without_deprecated_options(model, name, scope, options) end alias_method_chain :initialize, :deprecated_options end class CollectionAssociation include Module.new { def valid_options super + [:order, :group, :having, :limit, :offset, :uniq] end } end end activerecord-deprecated_finders-1.0.4/lib/active_record/deprecated_finders/base.rb000066400000000000000000000110771250666244700304500ustar00rootroot00000000000000require 'active_support/deprecation' module ActiveRecord module DeprecatedFinders class ScopeWrapper def self.wrap(klass, scope) if scope.is_a?(Hash) ActiveSupport::Deprecation.warn( "Calling #scope or #default_scope with a hash is deprecated. Please use a lambda " \ "containing a scope. E.g. scope :red, -> { where(color: 'red') }" ) new(klass, scope) elsif !scope.is_a?(Relation) && scope.respond_to?(:call) new(klass, scope) else scope end end def initialize(klass, scope) @klass = klass @scope = scope end def call(*args) if @scope.respond_to?(:call) result = @scope.call(*args) if result.is_a?(Hash) msg = "Returning a hash from a #scope or #default_scope block is deprecated. Please " \ "return an actual scope object instead. E.g. scope :red, -> { where(color: 'red') } " \ "rather than scope :red, -> { { conditions: { color: 'red' } } }. " if @scope.respond_to?(:source_location) msg << "(The scope was defined at #{@scope.source_location.join(':')}.)" end ActiveSupport::Deprecation.warn(msg) end else result = @scope end if result.is_a?(Hash) @klass.all.apply_finder_options(result, true) else result end end end def default_scope(scope = {}, &block) if block_given? super ScopeWrapper.new(self, block), &nil else super ScopeWrapper.wrap(self, scope) end end def scoped(options = nil) ActiveSupport::Deprecation.warn("Model.scoped is deprecated. Please use Model.all instead.") options ? all.apply_finder_options(options, true) : all end def all(options = nil) options ? super().all(options) : super() end def scope(name, body = {}, &block) super(name, ScopeWrapper.wrap(self, body), &block) end def with_scope(scope = {}, action = :merge) ActiveSupport::Deprecation.warn( "ActiveRecord::Base#with_scope and #with_exclusive_scope are deprecated. " \ "Please use ActiveRecord::Relation#scoping instead. (You can use #merge " \ "to merge multiple scopes together.)" ) # If another Active Record class has been passed in, get its current scope scope = scope.current_scope if !scope.is_a?(Relation) && scope.respond_to?(:current_scope) previous_scope = self.current_scope if scope.is_a?(Hash) # Dup first and second level of hash (method and params). scope = scope.dup scope.each do |method, params| scope[method] = params.dup unless params == true end scope.assert_valid_keys([ :find, :create ]) relation = construct_finder_arel(scope[:find] || {}) if relation.respond_to?(:default_scoped=) relation.default_scoped = true unless action == :overwrite end if previous_scope && previous_scope.create_with_value && scope[:create] scope_for_create = if action == :merge previous_scope.create_with_value.merge(scope[:create]) else scope[:create] end relation = relation.create_with(scope_for_create) else scope_for_create = scope[:create] scope_for_create ||= previous_scope.create_with_value if previous_scope relation = relation.create_with(scope_for_create) if scope_for_create end scope = relation end scope = previous_scope.merge(scope) if previous_scope && action == :merge scope.scoping { yield } end protected # Works like with_scope, but discards any nested properties. def with_exclusive_scope(method_scoping = {}, &block) if method_scoping.values.any? { |e| e.is_a?(ActiveRecord::Relation) } raise ArgumentError, <<-MSG New finder API can not be used with_exclusive_scope. You can either call unscoped to get an anonymous scope not bound to the default_scope: User.unscoped.where(:active => true) Or call unscoped with a block: User.unscoped do User.where(:active => true).all end MSG end with_scope(method_scoping, :overwrite, &block) end private def construct_finder_arel(options = {}, scope = nil) relation = options.is_a?(Hash) ? unscoped.apply_finder_options(options, true) : options relation = scope.merge(relation) if scope relation end end class Base extend DeprecatedFinders end end activerecord-deprecated_finders-1.0.4/lib/active_record/deprecated_finders/collection_proxy.rb000066400000000000000000000025621250666244700331310ustar00rootroot00000000000000module ActiveRecord module Associations class CollectionProxy module InterceptDynamicInstantiators def method_missing(method, *args, &block) match = DynamicMatchers::Method.match(klass, method) if match && match.is_a?(DynamicMatchers::Instantiator) scoping do klass.send(method, *args) do |record| sanitized_method = match.class.prefix + match.class.suffix if %w(find_or_create_by find_or_create_by!).include?(sanitized_method) && proxy_association.reflection.options[:through].present? proxy_association.send(:save_through_record, record) else proxy_association.add_to_target(record) end yield record if block_given? end end else super end end end def self.inherited(subclass) subclass.class_eval do # Ensure this get included first include ActiveRecord::Delegation::ClassSpecificRelation unless ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2 && ActiveRecord::VERSION::TINY > 1 # Now override the method_missing definition include InterceptDynamicInstantiators end end end end end end activerecord-deprecated_finders-1.0.4/lib/active_record/deprecated_finders/dynamic_matchers.rb000066400000000000000000000104311250666244700330410ustar00rootroot00000000000000require 'active_support/deprecation' module ActiveRecord module DynamicMatchers module DeprecatedFinder def body <<-CODE result = #{super} result && block_given? ? yield(result) : result CODE end def result "all.apply_finder_options(options, true).#{super}" end def signature "#{super}, options = {}" end end module DeprecationWarning def body "#{deprecation_warning}\n#{super}" end def deprecation_warning %{ActiveSupport::Deprecation.warn("This dynamic method is deprecated. Please use e.g. #{deprecation_alternative} instead.")} end end module FindByDeprecationWarning def body <<-CODE if block_given? ActiveSupport::Deprecation.warn("Calling find_by or find_by! methods with a block is deprecated with no replacement.") end unless options.empty? ActiveSupport::Deprecation.warn( "Calling find_by or find_by! methods with options is deprecated. " \ "Build a scope instead, e.g. User.where('age > 21').find_by_name('Jon')." ) end #{super} CODE end end class FindBy include DeprecatedFinder include FindByDeprecationWarning end class FindByBang include DeprecatedFinder include FindByDeprecationWarning end class FindAllBy < Method Method.matchers << self include Finder include DeprecatedFinder include DeprecationWarning def self.prefix "find_all_by" end def finder "where" end def result "#{super}.to_a" end def deprecation_alternative "Post.where(...).all" end end class FindLastBy < Method Method.matchers << self include Finder include DeprecatedFinder include DeprecationWarning def self.prefix "find_last_by" end def finder "where" end def result "#{super}.last" end def deprecation_alternative "Post.where(...).last" end end class ScopedBy < Method Method.matchers << self include Finder include DeprecationWarning def self.prefix "scoped_by" end def body "#{deprecation_warning} \n where(#{attributes_hash})" end def deprecation_alternative "Post.where(...)" end end class Instantiator < Method include DeprecationWarning def self.dispatch(klass, attribute_names, instantiator, args, block) if args.length == 1 && args.first.is_a?(Hash) attributes = args.first.stringify_keys conditions = attributes.slice(*attribute_names) rest = [attributes.except(*attribute_names)] else raise ArgumentError, "too few arguments" unless args.length >= attribute_names.length conditions = Hash[attribute_names.map.with_index { |n, i| [n, args[i]] }] rest = args.drop(attribute_names.length) end klass.where(conditions).first || klass.create_with(conditions).send(instantiator, *rest, &block) end def signature "*args, &block" end def body <<-CODE #{deprecation_warning} #{self.class}.dispatch(self, #{attribute_names.inspect}, #{instantiator.inspect}, args, block) CODE end def instantiator raise NotImplementedError end def deprecation_alternative "Post.#{self.class.prefix}#{self.class.suffix}(name: 'foo')" end end class FindOrInitializeBy < Instantiator Method.matchers << self def self.prefix "find_or_initialize_by" end def instantiator "new" end end class FindOrCreateBy < Instantiator Method.matchers << self def self.prefix "find_or_create_by" end def instantiator "create" end end class FindOrCreateByBang < Instantiator Method.matchers << self def self.prefix "find_or_create_by" end def self.suffix "!" end def instantiator "create!" end end end end activerecord-deprecated_finders-1.0.4/lib/active_record/deprecated_finders/relation.rb000066400000000000000000000136611250666244700313540ustar00rootroot00000000000000require 'active_record/relation' require 'active_support/core_ext/module/aliasing' module ActiveRecord class Relation module DeprecatedMethods VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from, :lock ] # The silence_deprecation arg is for internal use, where we have already output a # deprecation further up the call stack. def apply_finder_options(options, silence_deprecation = false) ActiveSupport::Deprecation.warn("#apply_finder_options is deprecated") unless silence_deprecation relation = clone return relation unless options options.assert_valid_keys(VALID_FIND_OPTIONS) finders = options.dup finders.delete_if { |key, value| value.nil? && key != :limit } ((VALID_FIND_OPTIONS - [:conditions, :include]) & finders.keys).each do |finder| relation = relation.send(finder, finders[finder]) end relation = relation.where(finders[:conditions]) if options.has_key?(:conditions) relation = relation.includes(finders[:include]) if options.has_key?(:include) relation end def update_all_with_deprecated_options(updates, conditions = nil, options = {}) scope = self if conditions scope = where(conditions) ActiveSupport::Deprecation.warn( "Relation#update_all with conditions is deprecated. Please use " \ "Item.where(color: 'red').update_all(...) rather than " \ "Item.update_all(..., color: 'red').", caller ) end if options.present? scope = scope.apply_finder_options(options.slice(:limit, :order), true) ActiveSupport::Deprecation.warn( "Relation#update_all with :limit / :order options is deprecated. " \ "Please use e.g. Post.limit(1).order(:foo).update_all instead.", caller ) end scope.update_all_without_deprecated_options(updates) end def find_in_batches(options = {}, &block) if (finder_options = options.except(:start, :batch_size)).present? ActiveSupport::Deprecation.warn( "Relation#find_in_batches with finder options is deprecated. Please build " \ "a scope and then call find_in_batches on it instead.", caller ) raise "You can't specify an order, it's forced to be #{batch_order}" if options[:order].present? raise "You can't specify a limit, it's forced to be the batch_size" if options[:limit].present? apply_finder_options(finder_options, true). find_in_batches(options.slice(:start, :batch_size), &block) else super end end def calculate(operation, column_name, options = {}) if options.except(:distinct).present? ActiveSupport::Deprecation.warn( "Relation#calculate with finder options is deprecated. Please build " \ "a scope and then call calculate on it instead.", caller ) apply_finder_options(options.except(:distinct), true) .calculate(operation, column_name, options.slice(:distinct)) else super end end def find(*args) options = args.extract_options! if options.present? scope = apply_finder_options(options, true) case finder = args.first when :first, :last, :all ActiveSupport::Deprecation.warn( "Calling #find(#{finder.inspect}) is deprecated. Please call " \ "##{finder} directly instead. You have also used finder options. " \ "These are also deprecated. Please build a scope instead of using " \ "finder options.", caller ) scope.send(finder) else ActiveSupport::Deprecation.warn( "Passing options to #find is deprecated. Please build a scope " \ "and then call #find on it.", caller ) scope.find(*args) end else case finder = args.first when :first, :last, :all ActiveSupport::Deprecation.warn( "Calling #find(#{finder.inspect}) is deprecated. Please call " \ "##{finder} directly instead.", caller ) send(finder) else super end end end def first(*args) if args.empty? super else if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash)) super else ActiveSupport::Deprecation.warn( "Relation#first with finder options is deprecated. Please build " \ "a scope and then call #first on it instead.", caller ) apply_finder_options(args.first, true).first end end end def last(*args) if args.empty? super else if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash)) super else ActiveSupport::Deprecation.warn( "Relation#last with finder options is deprecated. Please build " \ "a scope and then call #last on it instead.", caller ) apply_finder_options(args.first, true).last end end end def all(*args) ActiveSupport::Deprecation.warn( "Relation#all is deprecated. If you want to eager-load a relation, you can " \ "call #load (e.g. `Post.where(published: true).load`). If you want " \ "to get an array of records from a relation, you can call #to_a (e.g. " \ "`Post.where(published: true).to_a`).", caller ) apply_finder_options(args.first, true).to_a end end include DeprecatedMethods alias_method_chain :update_all, :deprecated_options end end activerecord-deprecated_finders-1.0.4/lib/active_record/deprecated_finders/version.rb000066400000000000000000000001171250666244700312140ustar00rootroot00000000000000module ActiveRecord module DeprecatedFinders VERSION = "1.0.4" end end activerecord-deprecated_finders-1.0.4/test/000077500000000000000000000000001250666244700207715ustar00rootroot00000000000000activerecord-deprecated_finders-1.0.4/test/associations_test.rb000066400000000000000000000057051250666244700250630ustar00rootroot00000000000000require 'helper' describe 'associations' do before do @klass = Class.new(ActiveRecord::Base) def @klass.name; 'Post'; end @klass.table_name = 'posts' Appointment.delete_all end it 'should respect find_by_anything method defined on the base class' do physician = Physician.create! ActiveSupport::Deprecation.silence do assert_equal [], physician.patients.find_by_custom_name end end it 'find_or_create_by on has_many through should work' do physician = Physician.create! ActiveSupport::Deprecation.silence do physician.patients.find_or_create_by_name('Tim') end assert_equal 1, Appointment.count end it 'find_or_create_by with bang on has_many through should work' do physician = Physician.create! ActiveSupport::Deprecation.silence do physician.patients.find_or_create_by_name!('Tim') end assert_equal 1, Appointment.count end it 'find_or_create_by on has_many should work' do physician = Physician.create! ActiveSupport::Deprecation.silence do appointment = physician.appointments.find_or_create_by_status(status: 'active', week_day: 'sunday') assert_equal appointment, physician.appointments.find_or_create_by_status(status: 'active', week_day: 'sunday') end end it 'translates hash scope options into scopes' do assert_deprecated do @klass.has_many :comments, readonly: 'a', order: 'b', limit: 'c', group: 'd', having: 'e', offset: 'f', select: 'g', uniq: 'h', include: 'i', conditions: 'j' end scope = @klass.new.comments scope.readonly_value.must_equal 'a' scope.order_values.must_equal ['b'] scope.limit_value.must_equal 'c' scope.group_values.must_equal ['d'] scope.having_values.must_equal ['e'] scope.offset_value.must_equal 'f' scope.select_values.must_equal ['g'] scope.uniq_value.must_equal 'h' scope.includes_values.must_equal ['i'] scope.where_values.must_include 'j' end it 'supports proc where values' do ActiveSupport::Deprecation.silence do @klass.has_many :comments, conditions: proc { 'omg' } end @klass.new.comments.where_values.must_include 'omg' ActiveSupport::Deprecation.silence do @klass.joins(:comments).to_sql.must_include 'omg' end end it 'supports proc where values which access the owner' do ActiveSupport::Deprecation.silence do @klass.has_many :comments, conditions: proc { title } end @klass.new(title: 'omg').comments.where_values.must_include 'omg' end it "allows a declaration with a scope with no options" do ActiveSupport::Deprecation.silence do @klass.has_many :comments, -> { limit 5 } end scope = @klass.new.comments scope.limit_value.must_equal 5 end it "raises an ArgumentError when declaration uses both scope and deprecated options" do assert_raises(ArgumentError) do @klass.has_many :comments, -> { limit 5 }, :order=>'b' end end end activerecord-deprecated_finders-1.0.4/test/calculate_test.rb000066400000000000000000000021041250666244700243070ustar00rootroot00000000000000require 'helper' describe 'calculate' do after do Post.destroy_all end it 'supports finder options' do Post.create id: 1 Post.create id: 2, title: 'foo' Post.create id: 3, title: 'foo' assert_deprecated { Post.calculate(:sum, :id, conditions: { title: 'foo' }) }.must_equal 5 end if active_record_4_0? it 'should pass :distinct option along' do Post.create id: 1, title: "foo" Post.create id: 2, title: "bar" Post.create id: 3, title: "bar" Post.create id: 4, title: "baz" _result, deprecations = collect_deprecations do Post.count(:title, conditions: { title: ["bar", "baz"] }, distinct: true).must_equal 2 end assert_equal 2, deprecations.size, deprecations end end it 'should not issue :distinct deprecation warning when :distinct was not passed' do Post.create id: 1 Post.create id: 2, title: "bar" _result, deprecations = collect_deprecations do Post.count(conditions: { title: "bar" }).must_equal 1 end assert_equal 1, deprecations.size, deprecations end end activerecord-deprecated_finders-1.0.4/test/default_scope_test.rb000066400000000000000000000015471250666244700252010ustar00rootroot00000000000000require 'helper' describe 'default scope' do before do Post.create(id: 1, title: 'foo lol') Post.create(id: 2, title: 'foo omg') Post.create(id: 3) @klass = Class.new(Post) @klass.table_name = 'posts' end after do Post.delete_all end it 'works with a finder hash' do assert_deprecated { @klass.default_scope conditions: { id: 1 } } @klass.all.map(&:id).must_equal [1] end it 'works with a finder hash and a scope' do @klass.default_scope { @klass.where("title like '%foo%'") } ActiveSupport::Deprecation.silence do @klass.default_scope conditions: "title like '%omg%'" end @klass.all.map(&:id).must_equal [2] end it 'works with a block that returns a hash' do @klass.default_scope { { conditions: { id: 1 } } } assert_deprecated { @klass.all.to_a }.map(&:id).must_equal [1] end end activerecord-deprecated_finders-1.0.4/test/dynamic_methods_test.rb000066400000000000000000000101231250666244700255210ustar00rootroot00000000000000require 'helper' describe 'dynamic_methods' do before do @posts = [ Post.create(title: 'foo', category: '1'), Post.create(title: 'bar', category: '1'), Post.create(title: 'bar', category: '2') ] end after do Post.delete_all Comment.delete_all end it 'supports find_all_by' do assert_deprecated do Post.find_all_by_title('bar').must_equal [@posts[1], @posts[2]] Post.find_all_by_title_and_category('bar', '2').must_equal [@posts[2]] end end it 'supports find_all_by with finder options' do assert_deprecated do Post.find_all_by_title('bar', conditions: { category: '1' }).must_equal [@posts[1]] end end it 'supports find_last_by' do assert_deprecated do Post.find_last_by_title('foo').must_equal @posts[0] Post.find_last_by_title('bar').must_equal @posts[2] end end it 'supports find_last_by with finder options' do assert_deprecated do Post.find_last_by_title('bar', conditions: { category: '1' }).must_equal @posts[1] end end it 'supports scoped_by' do scope = assert_deprecated { Post.scoped_by_title('bar') } scope.is_a?(ActiveRecord::Relation).must_equal true scope.to_a.must_equal [@posts[1], @posts[2]] end it 'supports find_or_initialize_by' do assert_deprecated { Post.find_or_initialize_by_title_and_category('bar', '1').must_equal @posts[1] } post = assert_deprecated { Post.find_or_initialize_by_title_and_category('bar', '3') } post.new_record?.must_equal true post.title.must_equal 'bar' post.category.must_equal '3' end it 'supports find_or_create_by' do assert_deprecated { Post.find_or_create_by_title_and_category('bar', '1').must_equal @posts[1] } post = assert_deprecated { Post.find_or_create_by_title_and_category('bar', '3') } post.new_record?.must_equal false post.title.must_equal 'bar' post.category.must_equal '3' end it 'supports find_or_create_by!' do assert_deprecated { Post.find_or_create_by_title_and_category!('bar', '1').must_equal @posts[1] } post = assert_deprecated { Post.find_or_create_by_title_and_category!('bar', '3') } post.new_record?.must_equal false post.title.must_equal 'bar' post.category.must_equal '3' klass = Class.new(ActiveRecord::Base) def klass.name; 'Post'; end klass.table_name = 'posts' klass.validates_presence_of :category lambda { ActiveSupport::Deprecation.silence { klass.find_or_create_by_title!('z') } }.must_raise ActiveRecord::RecordInvalid end it 'supports find_by with finder options' do assert_deprecated do Post.find_by_title('bar', conditions: { category: '2' }).must_equal @posts[2] end end it 'supports find_by! with finder options' do assert_deprecated do Post.find_by_title!('bar', conditions: { category: '2' }).must_equal @posts[2] end lambda { assert_deprecated { Post.find_by_title!('bar', conditions: { category: '3' }) } }.must_raise ActiveRecord::RecordNotFound end it 'supports find_by with a block' do assert_deprecated do Post.find_by_title('foo') { |r| [r, 'block'] }.must_equal [@posts[0], 'block'] Post.find_by_title('baz') { |r| [r, 'block'] }.must_equal nil end end it 'supports find_by! with a block' do assert_deprecated do Post.find_by_title!('foo') { |r| [r, 'block'] }.must_equal [@posts[0], 'block'] end end it 'adds to an association when find_or_initialize_by is called' do post = @posts.first comment = ActiveSupport::Deprecation.silence { post.comments.find_or_initialize_by_title('omg') } post.comments.must_equal [comment] end it 'adds to an association when find_or_create_by is called' do post = @posts.first post.comments.load_target comment = ActiveSupport::Deprecation.silence { post.comments.find_or_create_by_title('omg') } post.comments.must_equal [comment] post.reload.comments.must_equal [comment] end it "doesn't mess with method_missing for non-find_or_{initialize|create}_by methods" do post = @posts.first post.comments.lol.must_equal 'lol' end end activerecord-deprecated_finders-1.0.4/test/find_in_batches_test.rb000066400000000000000000000005241250666244700254550ustar00rootroot00000000000000require 'helper' describe 'find_in_batches' do after do Post.destroy_all end it 'accepts finder options' do foo = Post.create title: 'foo' Post.create title: 'bar' assert_deprecated do Post.find_in_batches(conditions: "title = 'foo'") do |records| records.must_equal [foo] end end end end activerecord-deprecated_finders-1.0.4/test/finder_options_test.rb000066400000000000000000000041771250666244700254100ustar00rootroot00000000000000require 'helper' describe 'apply_finder_options' do before do @deprecation_behavior = ActiveSupport::Deprecation.behavior ActiveSupport::Deprecation.behavior = :silence end after do ActiveSupport::Deprecation.behavior = @deprecation_behavior end it 'is deprecated' do assert_deprecated { Post.scoped.apply_finder_options(:conditions => 'foo') } end it 'supports :conditions' do scope = Post.scoped.apply_finder_options(:conditions => 'foo') scope.where_values.must_equal ['foo'] end it 'supports :include' do scope = Post.scoped.apply_finder_options(:include => :foo) scope.includes_values.must_equal [:foo] end it 'supports :joins' do scope = Post.scoped.apply_finder_options(:joins => :foo) scope.joins_values.must_equal [:foo] end it 'supports :limit' do scope = Post.scoped.apply_finder_options(:limit => 5) scope.limit_value.must_equal 5 end it 'supports :offset' do scope = Post.scoped.apply_finder_options(:offset => 5) scope.offset_value.must_equal 5 end it "does not support :references (as it's new in 4.0)" do lambda { Post.scoped.apply_finder_options(references: :foo) }.must_raise ArgumentError end it 'supports :order' do scope = Post.scoped.apply_finder_options(:order => 'foo') scope.order_values.must_equal ['foo'] end it 'supports :select' do scope = Post.scoped.apply_finder_options(:select => :foo) scope.select_values.must_equal [:foo] end it 'supports :readonly' do scope = Post.scoped.apply_finder_options(:readonly => :foo) scope.readonly_value.must_equal :foo end it 'supports :group' do scope = Post.scoped.apply_finder_options(:group => :foo) scope.group_values.must_equal [:foo] end it 'supports :having' do scope = Post.scoped.apply_finder_options(:having => :foo) scope.having_values.must_equal [:foo] end it 'supports :from' do scope = Post.scoped.apply_finder_options(:from => :foo) scope.from_value.must_equal [:foo, nil] end it 'supports :lock' do scope = Post.scoped.apply_finder_options(:lock => true) scope.lock_value.must_equal true end end activerecord-deprecated_finders-1.0.4/test/finder_test.rb000066400000000000000000000040771250666244700236340ustar00rootroot00000000000000require 'helper' describe 'finders' do before do @posts = 3.times.map { |i| Post.create id: i + 1 } end after do Post.delete_all end it 'supports find(:all) with no options' do assert_deprecated { Post.find(:all) }.must_equal @posts end it 'supports find(:all) with options' do assert_deprecated { Post.find(:all, conditions: 'id >= 2') }.must_equal [@posts[1], @posts[2]] end it 'supports all with options' do assert_deprecated { Post.all(conditions: 'id >= 2') }.must_equal [@posts[1], @posts[2]] end it 'returns an array from all when there are options' do posts = ActiveSupport::Deprecation.silence { Post.all(conditions: 'id >= 2') } posts.class.must_equal Array end it 'returns a relation from all when there are no options' do posts = Post.all posts.is_a?(ActiveRecord::Relation).must_equal true end it 'deprecates #all on a relation' do relation = Post.where('id >= 2') assert_deprecated { relation.all }.must_equal [@posts[1], @posts[2]] end it 'supports find(:first) with no options' do assert_deprecated { Post.order(:id).find(:first) }.must_equal @posts.first end it 'supports find(:first) with options' do assert_deprecated { Post.order(:id).find(:first, conditions: 'id >= 2') }.must_equal @posts[1] end it 'supports first with options' do assert_deprecated { Post.order(:id).first(conditions: 'id >= 2') }.must_equal @posts[1] end it 'supports find(:last) with no options' do assert_deprecated { Post.order(:id).find(:last) }.must_equal @posts.last end it 'supports find(:last) with options' do assert_deprecated { Post.order(:id).find(:last, conditions: 'id <= 2') }.must_equal @posts[1] end it 'supports last with options' do assert_deprecated { Post.order(:id).last(conditions: 'id <= 2') }.must_equal @posts[1] end it 'support find(1) etc with options' do assert_deprecated do Post.find(1, conditions: '1=1').must_equal Post.find(1) lambda { Post.find(1, conditions: '0=1') }.must_raise(ActiveRecord::RecordNotFound) end end end activerecord-deprecated_finders-1.0.4/test/helper.rb000066400000000000000000000025571250666244700226060ustar00rootroot00000000000000require 'bundler/setup' require 'minitest/autorun' require 'minitest/spec' require 'active_record' require 'active_record/deprecated_finders' def active_record_4_0? ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0 end ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') ActiveRecord::Schema.verbose = false ActiveRecord::Schema.define do create_table :posts do |t| t.string :title t.string :category end create_table :comments do |t| t.string :title t.references :post end create_table :appointments do |t| t.integer :physician_id t.integer :patient_id t.string :week_day t.string :status end create_table :physicians do |t| t.string :name end create_table :patients do |t| t.string :name end end class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base def self.lol "lol" end end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end class Patient < ActiveRecord::Base def self.find_by_custom_name [] end end class Physician < ActiveRecord::Base has_many :appointments has_many :patients, through: :appointments end require 'active_support/testing/deprecation' ActiveSupport::Deprecation.debug = true class MiniTest::Spec include ActiveSupport::Testing::Deprecation end activerecord-deprecated_finders-1.0.4/test/scope_test.rb000066400000000000000000000011741250666244700234710ustar00rootroot00000000000000require 'helper' describe 'scope' do before do @klass = Class.new(ActiveRecord::Base) end it 'supports a finder hash' do assert_deprecated { @klass.scope :foo, conditions: :foo } @klass.foo.where_values.must_equal [:foo] end it 'supports a finder hash inside a callable' do @klass.scope :foo, ->(v) { { conditions: v } } assert_deprecated { @klass.foo(:bar) }.where_values.must_equal [:bar] end it 'supports chaining' do @klass.scope :foo, -> { @klass.where(:foo) } assert_deprecated { @klass.scope :bar, conditions: :bar } @klass.foo.bar.where_values.must_equal [:foo, :bar] end end activerecord-deprecated_finders-1.0.4/test/scoped_test.rb000066400000000000000000000006121250666244700236310ustar00rootroot00000000000000require 'helper' describe 'scoped' do it 'accepts a deprecated conditions option' do assert_deprecated { Post.scoped(conditions: :foo) }.where_values.must_equal [:foo] end it 'accepts a deprecated include option' do assert_deprecated { Post.scoped(include: :foo) }.includes_values.must_equal [:foo] end it 'is deprecated' do assert_deprecated { Post.scoped } end end activerecord-deprecated_finders-1.0.4/test/update_all_test.rb000066400000000000000000000011071250666244700244660ustar00rootroot00000000000000require 'helper' describe 'update_all' do after do Post.destroy_all end it 'supports conditions' do foo = Post.create title: 'foo' bar = Post.create title: 'bar' assert_deprecated { Post.update_all({ title: 'omg' }, title: 'foo') } foo.reload.title.must_equal 'omg' bar.reload.title.must_equal 'bar' end it 'supports limit and order' do posts = 3.times.map { Post.create } assert_deprecated { Post.update_all({ title: 'omg' }, nil, limit: 2, order: :id) } posts.each(&:reload).map(&:title).must_equal ['omg', 'omg', nil] end end activerecord-deprecated_finders-1.0.4/test/with_scope_test.rb000066400000000000000000000014611250666244700245230ustar00rootroot00000000000000require 'helper' describe 'with_scope' do before do Post.create(id: 1, title: 'foo') Post.create(id: 2, title: 'bar') end after do Post.delete_all end it 'applies a scoping' do assert_deprecated do Post.with_scope(find: { conditions: { title: 'foo' } }) do assert_equal [1], Post.all.map(&:id) end end end it 'applies an exclusive scoping' do ActiveSupport::Deprecation.silence do Post.with_scope(find: { conditions: { title: 'foo' } }) do Post.send(:with_exclusive_scope, find: { conditions: { title: 'bar' } }) do assert_equal [2], Post.all.map(&:id) end end end end it 'gives a deprecation for #with_exclusive_scope' do assert_deprecated do Post.send(:with_exclusive_scope) {} end end end