friendly_id 4.0.4 → 4.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,12 @@ suggestions, ideas and improvements to FriendlyId.
6
6
  * Table of Contents
7
7
  {:toc}
8
8
 
9
+ ## 4.0.5 (2012-04-28)
10
+
11
+ * Favor `includes` over `joins` in globalize to avoid read-only results (Jakub Wojtysiak)
12
+ * Fix globalize compatibility with results from dynamic finders (Chris Salzberg)
13
+
14
+
9
15
  ## 4.0.4 (2012-03-26)
10
16
 
11
17
  * Fix globalize plugin to avoid issues with asset precompilation (Philip Arndt)
data/Guide.rdoc CHANGED
@@ -114,8 +114,9 @@ store them in a field in your model. By default, this field must be named
114
114
  +:slug+, though you may change this using the
115
115
  {FriendlyId::Slugged::Configuration#slug_column slug_column} configuration
116
116
  option. You should add an index to this column, and in most cases, make it
117
- unique. You may also wish to constrain it to NOT NULL, but this depends on your
118
- app's behavior and requirements.
117
+ unique. Do not make the column unique in case you wish to scope the slug
118
+ (more on this later). You may also wish to constrain it to NOT NULL, but this
119
+ depends on your app's behavior and requirements.
119
120
 
120
121
  === Example Setup
121
122
 
@@ -356,6 +357,9 @@ Without :scoped in this case, one of the restaurants would have the slug
356
357
  The value for the +:scope+ option can be the name of a +belongs_to+ relation, or
357
358
  a column.
358
359
 
360
+ Please do note that you must drop the uniqueness constraint on the slug's
361
+ column in the database when you're scoping the slug.
362
+
359
363
  === Finding Records by Friendly ID
360
364
 
361
365
  If you are using scopes your friendly ids may not be unique, so a simple find
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency "i18n"
25
25
  s.add_development_dependency "ffaker"
26
26
  s.add_development_dependency "simplecov"
27
+ s.add_development_dependency "globalize3"
27
28
 
28
29
  s.description = <<-EOM
29
30
  FriendlyId is the "Swiss Army bulldozer" of slugging and permalink plugins for
@@ -18,3 +18,4 @@ gem 'railties', '~> 3.0.0'
18
18
  gem 'minitest'
19
19
  gem 'mocha'
20
20
  gem 'rake'
21
+ gem 'globalize3'
@@ -19,3 +19,4 @@ gem 'ffaker'
19
19
  gem 'minitest'
20
20
  gem 'mocha'
21
21
  gem 'rake'
22
+ gem 'globalize3'
@@ -19,3 +19,4 @@ gem 'railties', '~> 3.2.0'
19
19
  gem 'minitest'
20
20
  gem 'mocha'
21
21
  gem 'rake'
22
+ gem 'globalize3'
@@ -45,7 +45,7 @@ with numeric ids:
45
45
  module FriendlyId
46
46
 
47
47
  # The current version.
48
- VERSION = "4.0.4"
48
+ VERSION = "4.0.5"
49
49
 
50
50
  @mutex = Mutex.new
51
51
 
@@ -83,15 +83,18 @@ assign a value to the +slug+ field:
83
83
  # @see FriendlyId::ObjectUtils
84
84
  def find_one(id)
85
85
  return super if id.unfriendly_id?
86
- where(@klass.friendly_id_config.query_field => id).first or
87
- joins(:translations).
88
- where(translation_class.arel_table[:locale].eq(I18n.locale)).
89
- where(translation_class.arel_table[@klass.friendly_id_config.query_field].eq(id)).first or
90
- # if locale is not translated fallback to default locale
91
- joins(:translations).
92
- where(translation_class.arel_table[:locale].eq(I18n.default_locale)).
93
- where(translation_class.arel_table[@klass.friendly_id_config.query_field].eq(id)).first or
94
- super
86
+ found = where(@klass.friendly_id_config.query_field => id).first
87
+ found = includes(:translations).
88
+ where(translation_class.arel_table[:locale].in([I18n.locale, I18n.default_locale])).
89
+ where(translation_class.arel_table[@klass.friendly_id_config.query_field].eq(id)).first if found.nil?
90
+
91
+ if found
92
+ # Reload the translations for the found records.
93
+ found.tap { |f| f.translations.reload }
94
+ else
95
+ # if locale is not translated fallback to default locale
96
+ super
97
+ end
95
98
  end
96
99
 
97
100
  protected :find_one
@@ -0,0 +1,42 @@
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
+ test "friendly_id should find slug in current locale if locale is set, otherwise in default locale" do
15
+ transaction do
16
+ I18n.default_locale = :en
17
+ article_en = I18n.with_locale(:en) { TranslatedArticle.create(:title => 'a title') }
18
+ article_de = I18n.with_locale(:de) { TranslatedArticle.create(:title => 'titel') }
19
+
20
+ I18n.with_locale(:de) {
21
+ assert_equal TranslatedArticle.find("titel"), article_de
22
+ assert_equal TranslatedArticle.find("a-title"), article_en
23
+ }
24
+ end
25
+ end
26
+
27
+ # https://github.com/svenfuchs/globalize3/blob/master/test/globalize3/dynamic_finders_test.rb#L101
28
+ # see: https://github.com/svenfuchs/globalize3/issues/100
29
+ test "record returned by friendly_id should have all translations" do
30
+ transaction do
31
+ I18n.with_locale(:en) do
32
+ article = TranslatedArticle.create(:title => 'a title')
33
+ Globalize.with_locale(:ja) { article.update_attributes(:title => 'タイトル') }
34
+ article_by_friendly_id = TranslatedArticle.find("a-title")
35
+ article.translations.each do |translation|
36
+ assert_includes article_by_friendly_id.translations, translation
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ end
@@ -1,4 +1,9 @@
1
1
  require "friendly_id/migration"
2
+ require "globalize3"
3
+
4
+ class TranslatedArticle < ActiveRecord::Base
5
+ translates :slug, :title
6
+ end
2
7
 
3
8
  module FriendlyId
4
9
  module Test
@@ -9,6 +14,7 @@ module FriendlyId
9
14
  tables.each do |name|
10
15
  drop_table name
11
16
  end
17
+ TranslatedArticle.drop_translation_table!
12
18
  end
13
19
 
14
20
  def up
@@ -45,13 +51,16 @@ module FriendlyId
45
51
  add_column :journalists, "slug_es", :string
46
52
  add_column :journalists, "slug_de", :string
47
53
 
54
+ # This will be used to test globalize translations
55
+ TranslatedArticle.create_translation_table! :slug => :string, :title => :string
56
+
48
57
  @done = true
49
58
  end
50
59
 
51
60
  private
52
61
 
53
62
  def slugged_tables
54
- ["journalists", "articles", "novelists", "novels", "manuals"]
63
+ ["journalists", "articles", "novelists", "novels", "manuals", "translated_articles"]
55
64
  end
56
65
 
57
66
  def simple_tables
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.4
4
+ version: 4.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-26 00:00:00.000000000 Z
12
+ date: 2012-04-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
16
- requirement: &70338516662860 !ruby/object:Gem::Requirement
16
+ requirement: &70139212924340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.2.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70338516662860
24
+ version_requirements: *70139212924340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activerecord
27
- requirement: &70338516662240 !ruby/object:Gem::Requirement
27
+ requirement: &70139212923860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.2.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70338516662240
35
+ version_requirements: *70139212923860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: minitest
38
- requirement: &70338516661800 !ruby/object:Gem::Requirement
38
+ requirement: &70139212923480 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70338516661800
46
+ version_requirements: *70139212923480
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: mocha
49
- requirement: &70338516694440 !ruby/object:Gem::Requirement
49
+ requirement: &70139212923020 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70338516694440
57
+ version_requirements: *70139212923020
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: maruku
60
- requirement: &70338516692760 !ruby/object:Gem::Requirement
60
+ requirement: &70139212971700 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70338516692760
68
+ version_requirements: *70139212971700
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yard
71
- requirement: &70338516724120 !ruby/object:Gem::Requirement
71
+ requirement: &70139212971280 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70338516724120
79
+ version_requirements: *70139212971280
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: i18n
82
- requirement: &70338516719100 !ruby/object:Gem::Requirement
82
+ requirement: &70139212970860 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70338516719100
90
+ version_requirements: *70139212970860
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: ffaker
93
- requirement: &70338516717320 !ruby/object:Gem::Requirement
93
+ requirement: &70139212970440 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70338516717320
101
+ version_requirements: *70139212970440
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &70338516730820 !ruby/object:Gem::Requirement
104
+ requirement: &70139212970020 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,7 +109,18 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70338516730820
112
+ version_requirements: *70139212970020
113
+ - !ruby/object:Gem::Dependency
114
+ name: globalize3
115
+ requirement: &70139212969600 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *70139212969600
113
124
  description: ! 'FriendlyId is the "Swiss Army bulldozer" of slugging and permalink
114
125
  plugins for
115
126
 
@@ -165,6 +176,7 @@ files:
165
176
  - test/core_test.rb
166
177
  - test/databases.yml
167
178
  - test/generator_test.rb
179
+ - test/globalize_test.rb
168
180
  - test/helper.rb
169
181
  - test/history_test.rb
170
182
  - test/i18n_test.rb