friendly_id-globalize 1.0.0.alpha1 → 1.0.0.alpha2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/Gemfile +3 -3
- data/README.md +55 -1
- data/friendly_id-globalize.gemspec +4 -3
- data/lib/friendly_id/globalize.rb +16 -1
- data/lib/friendly_id/globalize/version.rb +2 -2
- data/test/globalize_test.rb +26 -19
- metadata +21 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c240c7dc5d34b3afb7338ae100c7202f04d73e96
|
4
|
+
data.tar.gz: f5655d526ff831df27bad81a74699c824123ffb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6b36a173d89897737d18071f84a55eddeeb9edf1afaf61cec54b703a9a2975e1e65ab632486a5873bd707ab994a366c2d6105bbe15fb1008c8b455de332f5a9
|
7
|
+
data.tar.gz: 07d69a9ec4616a7ba1da6f744cae485b6c1bb7a75abd5b379334492992f1d0a829de35b3dd78454f87193663488b00dcaf77f3d5ef5636fc1ac64b967c50a53b
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -4,9 +4,8 @@ gemspec
|
|
4
4
|
|
5
5
|
# Database Configuration
|
6
6
|
group :development, :test do
|
7
|
-
gem
|
8
|
-
gem '
|
9
|
-
gem 'friendly_id', github: 'norman/friendly_id', branch: 'master'
|
7
|
+
gem "globalize", '~> 5.0.0'
|
8
|
+
gem 'rake'
|
10
9
|
|
11
10
|
platforms :jruby do
|
12
11
|
gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.0.beta2'
|
@@ -20,3 +19,4 @@ group :development, :test do
|
|
20
19
|
gem 'pry'
|
21
20
|
gem 'pry-nav'
|
22
21
|
end
|
22
|
+
|
data/README.md
CHANGED
@@ -1,4 +1,58 @@
|
|
1
1
|
# FriendlyId Globalize
|
2
2
|
|
3
|
-
[
|
3
|
+
[Globalize](https://github.com/globalize/globalize) support for
|
4
4
|
[FriendlyId](https://github.com/norman/friendly_id).
|
5
|
+
|
6
|
+
### Translating Slugs Using Globalize
|
7
|
+
The `FriendlyId::Globalize Globalize` module lets you use
|
8
|
+
[Globalize](https://github.com/globalize/globalize) to translate slugs. This
|
9
|
+
module is most suitable for applications that need to be localized to many
|
10
|
+
languages. If your application only needs to be localized to one or two
|
11
|
+
languages, you may wish to consider the `FriendlyId::SimpleI18n SimpleI18n`
|
12
|
+
module.
|
13
|
+
|
14
|
+
In order to use this module, your model's table and translation table must both
|
15
|
+
have a slug column, and your model must set the `slug` field as translatable
|
16
|
+
with Globalize:
|
17
|
+
```ruby
|
18
|
+
class Post < ActiveRecord::Base
|
19
|
+
translates :title, :slug
|
20
|
+
extend FriendlyId
|
21
|
+
friendly_id :title, :use => :globalize
|
22
|
+
end
|
23
|
+
```
|
24
|
+
Note that call to `translates` must be made before calling `friendly_id`.
|
25
|
+
|
26
|
+
### Finds
|
27
|
+
Finds will take the current locale into consideration:
|
28
|
+
```ruby
|
29
|
+
I18n.locale = :it
|
30
|
+
Post.find("guerre-stellari")
|
31
|
+
I18n.locale = :en
|
32
|
+
Post.find("star-wars")
|
33
|
+
```
|
34
|
+
Additionally, finds will fall back to the default locale:
|
35
|
+
```ruby
|
36
|
+
I18n.locale = :it
|
37
|
+
Post.find("star-wars")
|
38
|
+
```
|
39
|
+
To find a slug by an explicit locale, perform the find inside a block
|
40
|
+
passed to I18n's `with_locale` method:
|
41
|
+
```ruby
|
42
|
+
I18n.with_locale(:it) { Post.find("guerre-stellari") }
|
43
|
+
```
|
44
|
+
### Creating Records
|
45
|
+
When new records are created, the slug is generated for the current locale only.
|
46
|
+
### Translating Slugs
|
47
|
+
To translate an existing record's friendly_id, use
|
48
|
+
`FriendlyId::Globalize::Model#set_friendly_id`. This will ensure that the slug
|
49
|
+
you add is properly escaped, transliterated and sequenced:
|
50
|
+
```ruby
|
51
|
+
post = Post.create :name => "Star Wars"
|
52
|
+
post.set_friendly_id("Guerre stellari", :it)
|
53
|
+
```
|
54
|
+
If you don't pass in a locale argument, `FriendlyId::Globalize` will just use the
|
55
|
+
current locale:
|
56
|
+
```ruby
|
57
|
+
I18n.with_locale(:it) { post.set_friendly_id("Guerre stellari") }
|
58
|
+
```
|
@@ -12,12 +12,13 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
13
13
|
s.require_paths = ['lib']
|
14
14
|
s.license = 'MIT'
|
15
|
-
s.description = 'Adds Globalize
|
15
|
+
s.description = 'Adds Globalize support to the FriendlyId gem.'
|
16
16
|
|
17
17
|
s.required_ruby_version = '>= 1.9.3'
|
18
18
|
|
19
|
-
s.add_dependency 'friendly_id', '
|
19
|
+
s.add_dependency 'friendly_id', '~> 5.1.0', '< 6.0'
|
20
20
|
s.add_development_dependency 'minitest'
|
21
21
|
s.add_development_dependency 'yard'
|
22
|
-
s.add_development_dependency "
|
22
|
+
s.add_development_dependency "globalize"
|
23
23
|
end
|
24
|
+
|
@@ -94,7 +94,22 @@ current locale:
|
|
94
94
|
end
|
95
95
|
|
96
96
|
def set_slug(normalized_slug = nil)
|
97
|
-
|
97
|
+
if self.translations.size > 1
|
98
|
+
self.translations.map(&:locale).each do |locale|
|
99
|
+
::Globalize.with_locale(locale) { super_set_slug(normalized_slug) }
|
100
|
+
end
|
101
|
+
else
|
102
|
+
::Globalize.with_locale(::Globalize.locale) { super_set_slug(normalized_slug) }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def super_set_slug(normalized_slug = nil)
|
107
|
+
if should_generate_new_friendly_id?
|
108
|
+
candidates = FriendlyId::Candidates.new(self, normalized_slug || send(friendly_id_config.base))
|
109
|
+
slug = slug_generator.generate(candidates) || resolve_friendly_id_conflict(candidates)
|
110
|
+
translation.slug = slug
|
111
|
+
end
|
98
112
|
end
|
99
113
|
end
|
100
114
|
end
|
115
|
+
|
data/test/globalize_test.rb
CHANGED
@@ -3,32 +3,34 @@ require 'bundler/setup'
|
|
3
3
|
require 'active_record'
|
4
4
|
require 'friendly_id'
|
5
5
|
require 'friendly_id/globalize'
|
6
|
-
require '
|
6
|
+
require 'globalize'
|
7
7
|
require 'minitest/autorun'
|
8
8
|
|
9
9
|
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
10
|
-
|
11
|
-
|
12
|
-
translates :slug, :title
|
13
|
-
extend FriendlyId
|
14
|
-
friendly_id :title, :use => [:slugged, :globalize]
|
15
|
-
end
|
10
|
+
::I18n.enforce_available_locales = false
|
11
|
+
Globalize.fallbacks = {:en => [:en, :de], :de => [:de, :en]}
|
16
12
|
|
17
13
|
class FriendlyIdGlobalizeTest < ActiveRecord::Migration
|
18
14
|
def self.up
|
19
15
|
create_table :articles do |t|
|
20
16
|
t.string :name
|
21
|
-
t.string :slug
|
22
17
|
end
|
23
|
-
|
24
|
-
add_index :articles, :slug, unique: true
|
25
|
-
Article.create_translation_table! :slug => :string, :title => :string
|
26
18
|
end
|
27
19
|
end
|
28
20
|
|
29
21
|
ActiveRecord::Migration.verbose = false
|
30
22
|
FriendlyIdGlobalizeTest.up
|
31
23
|
|
24
|
+
class Article < ActiveRecord::Base
|
25
|
+
translates :slug, :title, fallbacks_for_empty_translations: true
|
26
|
+
accepts_nested_attributes_for :translations
|
27
|
+
|
28
|
+
extend FriendlyId
|
29
|
+
friendly_id :title, :use => [:slugged, :globalize]
|
30
|
+
end
|
31
|
+
|
32
|
+
Article.create_translation_table! :slug => :string, :title => :string
|
33
|
+
|
32
34
|
class Module
|
33
35
|
def test(name, &block)
|
34
36
|
define_method("test_#{name.gsub(/[^a-z0-9']/i, "_")}".to_sym, &block)
|
@@ -53,18 +55,11 @@ class GlobalizeTest < MiniTest::Unit::TestCase
|
|
53
55
|
|
54
56
|
test 'should have a value for friendly_id after creation' do
|
55
57
|
transaction do
|
56
|
-
article = I18n.with_locale(:de) { Article.create!(:title => 'Der Herbst des Einsamen') }
|
58
|
+
article = ::I18n.with_locale(:de) { Article.create!(:title => 'Der Herbst des Einsamen') }
|
57
59
|
refute_nil article.friendly_id
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
|
-
test 'should allow nil friendly_ids' do
|
62
|
-
transaction do
|
63
|
-
article = I18n.with_locale(:de) { Article.create!(:title => nil) }
|
64
|
-
assert_nil article.reload.friendly_id
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
63
|
test "should find slug in current locale if locale is set, otherwise in default locale" do
|
69
64
|
transaction do
|
70
65
|
I18n.default_locale = :en
|
@@ -89,6 +84,17 @@ class GlobalizeTest < MiniTest::Unit::TestCase
|
|
89
84
|
end
|
90
85
|
end
|
91
86
|
|
87
|
+
test "should set all friendly ids for each nested translation" do
|
88
|
+
transaction do
|
89
|
+
article = Article.create!(translations_attributes: {
|
90
|
+
xx: { :title => "Guerra e pace", locale: 'it' },
|
91
|
+
yy: { title: 'Guerre et paix', locale: 'fr' }
|
92
|
+
})
|
93
|
+
I18n.with_locale(:it) { assert_equal "guerra-e-pace", article.friendly_id }
|
94
|
+
I18n.with_locale(:fr) { assert_equal "guerre-et-paix", article.friendly_id }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
92
98
|
# https://github.com/svenfuchs/globalize3/blob/master/test/globalize3/dynamic_finders_test.rb#L101
|
93
99
|
# see: https://github.com/svenfuchs/globalize3/issues/100
|
94
100
|
test "record returned by friendly_id should have all translations" do
|
@@ -104,3 +110,4 @@ class GlobalizeTest < MiniTest::Unit::TestCase
|
|
104
110
|
end
|
105
111
|
end
|
106
112
|
end
|
113
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: friendly_id-globalize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.alpha2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Norman Clarke
|
@@ -9,71 +9,71 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-07-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: friendly_id
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- -
|
18
|
+
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 5.
|
21
|
-
- - <
|
20
|
+
version: 5.1.0
|
21
|
+
- - "<"
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: '6.0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
27
27
|
requirements:
|
28
|
-
- -
|
28
|
+
- - "~>"
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
version: 5.
|
31
|
-
- - <
|
30
|
+
version: 5.1.0
|
31
|
+
- - "<"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '6.0'
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: minitest
|
36
36
|
requirement: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
type: :development
|
42
42
|
prerelease: false
|
43
43
|
version_requirements: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: yard
|
50
50
|
requirement: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
57
|
version_requirements: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
63
|
+
name: globalize
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
type: :development
|
70
70
|
prerelease: false
|
71
71
|
version_requirements: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
|
-
description: Adds Globalize
|
76
|
+
description: Adds Globalize support to the FriendlyId gem.
|
77
77
|
email:
|
78
78
|
- norman@njclarke.com
|
79
79
|
- p@arndt.io
|
@@ -81,8 +81,8 @@ executables: []
|
|
81
81
|
extensions: []
|
82
82
|
extra_rdoc_files: []
|
83
83
|
files:
|
84
|
-
- .gitignore
|
85
|
-
- .yardopts
|
84
|
+
- ".gitignore"
|
85
|
+
- ".yardopts"
|
86
86
|
- Gemfile
|
87
87
|
- MIT-LICENSE
|
88
88
|
- README.md
|
@@ -101,17 +101,17 @@ require_paths:
|
|
101
101
|
- lib
|
102
102
|
required_ruby_version: !ruby/object:Gem::Requirement
|
103
103
|
requirements:
|
104
|
-
- -
|
104
|
+
- - ">="
|
105
105
|
- !ruby/object:Gem::Version
|
106
106
|
version: 1.9.3
|
107
107
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
108
|
requirements:
|
109
|
-
- -
|
109
|
+
- - ">"
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: 1.3.1
|
112
112
|
requirements: []
|
113
113
|
rubyforge_project:
|
114
|
-
rubygems_version: 2.
|
114
|
+
rubygems_version: 2.4.6
|
115
115
|
signing_key:
|
116
116
|
specification_version: 4
|
117
117
|
summary: Globalize support for FriendlyId.
|