Skip to content

Commit

Permalink
Merge pull request #1 from januszm/rails_4_plus
Browse files Browse the repository at this point in the history
Use new ActiveRecord Query Interface, compatibility with Rails 4+
  • Loading branch information
alexreisner authored Sep 4, 2016
2 parents 64fc88d + 7f8bb0c commit c683ff9
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= Handler

<b>Handler is currently compatible with Rails 2.x and Rails 3.</b>
<b>Handler is currently compatible with Rails 3.x and Rails 4+.</b> 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:

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.0
0.9.0
8 changes: 6 additions & 2 deletions handler.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions test/handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit c683ff9

Please sign in to comment.