File: base_test.rb

package info (click to toggle)
ruby-friendly-id 5.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: ruby: 3,143; makefile: 3
file content (69 lines) | stat: -rw-r--r-- 1,949 bytes parent folder | download
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
require "helper"

class CoreTest < TestCaseClass
  include FriendlyId::Test

  test "friendly_id can be added using 'extend'" do
    klass = Class.new(ActiveRecord::Base) do
      extend FriendlyId
    end
    assert klass.respond_to? :friendly_id
  end

  test "friendly_id can be added using 'include'" do
    klass = Class.new(ActiveRecord::Base) do
      include FriendlyId
    end
    assert klass.respond_to? :friendly_id
  end

  test "friendly_id should accept a base and a hash" do
    klass = Class.new(ActiveRecord::Base) do
      self.abstract_class = true
      extend FriendlyId
      friendly_id :foo, use: :slugged, slug_column: :bar
    end
    assert klass < FriendlyId::Slugged
    assert_equal :foo, klass.friendly_id_config.base
    assert_equal :bar, klass.friendly_id_config.slug_column
  end

  test "friendly_id should accept a block" do
    klass = Class.new(ActiveRecord::Base) do
      self.abstract_class = true
      extend FriendlyId
      friendly_id :foo do |config|
        config.use :slugged
        config.base = :foo
        config.slug_column = :bar
      end
    end
    assert klass < FriendlyId::Slugged
    assert_equal :foo, klass.friendly_id_config.base
    assert_equal :bar, klass.friendly_id_config.slug_column
  end

  test "the block passed to friendly_id should be evaluated before arguments" do
    klass = Class.new(ActiveRecord::Base) do
      self.abstract_class = true
      extend FriendlyId
      friendly_id :foo do |config|
        config.base = :bar
      end
    end
    assert_equal :foo, klass.friendly_id_config.base
  end

  test "should allow defaults to be set via a block" do
    FriendlyId.defaults do |config|
      config.base = :foo
    end
    klass = Class.new(ActiveRecord::Base) do
      self.abstract_class = true
      extend FriendlyId
    end
    assert_equal :foo, klass.friendly_id_config.base
  ensure
    FriendlyId.instance_variable_set :@defaults, nil
  end
end