Created
June 13, 2014 23:28
-
-
Save xymbol/1c7f08535ce02ab612b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "helper" | |
class CandidatesTest < MiniTest::Unit::TestCase | |
include FriendlyId::Test | |
class City < ActiveRecord::Base | |
extend FriendlyId | |
friendly_id :slug_candidates, use: :slugged | |
end | |
def model_class | |
City | |
end | |
def with_instances_of(klass, &block) | |
transaction do | |
city1 = klass.create! :name => "New York", :code => "JFK" | |
city2 = klass.create! :name => "New York", :code => "EWR" | |
yield city1, city2 | |
end | |
end | |
test "accepts multiple candidates" do | |
klass = Class.new model_class do | |
def slug_candidates | |
[:name, :code] | |
end | |
end | |
with_instances_of(klass) do |_, city| | |
assert_equal "ewr", city.slug | |
end | |
end | |
test "ignores blank candidate" do | |
klass = Class.new model_class do | |
def slug_candidates | |
[:name, ""] | |
end | |
end | |
with_instances_of(klass) do |_, city| | |
assert_match(/\Anew-york-([a-z0-9]+\-){4}[a-z0-9]+\z/, city.slug) | |
end | |
end | |
test "ignores nil candidate" do | |
klass = Class.new model_class do | |
def slug_candidates | |
[:name, nil] | |
end | |
end | |
with_instances_of(klass) do |_, city| | |
assert_match(/\Anew-york-([a-z0-9]+\-){4}[a-z0-9]+\z/, city.slug) | |
end | |
end | |
test "accepts candidate with nested array" do | |
klass = Class.new model_class do | |
def slug_candidates | |
[:name, [:name, :code]] | |
end | |
end | |
with_instances_of(klass) do |_, city| | |
assert_equal "new-york-ewr", city.slug | |
end | |
end | |
test "accepts candidate with lambda" do | |
klass = Class.new City do | |
def slug_candidates | |
[:name, [:name, ->{ rand 1000 }]] | |
end | |
end | |
with_instances_of(klass) do |_, city| | |
assert_match(/\Anew-york-\d+\z/, city.friendly_id) | |
end | |
end | |
test "accepts candidate with object" do | |
klass = Class.new City do | |
class A | |
alias_method :to_s, :object_id | |
end | |
def slug_candidates | |
[:name, [:name, A.new]] | |
end | |
end | |
with_instances_of(klass) do |_, city| | |
assert_match(/\Anew-york-\d+\z/, city.friendly_id) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment