Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exists? to behave the same as find for numeric slugs #875

Merged
merged 2 commits into from
Jun 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/friendly_id/finder_methods.rb
Original file line number Diff line number Diff line change
@@ -25,8 +25,9 @@ def find(*args)

# Returns true if a record with the given id exists.
def exists?(conditions = :none)
return super unless conditions.friendly_id?
exists_by_friendly_id?(conditions)
return super if conditions.unfriendly_id?
return true if exists_by_friendly_id?(conditions)
super
end

# Finds exclusively by the friendly id, completely bypassing original
31 changes: 31 additions & 0 deletions test/numeric_slug_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'helper'

class NumericSlugTest < TestCaseClass
include FriendlyId::Test
include FriendlyId::Test::Shared::Core

def model_class
Article
end

test "should generate numeric slugs" do
transaction do
record = model_class.create! :name => "123"
assert_equal "123", record.slug
end
end

test "should find by numeric slug" do
transaction do
record = model_class.create! :name => "123"
assert_equal model_class.friendly.find("123").id, record.id
end
end

test "should exist? by numeric slug" do
transaction do
record = model_class.create! :name => "123"
assert model_class.friendly.exists?("123")
end
end
end