1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
require "helper"
class Novelist < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
end
class Novel < ActiveRecord::Base
extend FriendlyId
belongs_to :novelist
belongs_to :publisher
friendly_id :name, use: :scoped, scope: [:publisher, :novelist]
def should_generate_new_friendly_id?
new_record? || super
end
end
class Publisher < ActiveRecord::Base
has_many :novels
end
class ScopedTest < TestCaseClass
include FriendlyId::Test
include FriendlyId::Test::Shared::Core
def model_class
Novel
end
test "should detect scope column from belongs_to relation" do
assert_equal ["publisher_id", "novelist_id"], Novel.friendly_id_config.scope_columns
end
test "should detect scope column from explicit column name" do
model_class = Class.new(ActiveRecord::Base) do
self.abstract_class = true
extend FriendlyId
friendly_id :empty, use: :scoped, scope: :dummy
end
assert_equal ["dummy"], model_class.friendly_id_config.scope_columns
end
test "should allow duplicate slugs outside scope" do
transaction do
novel1 = Novel.create! name: "a", novelist: Novelist.create!(name: "a")
novel2 = Novel.create! name: "a", novelist: Novelist.create!(name: "b")
assert_equal novel1.friendly_id, novel2.friendly_id
end
end
test "should not allow duplicate slugs inside scope" do
with_instance_of Novelist do |novelist|
novel1 = Novel.create! name: "a", novelist: novelist
novel2 = Novel.create! name: "a", novelist: novelist
assert novel1.friendly_id != novel2.friendly_id
end
end
test "should apply scope with multiple columns" do
transaction do
novelist = Novelist.create! name: "a"
publisher = Publisher.create! name: "b"
novel1 = Novel.create! name: "c", novelist: novelist, publisher: publisher
novel2 = Novel.create! name: "c", novelist: novelist, publisher: Publisher.create(name: "d")
novel3 = Novel.create! name: "c", novelist: Novelist.create(name: "e"), publisher: publisher
novel4 = Novel.create! name: "c", novelist: novelist, publisher: publisher
assert_equal novel1.friendly_id, novel2.friendly_id
assert_equal novel2.friendly_id, novel3.friendly_id
assert novel3.friendly_id != novel4.friendly_id
end
end
test "should allow a record to reuse its own slug" do
with_instance_of(model_class) do |record|
old_id = record.friendly_id
record.slug = nil
record.save!
assert_equal old_id, record.friendly_id
end
end
test "should generate new slug when scope changes" do
transaction do
novelist = Novelist.create! name: "a"
publisher = Publisher.create! name: "b"
novel1 = Novel.create! name: "c", novelist: novelist, publisher: publisher
novel2 = Novel.create! name: "c", novelist: novelist, publisher: Publisher.create(name: "d")
assert_equal novel1.friendly_id, novel2.friendly_id
novel2.publisher = publisher
novel2.save!
assert novel2.friendly_id != novel1.friendly_id
end
end
end
|