friendly_id-mobility 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b795a77df5b0754c68965a8edcaab982dbf16156
4
- data.tar.gz: 45f876b1fbd1390214ee2435406a8c018dadfc76
3
+ metadata.gz: 3beb3b78d2d0e0eccbc1405b0869f7b102c90e21
4
+ data.tar.gz: dbe2b5819a7451ab66b1108aa5a3822d5190b480
5
5
  SHA512:
6
- metadata.gz: f52c7b45cce67256652debbe82f843d465f2d6a80624645b811397dc037225fca021a82e607a18dc358bed13169d099a32ba1083846e7acd297ba26cdc59457f
7
- data.tar.gz: d3b91e56dfb308bd22d1f4a670a7c17ef5393f484aa38f4b31f5a5aa84b7685646f541a1e03bac252120b40ba8cf5da7d4762e616bf795e764598b6665fdc7ee
6
+ metadata.gz: 4b2b51f41c697f16c0288824bd589af0bd377cabfccf4dc366fbb859c37c8f06af68c086bb6c6971095f81cebd3b396d95587041033d96d1a8113afff7aecd65
7
+ data.tar.gz: 1d810d14699b41e9ab04797820b2531fb94f75fa33f28b19d1c4f3bf9e54d347ade8ff5f6baa34413593463b9993c23b8ec3565d760e21d6dddbc802bce61918
data/Gemfile CHANGED
@@ -2,3 +2,12 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in friendly_id-mobility.gemspec
4
4
  gemspec
5
+
6
+ group :development, :test do
7
+ gem 'rake'
8
+
9
+ gem 'sqlite3'
10
+
11
+ gem 'pry'
12
+ gem 'pry-byebug'
13
+ end
data/Gemfile.lock CHANGED
@@ -20,15 +20,26 @@ GEM
20
20
  minitest (~> 5.1)
21
21
  tzinfo (~> 1.1)
22
22
  arel (7.1.4)
23
+ byebug (9.0.6)
24
+ coderay (1.1.1)
23
25
  concurrent-ruby (1.0.5)
26
+ database_cleaner (1.5.3)
24
27
  diff-lcs (1.3)
25
28
  friendly_id (5.2.0)
26
29
  activerecord (>= 4.0.0)
27
30
  i18n (0.8.1)
31
+ method_source (0.8.2)
28
32
  minitest (5.10.1)
29
33
  mobility (0.1.12)
30
34
  i18n (>= 0.6.10)
31
35
  request_store (~> 1.0)
36
+ pry (0.10.4)
37
+ coderay (~> 1.1.0)
38
+ method_source (~> 0.8.1)
39
+ slop (~> 3.4)
40
+ pry-byebug (3.4.2)
41
+ byebug (~> 9.0)
42
+ pry (~> 0.10)
32
43
  rake (10.5.0)
33
44
  request_store (1.3.2)
34
45
  rspec (3.5.0)
@@ -44,6 +55,8 @@ GEM
44
55
  diff-lcs (>= 1.2.0, < 2.0)
45
56
  rspec-support (~> 3.5.0)
46
57
  rspec-support (3.5.0)
58
+ slop (3.6.0)
59
+ sqlite3 (1.3.13)
47
60
  thread_safe (0.3.6)
48
61
  tzinfo (1.2.3)
49
62
  thread_safe (~> 0.1)
@@ -53,9 +66,13 @@ PLATFORMS
53
66
 
54
67
  DEPENDENCIES
55
68
  bundler (~> 1.12)
69
+ database_cleaner (~> 1.5.3)
56
70
  friendly_id-mobility!
57
- rake (~> 10.0)
71
+ pry
72
+ pry-byebug
73
+ rake
58
74
  rspec (~> 3.0)
75
+ sqlite3
59
76
 
60
77
  BUNDLED WITH
61
78
  1.12.5
