devise-track_locale 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d42d8baab177ab12d9307f6dc3735467f738483b
4
- data.tar.gz: 1ca3d476811911db58bfd5bfaed8d0ae5d4f4a06
3
+ metadata.gz: db7291a96c9fa19229b50d62d6abaa6a0dcb55f3
4
+ data.tar.gz: f2ee7e4b6df4bd653e75d8cbd1354288b96d9a65
5
5
  SHA512:
6
- metadata.gz: b955c1ae100810673295c06adfa33cf54facc711ec321cfe0b3fe33c4f8ec956fc027e966af8aa0c5f8cbc854c03ad063ae9ea53ed1c86b8a24fa5cb9f2b3a5a
7
- data.tar.gz: ba715ebf37ad1f554294b4a6729d9c2d84ab54ee7fbfe7a0dbb9fe7b4eae8d3ee574bb171b798c9792b501265aaba489ab10d3bc854e9e29a42baad49dd00349
6
+ metadata.gz: 6a1a1e03598f363b77869e4b716119adb1900c9a28d0545d6ef03b88f2159f9d126b402158e2e69096dfb9b4b6640cf09b40b43d7487d985dd9a6f0ce051e78d
7
+ data.tar.gz: 84725fced06151d722d66d113f8644686a2451dd5b3d71afe885288cad89e93f861bddb0133df6d514a27780bd89d1aeb7b8a2b021db2eee72f27ac3fd3cae4f
data/README.md CHANGED
@@ -17,6 +17,15 @@ gem "devise-track_locale"
17
17
  Usage
18
18
  -----
19
19
 
20
+ Add a `locale` attribute to your users:
21
+
22
+ ```sh
23
+ $ rails g migration AddLocaleToUsers locale
24
+ $ rake db:migrate
25
+ ```
26
+
27
+ ### Save the locale on every sign-in
28
+
20
29
  In your model, add `:track_locale` as a devise module:
21
30
 
22
31
  ```rb
@@ -25,11 +34,14 @@ class User < ActiveRecord::Base
25
34
  end
26
35
  ```
27
36
 
28
- Add a `locale` attribute to your users:
37
+ ### Save the locale only once
29
38
 
30
- ```sh
31
- $ rails g migration AddLocaleToUsers locale
32
- $ rake db:migrate
39
+ In your model, add `:track_default_locale` as a devise module:
40
+
41
+ ```rb
42
+ class User < ActiveRecord::Base
43
+ devise …, :track_default_locale
44
+ end
33
45
  ```
34
46
 
35
47
  Copyright
@@ -0,0 +1,9 @@
1
+ module Devise
2
+ module Models
3
+ module TrackDefaultLocale
4
+ def set_default_locale!
5
+ update_column(:locale, I18n.locale.to_s) if locale.blank?
6
+ end
7
+ end
8
+ end
9
+ end
@@ -2,8 +2,8 @@ module Devise
2
2
  module Models
3
3
  module TrackLocale
4
4
  def set_locale!
5
- if self.locale.to_s != I18n.locale.to_s
6
- self.update_column(:locale, I18n.locale.to_s)
5
+ if locale.to_s != I18n.locale.to_s
6
+ update_column(:locale, I18n.locale.to_s)
7
7
  end
8
8
  end
9
9
  end
@@ -1,7 +1,12 @@
1
1
  require 'devise'
2
2
 
3
3
  Devise.add_module :track_locale, model: true
4
+ Devise.add_module :track_default_locale, model: true
4
5
 
5
6
  Warden::Manager.after_set_user do |record, *|
6
7
  record.set_locale! if record.respond_to?(:set_locale!)
7
8
  end
9
+
10
+ Warden::Manager.after_set_user do |record, *|
11
+ record.set_default_locale! if record.respond_to?(:set_default_locale!)
12
+ end
@@ -1,5 +1,5 @@
1
1
  module Devise
2
2
  module TrackLocale
3
- VERSION = "0.2.0"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ describe Devise::Models::TrackDefaultLocale do
4
+ class MockModel
5
+ include Devise::Models::TrackDefaultLocale
6
+ end
7
+
8
+ describe "#set_default_locale!" do
9
+ let(:model) { MockModel.new }
10
+ before do
11
+ allow(model).to receive(:update_column)
12
+ allow(model).to receive(:locale) { locale }
13
+ end
14
+
15
+ context "with no locale" do
16
+ let(:locale) { nil }
17
+
18
+ it "saves the new locale on the model" do
19
+ I18n.with_locale(:fr) do
20
+ model.set_default_locale!
21
+ end
22
+
23
+ expect(model).to have_received(:update_column).with(:locale, "fr")
24
+ end
25
+ end
26
+
27
+ context "with a locale already set" do
28
+ let(:locale) { "en" }
29
+
30
+ it "does not change the model" do
31
+ I18n.with_locale(:fr) do
32
+ model.set_default_locale!
33
+ end
34
+
35
+ expect(model).not_to have_received(:update_column)
36
+ end
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise-track_locale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sunny Ripert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-07 00:00:00.000000000 Z
11
+ date: 2016-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devise
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: ''
42
56
  email:
43
57
  - sunny@sunfox.org
@@ -49,9 +63,11 @@ files:
49
63
  - README.md
50
64
  - Rakefile
51
65
  - lib/devise-track_locale.rb
66
+ - lib/devise/models/track_default_locale.rb
52
67
  - lib/devise/models/track_locale.rb
53
68
  - lib/devise/track_locale.rb
54
69
  - lib/devise/track_locale/version.rb
70
+ - spec/models/track_default_locale_spec.rb
55
71
  - spec/models/track_locale_spec.rb
56
72
  - spec/spec_helper.rb
57
73
  homepage: http://github.com/sunny/devise-track_locale
@@ -78,5 +94,6 @@ signing_key:
78
94
  specification_version: 4
79
95
  summary: Track a user's last locale with Devise.
80
96
  test_files:
97
+ - spec/models/track_default_locale_spec.rb
81
98
  - spec/models/track_locale_spec.rb
82
99
  - spec/spec_helper.rb