Skip to content
Browse files

Fix setting slugs with the Globalize module

Model#slug= was completely ignored and non-functional. This replaces it
with working functionality that has the same interface used by SimpleI18n.
  • Loading branch information...
1 parent c31ffb4 commit 16abf370738f0921a19229960c556841ef9b1385 @norman committed
Showing with 34 additions and 11 deletions.
  1. +2 −0 Changelog.md
  2. +20 −10 lib/friendly_id/globalize.rb
  3. +1 −1 lib/friendly_id/simple_i18n.rb
  4. +11 −0 test/globalize_test.rb
View
2 Changelog.md
@@ -8,6 +8,8 @@ suggestions, ideas and improvements to FriendlyId.
## 4.0.8 (NOT RELEASED YET)
+* Added set_friendly_id method to Globalize module.
+
* autoload FriendlyId::Slug; previously this class was not accessible from
migrations unless required explicitly, which could cause some queries to
unexpectedly fail.
View
30 lib/friendly_id/globalize.rb
@@ -31,12 +31,15 @@ class Post < ActiveRecord::Base
I18n.locale = :en
Post.find("star-wars")
+Additionally, finds will fall back to the default locale:
+
+ I18n.locale = :it
+ Post.find("star-wars")
+
To find a slug by an explicit locale, perform the find inside a block
passed to I18n's +with_locale+ method:
- I18n.with_locale(:it) do
- Post.find("guerre-stellari")
- end
+ I18n.with_locale(:it) { Post.find("guerre-stellari") }
=== Creating Records
@@ -44,12 +47,17 @@ class Post < ActiveRecord::Base
=== Translating Slugs
-To translate an existing record's friendly_id, simply change the locale and
-assign a value to the +slug+ field:
+To translate an existing record's friendly_id, use
+{FriendlyId::Globalize::Model#set_friendly_id}. This will ensure that the slug
+you add is properly escaped, transliterated and sequenced:
- I18n.with_locale(:it) do
- post.slug = "guerre-stellari"
- end
+ post = Post.create :name => "Star Wars"
+ post.set_friendly_id("Guerre stellari", :it)
+
+If you don't pass in a locale argument, FriendlyId::Globalize will just use the
+current locale:
+
+ I18n.with_locale(:it) { post.set_friendly_id("Guerre stellari") }
=end
module Globalize
@@ -67,8 +75,10 @@ def self.included(model_class)
end
module Model
- def slug=(text)
- set_slug(normalize_friendly_id(text))
+ def set_friendly_id(text, locale)
@norman Owner
norman added a note

@parndt is this going to break Refinery? Model#slug= was actually completely ineffectual before, but it would be great if you could confirm this is ok before I push it into a release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ I18n.with_locale(locale || I18n.locale) do
+ set_slug(normalize_friendly_id(text))
+ end
end
end
View
2 lib/friendly_id/simple_i18n.rb
@@ -65,7 +65,7 @@ def self.up
current locale:
I18n.with_locale(:es) do
- post.set_friendly_id("la-guerra-de-las-galaxas")
+ post.set_friendly_id("La guerra de las galaxas")
end
=end
module SimpleI18n
View
11 test/globalize_test.rb
@@ -28,6 +28,17 @@ def setup
end
end
+ test "should set friendly id for locale" do
+ transaction do
+ article = TranslatedArticle.create!(:title => "War and Peace")
+ article.set_friendly_id("Guerra y paz", :es)
+ article.save!
+ article = TranslatedArticle.find('war-and-peace')
+ I18n.with_locale(:es) { assert_equal "guerra-y-paz", article.friendly_id }
+ I18n.with_locale(:en) { assert_equal "war-and-peace", article.friendly_id }
+ end
+ end
+
# https://github.com/svenfuchs/globalize3/blob/master/test/globalize3/dynamic_finders_test.rb#L101
# see: https://github.com/svenfuchs/globalize3/issues/100
test "record returned by friendly_id should have all translations" do

0 comments on commit 16abf37

Please sign in to comment.
Something went wrong with that request. Please try again.