friendly_id 4.1.0.beta.1 → 5.0.0.alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +7 -12
- data/Changelog.md +4 -0
- data/Gemfile +2 -2
- data/Guide.rdoc +34 -63
- data/README.md +116 -60
- data/Rakefile +5 -1
- data/WhatsNew.md +2 -94
- data/friendly_id.gemspec +10 -20
- data/gemfiles/Gemfile.rails-4.0.rb +19 -0
- data/lib/friendly_id.rb +11 -6
- data/lib/friendly_id/.gitattributes +1 -0
- data/lib/friendly_id/base.rb +6 -65
- data/lib/friendly_id/candidates.rb +41 -0
- data/lib/friendly_id/configuration.rb +10 -6
- data/lib/friendly_id/history.rb +22 -60
- data/lib/friendly_id/object_utils.rb +1 -1
- data/lib/friendly_id/scoped.rb +40 -34
- data/lib/friendly_id/scopes.rb +28 -0
- data/lib/friendly_id/simple_i18n.rb +13 -3
- data/lib/friendly_id/slug.rb +0 -1
- data/lib/friendly_id/slug_generator.rb +9 -68
- data/lib/friendly_id/slugged.rb +22 -34
- data/lib/friendly_id/version.rb +3 -0
- data/lib/generators/friendly_id_generator.rb +5 -4
- data/test/helper.rb +1 -1
- data/test/history_test.rb +63 -56
- data/test/object_utils_test.rb +0 -1
- data/test/schema.rb +1 -10
- data/test/shared.rb +27 -23
- data/test/simple_i18n_test.rb +3 -2
- data/test/slugged_test.rb +17 -48
- data/test/sti_test.rb +2 -2
- metadata +40 -103
- data/gemfiles/Gemfile.rails-3.0.rb +0 -21
- data/gemfiles/Gemfile.rails-3.1.rb +0 -22
- data/gemfiles/Gemfile.rails-3.2.rb +0 -22
- data/lib/friendly_id/finder_methods.rb +0 -35
- data/lib/friendly_id/globalize.rb +0 -115
- data/test/globalize_test.rb +0 -57
@@ -1,21 +0,0 @@
|
|
1
|
-
source :rubygems
|
2
|
-
|
3
|
-
platforms :jruby do
|
4
|
-
gem 'activerecord-jdbcsqlite3-adapter'
|
5
|
-
gem 'activerecord-jdbcmysql-adapter'
|
6
|
-
gem 'activerecord-jdbcpostgresql-adapter'
|
7
|
-
gem 'jruby-openssl'
|
8
|
-
end
|
9
|
-
|
10
|
-
platforms :ruby do
|
11
|
-
gem 'sqlite3'
|
12
|
-
gem 'mysql2', '~> 0.2.0'
|
13
|
-
gem 'pg'
|
14
|
-
end
|
15
|
-
|
16
|
-
gem 'activerecord', '~> 3.0.0'
|
17
|
-
gem 'railties', '~> 3.0.0'
|
18
|
-
gem 'minitest', '3.2.0'
|
19
|
-
gem 'mocha'
|
20
|
-
gem 'rake'
|
21
|
-
gem 'globalize3'
|
@@ -1,22 +0,0 @@
|
|
1
|
-
source :rubygems
|
2
|
-
|
3
|
-
platforms :jruby do
|
4
|
-
gem 'activerecord-jdbcsqlite3-adapter'
|
5
|
-
gem 'activerecord-jdbcmysql-adapter'
|
6
|
-
gem 'activerecord-jdbcpostgresql-adapter'
|
7
|
-
gem 'jruby-openssl'
|
8
|
-
end
|
9
|
-
|
10
|
-
platforms :ruby do
|
11
|
-
gem 'sqlite3'
|
12
|
-
gem 'mysql2'
|
13
|
-
gem 'pg'
|
14
|
-
end
|
15
|
-
|
16
|
-
gem 'activerecord', '~> 3.1.0'
|
17
|
-
gem 'railties', '~> 3.1.3'
|
18
|
-
gem 'ffaker'
|
19
|
-
gem 'minitest', '3.2.0'
|
20
|
-
gem 'mocha'
|
21
|
-
gem 'rake'
|
22
|
-
gem 'globalize3'
|
@@ -1,22 +0,0 @@
|
|
1
|
-
source :rubygems
|
2
|
-
|
3
|
-
platforms :jruby do
|
4
|
-
gem 'activerecord-jdbcsqlite3-adapter'
|
5
|
-
gem 'activerecord-jdbcmysql-adapter'
|
6
|
-
gem 'activerecord-jdbcpostgresql-adapter'
|
7
|
-
gem 'jruby-openssl'
|
8
|
-
end
|
9
|
-
|
10
|
-
platforms :ruby do
|
11
|
-
gem 'sqlite3'
|
12
|
-
gem 'mysql2'
|
13
|
-
gem 'pg'
|
14
|
-
end
|
15
|
-
|
16
|
-
gem 'ffaker'
|
17
|
-
gem 'activerecord', '~> 3.2.0'
|
18
|
-
gem 'railties', '~> 3.2.0'
|
19
|
-
gem 'minitest', '3.2.0'
|
20
|
-
gem 'mocha'
|
21
|
-
gem 'rake'
|
22
|
-
gem 'globalize3'
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module FriendlyId
|
2
|
-
# These methods will be added to the model's {FriendlyId::Base#relation_class relation_class}.
|
3
|
-
module FinderMethods
|
4
|
-
|
5
|
-
protected
|
6
|
-
|
7
|
-
# FriendlyId overrides this method to make it possible to use friendly id's
|
8
|
-
# identically to numeric ids in finders.
|
9
|
-
#
|
10
|
-
# @example
|
11
|
-
# person = Person.find(123)
|
12
|
-
# person = Person.find("joe")
|
13
|
-
#
|
14
|
-
# @see FriendlyId::ObjectUtils
|
15
|
-
def find_one(id)
|
16
|
-
return super if id.unfriendly_id?
|
17
|
-
where(@klass.friendly_id_config.query_field => id).first or super
|
18
|
-
end
|
19
|
-
|
20
|
-
# FriendlyId overrides this method to make it possible to use friendly id's
|
21
|
-
# identically to numeric ids in finders.
|
22
|
-
#
|
23
|
-
# @example
|
24
|
-
# person = Person.exists?(123)
|
25
|
-
# person = Person.exists?("joe")
|
26
|
-
# person = Person.exists?({:name => 'joe'})
|
27
|
-
# person = Person.exists?(['name = ?', 'joe'])
|
28
|
-
#
|
29
|
-
# @see FriendlyId::ObjectUtils
|
30
|
-
def exists?(id = false)
|
31
|
-
return super if id.unfriendly_id?
|
32
|
-
super @klass.friendly_id_config.query_field => id
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,115 +0,0 @@
|
|
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
|
-
|
66
|
-
def self.included(model_class)
|
67
|
-
model_class.instance_eval do
|
68
|
-
friendly_id_config.use :slugged
|
69
|
-
relation_class.send :include, FinderMethods
|
70
|
-
include Model
|
71
|
-
# Check if slug field is enabled to be translated with Globalize
|
72
|
-
unless respond_to?('translated_attribute_names') || translated_attribute_names.exclude?(friendly_id_config.query_field.to_sym)
|
73
|
-
puts "\n[FriendlyId] You need to translate '#{friendly_id_config.query_field}' field with Globalize (add 'translates :#{friendly_id_config.query_field}' in your model '#{self.class.name}')\n\n"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
module Model
|
79
|
-
def set_friendly_id(text, locale)
|
80
|
-
I18n.with_locale(locale || I18n.locale) do
|
81
|
-
set_slug(normalize_friendly_id(text))
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
module FinderMethods
|
87
|
-
# FriendlyId overrides this method to make it possible to use friendly id's
|
88
|
-
# identically to numeric ids in finders.
|
89
|
-
#
|
90
|
-
# @example
|
91
|
-
# person = Person.find(123)
|
92
|
-
# person = Person.find("joe")
|
93
|
-
#
|
94
|
-
# @see FriendlyId::ObjectUtils
|
95
|
-
def find_one(id)
|
96
|
-
return super if id.unfriendly_id?
|
97
|
-
found = where(@klass.friendly_id_config.query_field => id).first
|
98
|
-
found = includes(:translations).
|
99
|
-
where(translation_class.arel_table[:locale].in([I18n.locale, I18n.default_locale])).
|
100
|
-
where(translation_class.arel_table[@klass.friendly_id_config.query_field].eq(id)).first if found.nil?
|
101
|
-
|
102
|
-
if found
|
103
|
-
# Reload the translations for the found records.
|
104
|
-
found.tap { |f| f.translations.reload }
|
105
|
-
else
|
106
|
-
# if locale is not translated fallback to default locale
|
107
|
-
super
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
protected :find_one
|
112
|
-
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
data/test/globalize_test.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "helper"
|
4
|
-
|
5
|
-
class TranslatedArticle < ActiveRecord::Base
|
6
|
-
translates :slug, :title
|
7
|
-
extend FriendlyId
|
8
|
-
friendly_id :title, :use => :globalize
|
9
|
-
end
|
10
|
-
|
11
|
-
class GlobalizeTest < MiniTest::Unit::TestCase
|
12
|
-
include FriendlyId::Test
|
13
|
-
|
14
|
-
def setup
|
15
|
-
I18n.locale = :en
|
16
|
-
end
|
17
|
-
|
18
|
-
test "should find slug in current locale if locale is set, otherwise in default locale" do
|
19
|
-
transaction do
|
20
|
-
I18n.default_locale = :en
|
21
|
-
article_en = I18n.with_locale(:en) { TranslatedArticle.create(:title => 'a title') }
|
22
|
-
article_de = I18n.with_locale(:de) { TranslatedArticle.create(:title => 'titel') }
|
23
|
-
|
24
|
-
I18n.with_locale(:de) {
|
25
|
-
assert_equal TranslatedArticle.find("titel"), article_de
|
26
|
-
assert_equal TranslatedArticle.find("a-title"), article_en
|
27
|
-
}
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
test "should set friendly id for locale" do
|
32
|
-
transaction do
|
33
|
-
article = TranslatedArticle.create!(:title => "War and Peace")
|
34
|
-
article.set_friendly_id("Guerra y paz", :es)
|
35
|
-
article.save!
|
36
|
-
article = TranslatedArticle.find('war-and-peace')
|
37
|
-
I18n.with_locale(:es) { assert_equal "guerra-y-paz", article.friendly_id }
|
38
|
-
I18n.with_locale(:en) { assert_equal "war-and-peace", article.friendly_id }
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# https://github.com/svenfuchs/globalize3/blob/master/test/globalize3/dynamic_finders_test.rb#L101
|
43
|
-
# see: https://github.com/svenfuchs/globalize3/issues/100
|
44
|
-
test "record returned by friendly_id should have all translations" do
|
45
|
-
transaction do
|
46
|
-
I18n.with_locale(:en) do
|
47
|
-
article = TranslatedArticle.create(:title => 'a title')
|
48
|
-
Globalize.with_locale(:ja) { article.update_attributes(:title => 'タイトル') }
|
49
|
-
article_by_friendly_id = TranslatedArticle.find("a-title")
|
50
|
-
article.translations.each do |translation|
|
51
|
-
assert_includes article_by_friendly_id.translations, translation
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|