contexts 1.1.0 → 2.0.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 +4 -4
- data/lib/contexts.rb +14 -15
- data/lib/contexts/version.rb +1 -1
- data/lib/mapper.rb +3 -3
- data/spec/controllers/application_controller_spec.rb +14 -14
- data/spec/controllers/context_controller_spec.rb +13 -0
- data/spec/dummy/app/contexts/city_context.rb +1 -5
- data/spec/dummy/app/contexts/{language_context.rb → locale_context.rb} +2 -10
- data/spec/dummy/app/controllers/context_controller.rb +3 -0
- data/spec/dummy/routes.rb +4 -2
- data/spec/dummy_app.rb +2 -2
- metadata +8 -8
- data/spec/controllers/context_preload_controller_spec.rb +0 -12
- data/spec/dummy/app/controllers/context_preload_controller.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 137cc0152fdd49899c5443d6c52cf125d6aad5e5
|
4
|
+
data.tar.gz: afa3ddc36e78a1d62848dff1ec420a42689c0b2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e45639cb32b939431ebbcc1b421de03cd630fb6f3baa51096caf83b44ed28f19e61b65e1db0a3f0e9fdd4c5df5596c40da4e8d20350bc3cd49a934fdef1c3025
|
7
|
+
data.tar.gz: 7e6e0ee4d91d7c075a37527f2f4a32e9e5b6ea78a492ca2a828fadff382ed5be1f897de6b02efa9e5c0bab697730421279bcedfb8fa514a2b285f13f930943fc
|
data/lib/contexts.rb
CHANGED
@@ -3,11 +3,7 @@ require 'mapper'
|
|
3
3
|
|
4
4
|
module Contexts
|
5
5
|
def self.resolve(name)
|
6
|
-
|
7
|
-
name.new
|
8
|
-
else
|
9
|
-
"#{name}_context".classify.constantize.new
|
10
|
-
end
|
6
|
+
"#{name}_context".classify.constantize.new
|
11
7
|
end
|
12
8
|
|
13
9
|
def self.included(base)
|
@@ -29,28 +25,31 @@ module Contexts
|
|
29
25
|
names.each{ |name| contexts[name] ||= Contexts.resolve(name) }
|
30
26
|
end
|
31
27
|
end
|
28
|
+
|
29
|
+
alias_method :context, :preload_contexts
|
30
|
+
alias_method :contexts, :preload_contexts
|
32
31
|
end
|
33
32
|
|
34
33
|
def contexts
|
35
34
|
@contexts ||= (request.env['contexts'] || {})
|
36
35
|
end
|
37
36
|
|
38
|
-
def current_context(
|
39
|
-
if
|
40
|
-
(ctx = contexts[
|
37
|
+
def current_context(name = nil)
|
38
|
+
if name
|
39
|
+
(ctx = contexts[name]) && ctx.current
|
41
40
|
else
|
42
41
|
Hash[contexts.map{ |name, ctx| [ name, ctx.current ] }]
|
43
42
|
end
|
44
43
|
end
|
45
44
|
|
46
|
-
def locked_context(
|
47
|
-
|
45
|
+
def locked_context(name = nil)
|
46
|
+
locked = (session[:locked_context] ||= {})
|
48
47
|
|
49
|
-
|
48
|
+
name ? locked[name.to_s] : locked
|
50
49
|
end
|
51
50
|
|
52
|
-
def context_locked?(
|
53
|
-
locked_context(
|
51
|
+
def context_locked?(name = nil)
|
52
|
+
locked_context(name).present?
|
54
53
|
end
|
55
54
|
|
56
55
|
def lock_context(data)
|
@@ -62,13 +61,13 @@ module Contexts
|
|
62
61
|
protected
|
63
62
|
|
64
63
|
def apply_contexts
|
65
|
-
contexts.each{ |name, ctx| ctx.apply(self, locked_context(
|
64
|
+
contexts.each{ |name, ctx| ctx.apply(self, locked_context(name)) }
|
66
65
|
end
|
67
66
|
|
68
67
|
def default_url_options
|
69
68
|
contexts.inject({}) do |options, (name, ctx)|
|
70
69
|
option = ctx.respond_to?(:url_option) ? ctx.url_option : ctx.current
|
71
|
-
options[
|
70
|
+
options[name] = option
|
72
71
|
options
|
73
72
|
end
|
74
73
|
end
|
data/lib/contexts/version.rb
CHANGED
data/lib/mapper.rb
CHANGED
@@ -13,12 +13,12 @@ module ActionDispatch::Routing
|
|
13
13
|
def context(name, &blk)
|
14
14
|
ctx = Contexts.resolve(name)
|
15
15
|
|
16
|
-
path = ":#{
|
16
|
+
path = ":#{name}"
|
17
17
|
path = "(#{path})" unless ctx.respond_to?(:required?) and ctx.required?
|
18
18
|
|
19
|
-
constraints ->(req) { (req.env['contexts'] ||= {})[
|
19
|
+
constraints ->(req) { (req.env['contexts'] ||= {})[name] = ctx } do
|
20
20
|
if ctx.respond_to?(:constraints)
|
21
|
-
options = { constraints: {
|
21
|
+
options = { constraints: { name => ctx.constraints } }
|
22
22
|
end
|
23
23
|
|
24
24
|
scope(path, options, &blk)
|
@@ -3,15 +3,15 @@ require 'spec_helper'
|
|
3
3
|
describe ApplicationController, type: [:controller, :request] do
|
4
4
|
context 'when context has constraints' do
|
5
5
|
it "checks value via regexp" do
|
6
|
-
expect(get: '/en').to route_to('application#index',
|
7
|
-
expect(get: '/ru/
|
6
|
+
expect(get: '/en').to route_to('application#index', locale: 'en')
|
7
|
+
expect(get: '/ru/locale').to route_to('application#index', locale: 'ru')
|
8
8
|
expect(get: '/de').not_to be_routable
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
context 'when context has no constraints' do
|
13
13
|
it 'allows any value' do
|
14
|
-
expect(get: '/ru/moscow/city').to route_to('application#index',
|
14
|
+
expect(get: '/ru/moscow/city').to route_to('application#index', locale: 'ru', city: 'moscow')
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -23,14 +23,14 @@ describe ApplicationController, type: [:controller, :request] do
|
|
23
23
|
|
24
24
|
context 'context locking' do
|
25
25
|
it 'overrides current context' do
|
26
|
-
get '/ru/
|
27
|
-
expect(controller).not_to be_context_locked(:
|
26
|
+
get '/ru/locale'
|
27
|
+
expect(controller).not_to be_context_locked(:locale)
|
28
28
|
|
29
|
-
controller.lock_context(
|
29
|
+
controller.lock_context(locale: :en)
|
30
30
|
|
31
|
-
expect(controller).to be_context_locked(:
|
32
|
-
expect(controller.locked_context(:
|
33
|
-
expect(controller.locked_context).to eq('
|
31
|
+
expect(controller).to be_context_locked(:locale)
|
32
|
+
expect(controller.locked_context(:locale)).to eq(:en)
|
33
|
+
expect(controller.locked_context).to eq('locale' => :en)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
@@ -38,16 +38,16 @@ describe ApplicationController, type: [:controller, :request] do
|
|
38
38
|
it 'returns contexts hash' do
|
39
39
|
get '/en'
|
40
40
|
|
41
|
-
expect(controller.contexts[:
|
41
|
+
expect(controller.contexts[:locale]).to be_a(LocaleContext)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
describe '#current_context' do
|
46
46
|
it 'returns current context value' do
|
47
|
-
get '/ru/
|
47
|
+
get '/ru/locale'
|
48
48
|
|
49
|
-
expect(controller.current_context(:
|
50
|
-
expect(controller.current_context).to eq(
|
49
|
+
expect(controller.current_context(:locale)).to eq(:ru)
|
50
|
+
expect(controller.current_context).to eq(locale: :ru)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -62,7 +62,7 @@ describe ApplicationController, type: [:controller, :request] do
|
|
62
62
|
get '/ru/moscow/city'
|
63
63
|
get '/en'
|
64
64
|
|
65
|
-
expect(controller.
|
65
|
+
expect(controller.locale_page_path).to eq('/locale')
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ContextController, type: :controller do
|
4
|
+
it 'preloads contexts' do
|
5
|
+
get :index
|
6
|
+
|
7
|
+
expect(controller.current_context(:locale)).to eq(:en)
|
8
|
+
expect(controller.current_context(:city)).to eq(nil)
|
9
|
+
|
10
|
+
controller.contexts[:locale].apply(controller, :ru)
|
11
|
+
expect(controller.locale_page_path).to eq('/ru/locale')
|
12
|
+
end
|
13
|
+
end
|
@@ -1,8 +1,4 @@
|
|
1
1
|
class CityContext
|
2
|
-
def key
|
3
|
-
:city
|
4
|
-
end
|
5
|
-
|
6
2
|
def required?
|
7
3
|
true
|
8
4
|
end
|
@@ -12,6 +8,6 @@ class CityContext
|
|
12
8
|
end
|
13
9
|
|
14
10
|
def apply(controller, value)
|
15
|
-
@city = value || controller.params[
|
11
|
+
@city = value || controller.params[:city].presence
|
16
12
|
end
|
17
13
|
end
|
@@ -1,12 +1,4 @@
|
|
1
|
-
class
|
2
|
-
def key
|
3
|
-
:lang
|
4
|
-
end
|
5
|
-
|
6
|
-
def required?
|
7
|
-
false
|
8
|
-
end
|
9
|
-
|
1
|
+
class LocaleContext
|
10
2
|
def constraints
|
11
3
|
Regexp.new(I18n.available_locales.join('|'))
|
12
4
|
end
|
@@ -20,7 +12,7 @@ class LanguageContext
|
|
20
12
|
end
|
21
13
|
|
22
14
|
def apply(controller, value)
|
23
|
-
I18n.locale = value || controller.params[
|
15
|
+
I18n.locale = value || controller.params[:locale].presence || default
|
24
16
|
end
|
25
17
|
|
26
18
|
def url_option
|
data/spec/dummy/routes.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
|
-
contexts :
|
2
|
+
contexts :locale do
|
3
3
|
get '/', to: 'application#index', as: :root
|
4
|
-
get '/
|
4
|
+
get '/locale', to: 'application#index', as: :locale_page
|
5
5
|
|
6
6
|
context :city do
|
7
7
|
get '/city/', to: 'application#index', as: :city_page
|
8
8
|
end
|
9
9
|
end
|
10
|
+
|
11
|
+
get '/context', to: 'context#index'
|
10
12
|
end
|
data/spec/dummy_app.rb
CHANGED
@@ -18,8 +18,8 @@ end
|
|
18
18
|
|
19
19
|
Rails.application.initialize!
|
20
20
|
|
21
|
-
require 'dummy/app/contexts/
|
21
|
+
require 'dummy/app/contexts/locale_context'
|
22
22
|
require 'dummy/app/contexts/city_context'
|
23
23
|
require 'dummy/app/controllers/application_controller'
|
24
|
-
require 'dummy/app/controllers/
|
24
|
+
require 'dummy/app/controllers/context_controller'
|
25
25
|
require 'dummy/routes'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contexts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kolesnikov Danil
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -113,11 +113,11 @@ files:
|
|
113
113
|
- lib/contexts/version.rb
|
114
114
|
- lib/mapper.rb
|
115
115
|
- spec/controllers/application_controller_spec.rb
|
116
|
-
- spec/controllers/
|
116
|
+
- spec/controllers/context_controller_spec.rb
|
117
117
|
- spec/dummy/app/contexts/city_context.rb
|
118
|
-
- spec/dummy/app/contexts/
|
118
|
+
- spec/dummy/app/contexts/locale_context.rb
|
119
119
|
- spec/dummy/app/controllers/application_controller.rb
|
120
|
-
- spec/dummy/app/controllers/
|
120
|
+
- spec/dummy/app/controllers/context_controller.rb
|
121
121
|
- spec/dummy/routes.rb
|
122
122
|
- spec/dummy_app.rb
|
123
123
|
- spec/spec_helper.rb
|
@@ -147,11 +147,11 @@ specification_version: 4
|
|
147
147
|
summary: Simple way to reuse routes logic in rails apps
|
148
148
|
test_files:
|
149
149
|
- spec/controllers/application_controller_spec.rb
|
150
|
-
- spec/controllers/
|
150
|
+
- spec/controllers/context_controller_spec.rb
|
151
151
|
- spec/dummy/app/contexts/city_context.rb
|
152
|
-
- spec/dummy/app/contexts/
|
152
|
+
- spec/dummy/app/contexts/locale_context.rb
|
153
153
|
- spec/dummy/app/controllers/application_controller.rb
|
154
|
-
- spec/dummy/app/controllers/
|
154
|
+
- spec/dummy/app/controllers/context_controller.rb
|
155
155
|
- spec/dummy/routes.rb
|
156
156
|
- spec/dummy_app.rb
|
157
157
|
- spec/spec_helper.rb
|
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ContextPreloadController, type: [:controller, :request] do
|
4
|
-
describe '.preload_contexts' do
|
5
|
-
it 'populates contexts' do
|
6
|
-
get '/'
|
7
|
-
|
8
|
-
expect(controller.current_context(:lang)).to eq(:en)
|
9
|
-
expect(controller.current_context(:city)).to eq(nil)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|