friendly_id-globalize 1.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.yardopts +3 -0
- data/Gemfile +22 -0
- data/MIT-LICENSE +19 -0
- data/README.md +4 -0
- data/Rakefile +20 -0
- data/friendly_id-globalize.gemspec +23 -0
- data/lib/friendly_id/globalize.rb +100 -0
- data/lib/friendly_id/globalize/version.rb +5 -0
- data/test/globalize_test.rb +106 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 62cd4376867d5bac0911aee989a1772629a02456
|
4
|
+
data.tar.gz: 1b996d875da46468e07477f4c6b4ca293c403730
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 34e2632179917e0fd224e2bf296a55d31ade506f83f16fbae8191be5adb795949bffff910726b3bc94a435af68617e51e7be6006766c3ed0cea25b454b86433d
|
7
|
+
data.tar.gz: cef14323a9b9c071b823a0b059ada2becfa590548634061d6151ba4d4f9ca3a5bbd4beb8016f92c3909b79e9d718996c4c271b4f5bc7d385a32916b3e0350924
|
data/.gitignore
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
# Database Configuration
|
6
|
+
group :development, :test do
|
7
|
+
gem 'globalize3', github: 'svenfuchs/globalize3', branch: 'rails4'
|
8
|
+
gem 'paper_trail', github: 'airblade/paper_trail', branch: 'master'
|
9
|
+
gem 'friendly_id', github: 'norman/friendly_id', branch: 'master'
|
10
|
+
|
11
|
+
platforms :jruby do
|
12
|
+
gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.0.beta2'
|
13
|
+
gem 'jruby-openssl'
|
14
|
+
end
|
15
|
+
|
16
|
+
platforms :ruby do
|
17
|
+
gem 'sqlite3'
|
18
|
+
end
|
19
|
+
|
20
|
+
gem 'pry'
|
21
|
+
gem 'pry-nav'
|
22
|
+
end
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2013 Norman Clarke and Philip Arndt
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
task :default => :test
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << "test"
|
7
|
+
t.test_files = FileList['test/*_test.rb']
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "Remove temporary files"
|
12
|
+
task :clean do
|
13
|
+
%x{rm -rf *.gem doc pkg coverage}
|
14
|
+
%x{rm -f `find . -name '*.rbc'`}
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Build the gem"
|
18
|
+
task :gem do
|
19
|
+
%x{gem build friendly_id-globalize.gemspec}
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path('../lib/friendly_id/globalize/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'friendly_id-globalize'
|
6
|
+
s.version = FriendlyId::Globalize::VERSION
|
7
|
+
s.authors = ['Norman Clarke', 'Philip Arndt']
|
8
|
+
s.email = ['norman@njclarke.com', 'p@arndt.io']
|
9
|
+
s.homepage = 'http://github.com/norman/friendly_id-globalize'
|
10
|
+
s.summary = 'Globalize support for FriendlyId.'
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
13
|
+
s.require_paths = ['lib']
|
14
|
+
s.license = 'MIT'
|
15
|
+
s.description = 'Adds Globalize 3 support to the FriendlyId gem.'
|
16
|
+
|
17
|
+
s.required_ruby_version = '>= 1.9.3'
|
18
|
+
|
19
|
+
s.add_dependency 'friendly_id', '>= 5.0.0.beta4', '< 6.0'
|
20
|
+
s.add_development_dependency 'minitest'
|
21
|
+
s.add_development_dependency 'yard'
|
22
|
+
s.add_development_dependency "globalize3"
|
23
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
|
3
|
+
module FriendlyId
|
4
|
+
|
5
|
+
=begin
|
6
|
+
|
7
|
+
== Translating Slugs Using Globalize
|
8
|
+
|
9
|
+
The {FriendlyId::Globalize Globalize} module lets you use
|
10
|
+
Globalize[https://github.com/svenfuchs/globalize3] to translate slugs. This
|
11
|
+
module is most suitable for applications that need to be localized to many
|
12
|
+
languages. If your application only needs to be localized to one or two
|
13
|
+
languages, you may wish to consider the {FriendlyId::SimpleI18n SimpleI18n}
|
14
|
+
module.
|
15
|
+
|
16
|
+
In order to use this module, your model's table and translation table must both
|
17
|
+
have a slug column, and your model must set the +slug+ field as translatable
|
18
|
+
with Globalize:
|
19
|
+
|
20
|
+
class Post < ActiveRecord::Base
|
21
|
+
translates :title, :slug
|
22
|
+
extend FriendlyId
|
23
|
+
friendly_id :title, :use => :globalize
|
24
|
+
end
|
25
|
+
|
26
|
+
=== Finds
|
27
|
+
|
28
|
+
Finds will take the current locale into consideration:
|
29
|
+
|
30
|
+
I18n.locale = :it
|
31
|
+
Post.find("guerre-stellari")
|
32
|
+
I18n.locale = :en
|
33
|
+
Post.find("star-wars")
|
34
|
+
|
35
|
+
Additionally, finds will fall back to the default locale:
|
36
|
+
|
37
|
+
I18n.locale = :it
|
38
|
+
Post.find("star-wars")
|
39
|
+
|
40
|
+
To find a slug by an explicit locale, perform the find inside a block
|
41
|
+
passed to I18n's +with_locale+ method:
|
42
|
+
|
43
|
+
I18n.with_locale(:it) { Post.find("guerre-stellari") }
|
44
|
+
|
45
|
+
=== Creating Records
|
46
|
+
|
47
|
+
When new records are created, the slug is generated for the current locale only.
|
48
|
+
|
49
|
+
=== Translating Slugs
|
50
|
+
|
51
|
+
To translate an existing record's friendly_id, use
|
52
|
+
{FriendlyId::Globalize::Model#set_friendly_id}. This will ensure that the slug
|
53
|
+
you add is properly escaped, transliterated and sequenced:
|
54
|
+
|
55
|
+
post = Post.create :name => "Star Wars"
|
56
|
+
post.set_friendly_id("Guerre stellari", :it)
|
57
|
+
|
58
|
+
If you don't pass in a locale argument, FriendlyId::Globalize will just use the
|
59
|
+
current locale:
|
60
|
+
|
61
|
+
I18n.with_locale(:it) { post.set_friendly_id("Guerre stellari") }
|
62
|
+
|
63
|
+
=end
|
64
|
+
module Globalize
|
65
|
+
class << self
|
66
|
+
|
67
|
+
def setup(model_class)
|
68
|
+
model_class.friendly_id_config.use :slugged
|
69
|
+
end
|
70
|
+
|
71
|
+
def included(model_class)
|
72
|
+
advise_against_untranslated_model(model_class)
|
73
|
+
end
|
74
|
+
|
75
|
+
def advise_against_untranslated_model(model)
|
76
|
+
field = model.friendly_id_config.query_field
|
77
|
+
unless model.respond_to?('translated_attribute_names') ||
|
78
|
+
model.translated_attribute_names.exclude?(field.to_sym)
|
79
|
+
raise "[FriendlyId] You need to translate the '#{field}' field with " \
|
80
|
+
"Globalize (add 'translates :#{field}' in your model '#{model.name}')"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
private :advise_against_untranslated_model
|
84
|
+
end
|
85
|
+
|
86
|
+
def set_friendly_id(text, locale = nil)
|
87
|
+
::Globalize.with_locale(locale || ::Globalize.locale) do
|
88
|
+
set_slug normalize_friendly_id(text)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def should_generate_new_friendly_id?
|
93
|
+
translation_for(::Globalize.locale).send(friendly_id_config.slug_column).nil?
|
94
|
+
end
|
95
|
+
|
96
|
+
def set_slug(normalized_slug = nil)
|
97
|
+
::Globalize.with_locale(::Globalize.locale) { super }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'active_record'
|
4
|
+
require 'friendly_id'
|
5
|
+
require 'friendly_id/globalize'
|
6
|
+
require 'globalize3'
|
7
|
+
require 'minitest/autorun'
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
10
|
+
|
11
|
+
class Article < ActiveRecord::Base
|
12
|
+
translates :slug, :title
|
13
|
+
extend FriendlyId
|
14
|
+
friendly_id :title, :use => [:slugged, :globalize]
|
15
|
+
end
|
16
|
+
|
17
|
+
class FriendlyIdGlobalizeTest < ActiveRecord::Migration
|
18
|
+
def self.up
|
19
|
+
create_table :articles do |t|
|
20
|
+
t.string :name
|
21
|
+
t.string :slug
|
22
|
+
end
|
23
|
+
|
24
|
+
add_index :articles, :slug, unique: true
|
25
|
+
Article.create_translation_table! :slug => :string, :title => :string
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
ActiveRecord::Migration.verbose = false
|
30
|
+
FriendlyIdGlobalizeTest.up
|
31
|
+
|
32
|
+
class Module
|
33
|
+
def test(name, &block)
|
34
|
+
define_method("test_#{name.gsub(/[^a-z0-9']/i, "_")}".to_sym, &block)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class GlobalizeTest < MiniTest::Unit::TestCase
|
39
|
+
|
40
|
+
def transaction
|
41
|
+
ActiveRecord::Base.transaction { yield ; raise ActiveRecord::Rollback }
|
42
|
+
end
|
43
|
+
|
44
|
+
def with_instance_of(*args)
|
45
|
+
model_class = args.shift
|
46
|
+
args[0] ||= {:name => "a b c"}
|
47
|
+
transaction { yield model_class.create!(*args) }
|
48
|
+
end
|
49
|
+
|
50
|
+
def setup
|
51
|
+
I18n.locale = :en
|
52
|
+
end
|
53
|
+
|
54
|
+
test 'should have a value for friendly_id after creation' do
|
55
|
+
transaction do
|
56
|
+
article = I18n.with_locale(:de) { Article.create!(:title => 'Der Herbst des Einsamen') }
|
57
|
+
refute_nil article.friendly_id
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
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
|
+
test "should find slug in current locale if locale is set, otherwise in default locale" do
|
69
|
+
transaction do
|
70
|
+
I18n.default_locale = :en
|
71
|
+
article_en = I18n.with_locale(:en) { Article.create!(:title => 'a title') }
|
72
|
+
article_de = I18n.with_locale(:de) { Article.create!(:title => 'titel') }
|
73
|
+
|
74
|
+
I18n.with_locale(:de) do
|
75
|
+
assert_equal Article.friendly.find("titel"), article_de
|
76
|
+
assert_equal Article.friendly.find("a-title"), article_en
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
test "should set friendly id for locale" do
|
82
|
+
transaction do
|
83
|
+
article = Article.create!(:title => "War and Peace")
|
84
|
+
article.set_friendly_id("Guerra y paz", :es)
|
85
|
+
article.save!
|
86
|
+
found_article = Article.friendly.find('war-and-peace')
|
87
|
+
I18n.with_locale(:es) { assert_equal "guerra-y-paz", found_article.friendly_id }
|
88
|
+
I18n.with_locale(:en) { assert_equal "war-and-peace", found_article.friendly_id }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# https://github.com/svenfuchs/globalize3/blob/master/test/globalize3/dynamic_finders_test.rb#L101
|
93
|
+
# see: https://github.com/svenfuchs/globalize3/issues/100
|
94
|
+
test "record returned by friendly_id should have all translations" do
|
95
|
+
transaction do
|
96
|
+
I18n.with_locale(:en) do
|
97
|
+
article = Article.create!(:title => 'a title')
|
98
|
+
Globalize.with_locale(:de) {article.update_attribute(:title, 'ein titel')}
|
99
|
+
article_by_friendly_id = Article.friendly.find("a-title")
|
100
|
+
article.translations.each do |translation|
|
101
|
+
assert_includes article_by_friendly_id.translations, translation
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: friendly_id-globalize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.alpha1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Norman Clarke
|
8
|
+
- Philip Arndt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: friendly_id
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 5.0.0.beta4
|
21
|
+
- - <
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '6.0'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 5.0.0.beta4
|
31
|
+
- - <
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '6.0'
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: minitest
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
type: :development
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: yard
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: globalize3
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
type: :development
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
description: Adds Globalize 3 support to the FriendlyId gem.
|
77
|
+
email:
|
78
|
+
- norman@njclarke.com
|
79
|
+
- p@arndt.io
|
80
|
+
executables: []
|
81
|
+
extensions: []
|
82
|
+
extra_rdoc_files: []
|
83
|
+
files:
|
84
|
+
- .gitignore
|
85
|
+
- .yardopts
|
86
|
+
- Gemfile
|
87
|
+
- MIT-LICENSE
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- friendly_id-globalize.gemspec
|
91
|
+
- lib/friendly_id/globalize.rb
|
92
|
+
- lib/friendly_id/globalize/version.rb
|
93
|
+
- test/globalize_test.rb
|
94
|
+
homepage: http://github.com/norman/friendly_id-globalize
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: 1.9.3
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - '>'
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: 1.3.1
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.0.3
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Globalize support for FriendlyId.
|
118
|
+
test_files: []
|
119
|
+
has_rdoc:
|