rack-locale_memorable 0.3.1 → 0.3.3

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
  SHA256:
3
- metadata.gz: fd5bd6d13a8a85a10053362a85a1d5c3530348fadadb5f871f9990e40e36d59b
4
- data.tar.gz: 6f9c459b2527f17fc153c43bd3d71b9ecfb5c6ae60402e142ecc0da8bde1afa2
3
+ metadata.gz: 51ae9ed5a060ebb4263d0faeded7a6bdd94d40abb9835c670ce500e278ae5ac1
4
+ data.tar.gz: 758bc0cf5ac3498be41d78b79382024752d8a7ebb5995c0097b16031dacf4d86
5
5
  SHA512:
6
- metadata.gz: 9ed6cc8acbfbe09b475fc80c90f155102f25916a28f279e7efadc28e2660a5fce8cd7896727c4b819906097009886112d10df9e7e807eefb1ffa10e798d2c1d2
7
- data.tar.gz: f8304fa8aa3ed7b6f0fa102935828f5eeec3d0cae20bbd0bc8e5d10bc8fba3f37edbdd7879bfdc7f416f39ba8f43c08f2f242fa088661edf52e0b1c2afd6f595
6
+ metadata.gz: a6f73bce4a3940de8115d561cb2a3785f143e48496ba0822a186257122a3b8e4a0e2de1bc84ef5466da5cb0862fdcf5e410520d0d949100f7cfecfefad9e1a15
7
+ data.tar.gz: e38a7bd143e65235b55af8b05897135d04ca897ba0fdac1892e66b992005f7e81517a3265924bbba210860451abd8c39e9d5c756f029a5299bd9c98831599027
data/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.3] - 2022-09-28
4
+
5
+ ### Refactored
6
+
7
+ - requre active_support/core_ext only https://github.com/nov/rack-locale_memorable/commit/bf56b49f064a30d9688d65d2dd2901763bf31cee
8
+ - stop using request class's instance variables following response class's design https://github.com/nov/rack-locale_memorable/commit/056807010fcb5c01d4d1745f06f8a9f87aad2ec6
9
+
10
+ ## [0.3.2] - 2022-09-28
11
+
12
+ ### Fixed
13
+
14
+ - handle cookie expiry correctly https://github.com/nov/rack-locale_memorable/commit/886fed66da5513a4c0b6408a9ac7685932b0a034
15
+
16
+ ## [0.3.1] - 2022-09-28
17
+
18
+ ### Fixed
19
+
20
+ - fixed cookie key handling when remembering https://github.com/nov/rack-locale_memorable/commit/ab8c87bff449e762daf00dea5cef44d001176141
21
+
3
22
  ## [0.3.0] - 2022-09-28
4
23
 
5
24
  - First stable release.
data/README.md CHANGED
@@ -17,10 +17,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
17
17
  ```ruby
18
18
  # in initializers/middlewares.rb etc.
19
19
  Rails.application.configure do |config|
20
- config.middleware.use(
21
- Rack::LocaleMemorable,
22
- secure_cookie: Rails.env.production?
23
- )
20
+ config.middleware.use Rack::LocaleMemorable
24
21
  end
25
22
  ```
26
23
 
@@ -28,10 +25,34 @@ NOTE: If you're using devise, set `Rack::LocaleMemorable` before `Warden::Manage
28
25
 
29
26
  ```ruby
30
27
  Rails.application.configure do |config|
31
- config.middleware.insert_before(
32
- Warden::Manager,
28
+ config.middleware.insert_before Warden::Manager, Rack::LocaleMemorable
29
+ end
30
+ ```
31
+
32
+ By default, this gem handles
33
+
34
+ * `params['locale']` as explicit locale
35
+ * `cookies['locale']` as remembered locale
36
+ * `headers['HTTP_ACCEPT_LANGUAGE']` as implicit locale
37
+
38
+ and when explicit locale is specified in the request, remember it in cookie.
39
+
40
+ You can customize those values
41
+ * params_key (`'locale'` by default)
42
+ * cookie_key (`'locale'` by default)
43
+ * secure_cookie (`true` by default)
44
+ * cookie_lifetime (`1.year` by default)
45
+
46
+ like below
47
+
48
+ ```ruby
49
+ Rails.application.configure do |config|
50
+ config.middleware.use(
33
51
  Rack::LocaleMemorable,
34
- secure_cookie: Rails.env.production?
52
+ params_key: 'ui_locale',
53
+ cookie_key: 'ui_locale',
54
+ secure_cookie: Rails.env.production?,
55
+ cookie_lifetime: 3.months
35
56
  )
36
57
  end
37
58
  ```
