From 7f8bb0c1bdfd35d02799096ed48045614c89e514 Mon Sep 17 00:00:00 2001 From: Janusz Mordarski Date: Sun, 4 Sep 2016 12:16:38 +0200 Subject: [PATCH] Use new ActiveRecord Query Interface, compatibility with Rails 4+ --- CHANGELOG.rdoc | 3 +++ README.rdoc | 2 +- Rakefile | 2 +- VERSION | 2 +- handler.gemspec | 8 ++++++-- lib/handler.rb | 4 ++-- test/handler_test.rb | 19 +++++++++++-------- test/test_helper.rb | 2 +- 8 files changed, 26 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 0f07ad8..96d2b25 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -2,6 +2,9 @@ Per-release changes to Handler. +== 0.9.0 (2016 Sep 5) + +Compatibility with ActiveRecord 4+ (use new AR Query Interface). == 0.8.0 (2009 Oct 12) diff --git a/README.rdoc b/README.rdoc index 2bd5c74..e7d439e 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,6 +1,6 @@ = Handler -Handler is currently compatible with Rails 2.x and Rails 3. +Handler is currently compatible with Rails 3.x and Rails 4+. For Rails 2.x use version 0.8.0. Handler is a Rails plugin that generates handles (filesystem- and URL-friendly names) for ActiveRecord models based on a given attribute or method. For example, in your model: diff --git a/Rakefile b/Rakefile index e5077da..e3061a5 100644 --- a/Rakefile +++ b/Rakefile @@ -41,7 +41,7 @@ task :test => :check_dependencies task :default => :test -require 'rake/rdoctask' +require 'rdoc/task' Rake::RDocTask.new do |rdoc| if File.exist?('VERSION') version = File.read('VERSION') diff --git a/VERSION b/VERSION index a3df0a6..ac39a10 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.8.0 +0.9.0 diff --git a/handler.gemspec b/handler.gemspec index 98f4328..202630c 100644 --- a/handler.gemspec +++ b/handler.gemspec @@ -5,7 +5,7 @@ Gem::Specification.new do |s| s.name = %q{handler} - s.version = "0.8.0" + s.version = "0.9.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Alex Reisner"] @@ -36,9 +36,13 @@ Gem::Specification.new do |s| s.summary = %q{Handler generates filesystem- and URL-friendly names for ActiveRecord models.} s.test_files = [ "test/handler_test.rb", - "test/test_helper.rb" + "test/test_helper.rb" ] + s.add_dependency 'activesupport', '>= 3.0' + s.add_dependency 'activerecord', '>= 3.0' + s.add_development_dependency 'minitest' + if s.respond_to? :specification_version then current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 3 diff --git a/lib/handler.rb b/lib/handler.rb index ad8cfb2..b4b2449 100644 --- a/lib/handler.rb +++ b/lib/handler.rb @@ -84,7 +84,7 @@ def self.next_handle(handle, separator) end end - module ClassMethod + module ClassMethods ## # Declare that a model generates a handle based on @@ -119,7 +119,7 @@ def handle_based_on(attribute, options = {}) # increase number while *other* records exist with the same handle # (record might be saved and should keep its handle) - while self.class.all(:conditions => find_dupe.call(h)).size > 0 + while self.class.where(find_dupe.call(h)).size > 0 h = Handler.next_handle(h, options[:separator]) end end diff --git a/test/handler_test.rb b/test/handler_test.rb index 2d9b390..778f8af 100644 --- a/test/handler_test.rb +++ b/test/handler_test.rb @@ -24,7 +24,7 @@ class HandlerTest < ActiveSupport::TestCase ".38 Special" => "38_special", "Guns N' Roses" => "guns_n_roses", "The Rossington-Collins Band" => "the_rossington_collins_band" - + }.each do |s,t| b.name = s assert_equal t, b.generate_handle @@ -37,19 +37,19 @@ class HandlerTest < ActiveSupport::TestCase "Y&T" => "y_and_t", "Huey Lewis & the News" => "huey_lewis_and_the_news", "Emerson, Lake & Palmer" => "emerson_lake_and_palmer" - + }.each do |s,t| b.name = s assert_equal t, b.generate_handle end end - + test "next handle" do assert_equal "banana_boat_2", Handler.next_handle("banana_boat", "_") assert_equal "banana-boat-3", Handler.next_handle("banana-boat-2", "-") assert_equal "banana-boat-12", Handler.next_handle("banana-boat-11", "-") end - + test "unique handle generation" do p = Person.new p.name = "Captain Beefheart" @@ -62,14 +62,17 @@ class HandlerTest < ActiveSupport::TestCase class ActiveRecordSimulator include Handler attr_accessor :name - + # simulate id method def id; 123; end - # simulate ActiveRecord::Base.all method; + # simulate new_record? method + def new_record?; true; end + + # simulate ActiveRecord::Base.where method; # needed for testing whether records exist with a given handle - def self.all(options) - @existing_handles.include?(options[:conditions][1]) ? [nil, nil] : [] + def self.where(options) + @existing_handles.include?(options[1]) ? [nil, nil] : [] end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 4e1bb33..8800fc3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,7 +1,7 @@ require 'rubygems' require 'active_support' require 'active_support/test_case' -require 'test/unit' +require 'minitest/autorun' # required to avoid errors module ActiveRecord