File: simple_i18n_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 (144 lines) | stat: -rw-r--r-- 4,441 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require "helper"

class SimpleI18nTest < TestCaseClass
  include FriendlyId::Test

  class Journalist < ActiveRecord::Base
    extend FriendlyId
    friendly_id :name, use: :simple_i18n
  end

  def setup
    I18n.locale = :en
  end

  test "friendly_id should return the current locale's slug" do
    journalist = Journalist.new(name: "John Doe")
    journalist.slug_es = "juan-fulano"
    journalist.slug_fr_ca = "jean-dupont"
    journalist.valid?
    I18n.with_locale(I18n.default_locale) do
      assert_equal "john-doe", journalist.friendly_id
    end
    I18n.with_locale(:es) do
      assert_equal "juan-fulano", journalist.friendly_id
    end
    I18n.with_locale(:"fr-CA") do
      assert_equal "jean-dupont", journalist.friendly_id
    end
  end

  test "should create record with slug in column for the current locale" do
    I18n.with_locale(I18n.default_locale) do
      journalist = Journalist.new(name: "John Doe")
      journalist.valid?
      assert_equal "john-doe", journalist.slug_en
      assert_nil journalist.slug_es
    end
    I18n.with_locale(:es) do
      journalist = Journalist.new(name: "John Doe")
      journalist.valid?
      assert_equal "john-doe", journalist.slug_es
      assert_nil journalist.slug_en
    end
  end

  test "to_param should return the numeric id when there's no slug for the current locale" do
    transaction do
      journalist = Journalist.new(name: "Juan Fulano")
      I18n.with_locale(:es) do
        journalist.save!
        assert_equal "juan-fulano", journalist.to_param
      end
      assert_equal journalist.id.to_s, journalist.to_param
    end
  end

  test "should set friendly id for locale" do
    transaction do
      journalist = Journalist.create!(name: "John Smith")
      journalist.set_friendly_id("Juan Fulano", :es)
      journalist.save!
      assert_equal "juan-fulano", journalist.slug_es
      I18n.with_locale(:es) do
        assert_equal "juan-fulano", journalist.to_param
      end
    end
  end

  test "set friendly_id should fall back default locale when none is given" do
    transaction do
      journalist = I18n.with_locale(:es) do
        Journalist.create!(name: "Juan Fulano")
      end
      journalist.set_friendly_id("John Doe")
      journalist.save!
      assert_equal "john-doe", journalist.slug_en
    end
  end

  test "should sequence localized slugs" do
    transaction do
      journalist = Journalist.create!(name: "John Smith")
      I18n.with_locale(:es) do
        Journalist.create!(name: "Juan Fulano")
      end
      journalist.set_friendly_id("Juan Fulano", :es)
      journalist.save!
      assert_equal "john-smith", journalist.to_param
      I18n.with_locale(:es) do
        assert_match(/juan-fulano-.+/, journalist.to_param)
      end
    end
  end

  class RegressionTest < TestCaseClass
    include FriendlyId::Test

    test "should not overwrite other locale's slugs on update" do
      transaction do
        journalist = Journalist.create!(name: "John Smith")
        journalist.set_friendly_id("Juan Fulano", :es)
        journalist.save!
        assert_equal "john-smith", journalist.to_param
        journalist.slug = nil
        journalist.update name: "Johnny Smith"
        assert_equal "johnny-smith", journalist.to_param
        I18n.with_locale(:es) do
          assert_equal "juan-fulano", journalist.to_param
        end
      end
    end
  end

  class ConfigurationTest < TestCaseClass
    test "should add locale to slug column for a non-default locale" do
      I18n.with_locale :es do
        assert_equal "slug_es", Journalist.friendly_id_config.slug_column
      end
    end

    test "should add locale to slug column for a locale with a region subtag" do
      I18n.with_locale :"fr-CA" do
        assert_equal "slug_fr_ca", Journalist.friendly_id_config.slug_column
      end
    end

    test "should add locale to non-default slug column and non-default locale" do
      model_class = Class.new(ActiveRecord::Base) do
        self.abstract_class = true
        extend FriendlyId
        friendly_id :name, use: :simple_i18n, slug_column: :foo
      end
      I18n.with_locale :es do
        assert_equal "foo_es", model_class.friendly_id_config.slug_column
      end
    end

    test "should add locale to slug column for default locale" do
      I18n.with_locale(I18n.default_locale) do
        assert_equal "slug_en", Journalist.friendly_id_config.slug_column
      end
    end
  end
end