friendly_id 4.1.0.beta.1 → 5.0.0.alpha.1

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.
@@ -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
@@ -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