Skip to content

Commit

Permalink
Add tests for :scoped feature in friendly_id-mobility
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
bricesanchez authored and shioyama committed Apr 5, 2018
1 parent 89acc17 commit f5d952f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
57 changes: 57 additions & 0 deletions spec/friendly_id/mobility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,61 @@
end
end
end

describe "scoped" do
# Check that normal scoped functions are working, both with and without locale
# column on the slugs table.
describe "base features" do
it "detects scope column from belongs_to relation" do
expect(Novel.friendly_id_config.scope_columns).to eq(["publisher_id", "novelist_id"])
end

it "detects scope column from explicit column name" do
model_class = Class.new(ActiveRecord::Base) do
extend Mobility
translates :slug, :empty, type: :string, dirty: true, backend: :key_value

self.abstract_class = true
extend FriendlyId
friendly_id :empty, use: [:scoped, :mobility], scope: :dummy
end

expect(model_class.friendly_id_config.scope_columns).to eq(["dummy"])
end

it "allows duplicate slugs outside scope" do
novel1 = Novel.create! name: "a", novelist: Novelist.create!(name: "a")
novel2 = Novel.create! name: "a", novelist: Novelist.create!(name: "b")
expect(novel1.friendly_id).to eq(novel2.friendly_id)
end

it "does not allow duplicate slugs inside scope" do
novelist = Novelist.create!(name: "a")
novel1 = Novel.create! name: "a", novelist: novelist
novel2 = Novel.create! name: "a", novelist: novelist
expect(novel1.friendly_id).not_to eq(novel2.friendly_id)
end

it "applies scope with multiple columns" 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
expect(novel1.friendly_id).to eq(novel2.friendly_id)
expect(novel2.friendly_id).to eq(novel3.friendly_id)
expect(novel3.friendly_id).not_to eq(novel4.friendly_id)
end

it "allows a record to reuse its own slug" do
record = Novel.create!(name: "a")
old_id = record.friendly_id
record.slug = nil
record.save!

expect(record.friendly_id).to eq(old_id)
end
end
end
end
40 changes: 40 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def self.up
t.boolean :published
end

create_table :novels do |t|
t.integer :novelist_id
t.integer :publisher_id
end

create_table :novelists do |t|
end

create_table :publishers do |t|
end

create_table :mobility_string_translations do |t|
t.string :locale
t.string :key
Expand Down Expand Up @@ -82,6 +93,35 @@ class Post < ActiveRecord::Base
friendly_id :title, use: [:history, :mobility]
end

class Novelist < ActiveRecord::Base
extend Mobility
translates :slug, :name, type: :string, dirty: true, backend: :key_value

extend FriendlyId
friendly_id :name, use: [:mobility, :slugged]
end

class Novel < ActiveRecord::Base
extend Mobility
translates :slug, :name, type: :string, dirty: true, backend: :key_value

extend FriendlyId
belongs_to :novelist
belongs_to :publisher
friendly_id :name, use: [:mobility, :scoped], scope: [:publisher, :novelist]

def should_generate_new_friendly_id?
new_record? || super
end
end

class Publisher < ActiveRecord::Base
extend Mobility
translates :name, type: :string, dirty: true, backend: :key_value

has_many :novels
end

ActiveRecord::Migration.verbose = false
FriendlyIdMobilityTest.up

Expand Down

0 comments on commit f5d952f

Please sign in to comment.