@@ -3,30 +3,24 @@
3
3
  module Rack
4
4
  class LocaleMemorable
5
5
  class Request < Rack::Request
6
- attr_reader :explicit_locale, :params_key, :cookie_key
6
+ attr_reader :explicit_locale
7
7
 
8
- def initialize(env, params_key:, cookie_key:)
9
- super env
10
- @params_key = params_key
11
- @cookie_key = cookie_key
12
- end
13
-
14
- def detect_locale
8
+ def detect_locale(params_key:, cookie_key:)
15
9
  (
16
- from_params ||
17
- from_cookies ||
10
+ from_params(params_key) ||
11
+ from_cookies(cookie_key) ||
18
12
  from_headers
19
13
  )
20
14
  end
21
15
 
22
16
  private
23
17
 
24
- def from_params
25
- @explicit_locale = primary_locale_from params[params_key]
18
+ def from_params(key)
19
+ @explicit_locale = primary_locale_from params[key]
26
20
  end
27
21
 
28
- def from_cookies
29
- primary_locale_from cookies[cookie_key]
22
+ def from_cookies(key)
23
+ primary_locale_from cookies[key]
30
24
  end
31
25
 
32
26
  def from_headers
@@ -3,10 +3,10 @@
3
3
  module Rack
4
4
  class LocaleMemorable
5
5
  class Response < Rack::Response
6
- def remember_locale(explicit_locale, secure_cookie:, cookie_expiry:, cookie_key:)
6
+ def remember_locale(explicit_locale, secure_cookie:, cookie_lifetime:, cookie_key:)
7
7
  set_cookie cookie_key, {
8
8
  value: explicit_locale,
9
- expires: cookie_expiry,
9
+ expires: cookie_lifetime.from_now,
10
10
  http_only: true,
11
11
  secure: secure_cookie
12
12
  }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rack
4
4
  class LocaleMemorable
5
- VERSION = '0.3.1'
5
+ VERSION = '0.3.3'
6
6
  end
7
7
  end
@@ -1,30 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support'
4
- require 'active_support/all'
4
+ require 'active_support/core_ext'
5
5
  require 'http/accept'
6
6
  require 'rack'
7
7
 
8
8
  module Rack
9
9
  class LocaleMemorable
10
- def initialize(app, params_key: 'locale', cookie_key: 'locale', secure_cookie: true, cookie_expiry: 1.year.from_now)
10
+ def initialize(app, params_key: 'locale', cookie_key: 'locale', secure_cookie: true, cookie_lifetime: 1.year)
11
11
  @app = app
12
12
  @params_key = params_key
13
13
  @cookie_key = cookie_key
14
14
  @secure_cookie = secure_cookie
15
- @cookie_expiry = cookie_expiry
15
+ @cookie_lifetime = cookie_lifetime
16
16
  end
17
17
 
18
18
  def call(env)
19
- request = Request.new(env, params_key: @params_key, cookie_key: @cookie_key)
20
- I18n.with_locale(request.detect_locale) do
19
+ request = Request.new env
20
+ I18n.with_locale(request.detect_locale params_key: @params_key, cookie_key: @cookie_key) do
21
21
  status, headers, body = @app.call(env)
22
22
  response = Response.new body, status, headers
23
23
  if request.explicit_locale.present?
24
24
  response.remember_locale(
25
25
  request.explicit_locale,
26
26
  secure_cookie: @secure_cookie,
27
- cookie_expiry: @cookie_expiry,
27
+ cookie_lifetime: @cookie_lifetime,
28
28
  cookie_key: @cookie_key
29
29
  )
30
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-locale_memorable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - nov
@@ -163,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
163
  - !ruby/object:Gem::Version
164
164
  version: '0'
165
165
  requirements: []
166
- rubygems_version: 3.1.6
166
+ rubygems_version: 3.3.7
167
167
  signing_key:
168
168
  specification_version: 4
169
169
  summary: Remember locale in rack layer