data/README.md CHANGED
@@ -1,75 +1,145 @@
1
- # FriendlyId Mobility
1
+ FriendlyId Mobility
2
+ ===================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/friendly_id-mobility.svg)][gem]
5
+ [![Build Status](https://travis-ci.org/shioyama/friendly_id-mobility.svg?branch=master)][travis]
6
+
7
+ [gem]: https://rubygems.org/gems/friendly_id-mobility
8
+ [travis]: https://travis-ci.org/shioyama/friendly_id-mobility
2
9
 
3
10
  [Mobility](https://github.com/shioyama/mobility) support for
4
11
  [FriendlyId](https://github.com/norman/friendly_id).
5
12
 
6
- ## Installation
13
+ Installation
14
+ ------------
7
15
 
8
16
  Add this line to your application's Gemfile:
9
17
 
10
18
  ```ruby
11
- gem 'friendly_id-mobility'
19
+ gem 'friendly_id-mobility', '~> 0.2.0'
12
20
  ```
13
21
 
14
22
  And then execute:
15
23
 
16
- $ bundle
24
+ ```
25
+ bundle
26
+ ```
17
27
 
18
28
  Or install it yourself as:
19
29
 
20
- $ gem install friendly_id-mobility
30
+ ```
31
+ gem install friendly_id-mobility
32
+ ```
33
+
34
+ Run the Mobility generator and migrate:
21
35
 
22
- ## Translating Slugs using Mobility
36
+ ```
37
+ rails generate mobility:install
38
+ rake db:migrate
39
+ ```
23
40
 
24
- To translate slugs using Mobility, simply translate the attribute you want to
25
- use to generate slugs and also translate the slug attribute itself, and then
26
- call `friendly_id` with `use: :mobility`:
41
+ Run the FriendlyId generator, without the migration to generate the slugs
42
+ table:
43
+
44
+ ```
45
+ rails generate friendly_id --skip-migration
46
+ ```
47
+
48
+ You're ready to go!
49
+
50
+ Usage
51
+ -----
52
+
53
+ There are two ways to translate FriendlyId slugs with Mobility: with an
54
+ untranslated base column (like the SimpleI18n module included with FriendlyId),
55
+ and with a translated base column.
56
+
57
+ ### Translating Slug
58
+
59
+ If you only want to translate the slug, include `Mobility` and translate the
60
+ slug with whichever backend you want (here we're assuming the default KeyValue
61
+ backend). Here, `name` is untranslated (so there is a column on the `posts`
62
+ table named `name`):
27
63
 
28
64
  ```ruby
29
- class Post < ActiveRecord::Base
65
+ class Journalist < ActiveRecord::Base
30
66
  include Mobility
31
- translates :title, :slug
67
+ translates :slug
32
68
 
33
69
  extend FriendlyId
34
70
  friendly_id :title, use: :mobility
35
71
  end
36
72
  ```
37
73
 
38
- You can now save `title` in any locale and FriendlyId will generate a
39
- slug, stored by Mobility in the current locale:
74
+ You can now save `name` and generate a slug, and update the slug in any locale
75
+ using `set_friendly_id`
40
76
 
41
77
  ```ruby
42
- post = Post.create(title: "My Foo Title")
43
- post.title
78
+ journalist = Journalist.create(name: "John Smith")
79
+ journalist.slug
80
+ #=> "john-smith"
81
+ journalist.set_friendly_id("Juan Fulano", :es)
82
+ journalist.save!
83
+ I18n.with_locale(:es) { journalist.friendly_id }
84
+ #=> "juan-fulano"
85
+ ```
86
+
87
+ So the slug is translated, but the base attribute (`name`) is not.
88
+
89
+ ### Translating Slug and Base Attribute
90
+
91
+ You can also translate both slug and base attribute:
92
+
93
+ ```ruby
94
+ class Article < ActiveRecord::Base
95
+ include Mobility
96
+ translates :slug, :title, dirty: true
97
+
98
+ extend FriendlyId
99
+ friendly_id :title, use: :mobility
100
+ end
101
+ ```
102
+
103
+ In this case, `title` is translated, and its value in the current locale will
104
+ be used to generate the slug in this locale:
105
+
106
+ ```ruby
107
+ article = Article.create(title: "My Foo Title")
108
+ article.title
44
109
  #=> "My Foo Title"
45
- post.slug
110
+ article.slug
46
111
  #=> "my-foo-title"
47
112
  Mobility.locale = :fr
48
- post.title = "Mon titre foo"
49
- post.save
50
- post.slug
113
+ article.title = "Mon titre foo"
114
+ article.save
115
+ article.slug
51
116
  #=> "mon-titre-foo"
52
117
  Mobility.locale = :en
53
- post.slug
118
+ article.slug
54
119
  #=> "my-foo-title"
55
120
  ```
56
121
 
57
- Finders work too:
122
+ Setting `dirty: true` on the translated base attribute is recommended in order
123
+ to ensure that changes in any locale trigger updates to the slug in that
124
+ locale.
125
+
126
+ ### Friendly Finders with Translated Attributes
127
+
128
+ The Mobility `i18n` scope is mixed into the `friendly` scope for models which
129
+ `use: mobility`, so you can find translated slugs just like you would an
130
+ untranslated one:
58
131
 
59
132
  ```ruby
60
133
  Mobility.locale = :en
61
- Post.friendly.find("my-foo-title")
62
- #=> #<Post id: 1 ...>
134
+ Article.friendly.find("my-foo-title")
135
+ #=> #<Article id: 1 ...>
63
136
  Mobility.locale = :fr
64
- Post.friendly.find("mon-titre-foo")
65
- #=> #<Post id: 1 ...>
137
+ Article.friendly.find("mon-titre-foo")
138
+ #=> #<Article id: 1 ...>
66
139
  ```
67
140
 
68
- ## Development
69
-
70
- (TODO)
71
-
72
- ## Contributing
141
+ Contributing
142
+ ------------
73
143
 
74
144
  Bug reports and pull requests are welcome on GitHub at
75
145
  https://github.com/shioyama/friendly_id-mobility. This project is intended to
@@ -77,8 +147,8 @@ be a safe, welcoming space for collaboration, and contributors are expected to
77
147
  adhere to the [Contributor Covenant](http://contributor-covenant.org) code of
78
148
  conduct.
79
149
 
80
-
81
- ## License
150
+ License
151
+ -------
82
152
 
83
153
  The gem is available as open source under the terms of the [MIT
84
154
  License](http://opensource.org/licenses/MIT).
@@ -26,6 +26,30 @@ module FriendlyId
26
26
  "Mobility (add 'translates :#{field}' in your model '#{model.name}')"
27
27
  end
28
28
  end
29
+ private :advise_against_untranslated_model
29
30
  end
31
+
32
+ def set_friendly_id(text, locale = nil)
33
+ ::Mobility.with_locale(locale || ::Mobility.locale) do
34
+ set_slug normalize_friendly_id(text)
35
+ end
36
+ end
37
+
38
+ def should_generate_new_friendly_id?
39
+ send(friendly_id_config.slug_column).nil?
40
+ end
41
+
42
+ include(Module.new do
43
+ def set_slug(normalized_slug = nil)
44
+ super
45
+ changed.each do |change|
46
+ if change =~ /\A#{friendly_id_config.base}_([a-z]{2}(_[a-z]{2})?)/
47
+ locale, suffix = $1.split('_'.freeze)
48
+ locale = "#{locale}-#{suffix.upcase}".freeze if suffix
49
+ ::Mobility.with_locale(locale) { super }
50
+ end
51
+ end
52
+ end
53
+ end)
30
54
  end
31
55
  end
@@ -1,5 +1,5 @@
1
1
  module FriendlyId
2
2
  module Mobility
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly_id-mobility
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Salzberg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-07 00:00:00.000000000 Z
11
+ date: 2017-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mobility
@@ -86,6 +86,20 @@ dependencies:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
88
  version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: database_cleaner
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 1.5.3
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 1.5.3
89
103
  description:
90
104
  email:
91
105
  - chris@dejimata.com