contexts 0.1.1 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 152d6f29d9baeeed0af51ba46574a3f96d37ec7d
4
- data.tar.gz: 2138c23ac0adb9f181f82dd7db13499f8ab3c21e
3
+ metadata.gz: b6b7149a89f0e5a6552c1087d1f58dd8bfa9967c
4
+ data.tar.gz: f61791aa859430bd5ab8e16f84f5838eef32635c
5
5
  SHA512:
6
- metadata.gz: 6be9641c3cf6731b5f564c83c87f47ec74e9bdd327369a657f3f91eb474b09a70e77691e2c10a0c3773ad7a51f5a8434d381b706b76eff8b65db5a7375a7487f
7
- data.tar.gz: d882b04c68bd96ce2a029bea9ddfbca66a4656e0a028dae2a669406e508ac161805bca7a311c92e14b123cae9ff6761518f53fa95ea83b9c5d517d18275234ce
6
+ metadata.gz: 18affc6a95b04672cee4d80ee67b4f0ee7062d3abe0e362c90e90aa92ff2e245e4139c8bf52ea8cd3ee4df8cfa878060bc8e28c51b8fc1e07f892046c3e2db7e
7
+ data.tar.gz: a3443e921fba6200f3f68c09f80e0e1eb9d222f0f633c475eb0817eba6582dce34a55fc8a6692ddc4b183d4e0e52f0d343d44be1e40a45fb486f2cf9d23a3a2d
data/README.md CHANGED
@@ -3,11 +3,24 @@
3
3
  [![Code Climate](https://codeclimate.com/github/kolesnikovde/contexts/badges/gpa.svg)](https://codeclimate.com/github/kolesnikovde/contexts)
4
4
  [![Test Coverage](https://codeclimate.com/github/kolesnikovde/contexts/badges/coverage.svg)](https://codeclimate.com/github/kolesnikovde/contexts)
5
5
 
6
-
7
6
  # Contexts
8
7
 
9
8
  This gem provides simple way to reuse routes logic in rails apps.
10
9
 
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'contexts'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ ## Usage
21
+
22
+ Check out specs and [dummy app](https://github.com/kolesnikovde/contexts/tree/master/spec/dummy).
23
+
11
24
  ## License
12
25
 
13
26
  Copyright (c) 2014 Kolesnikov Danil
data/lib/contexts.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'contexts/version'
2
- require 'contexts/base'
3
2
  require 'mapper'
4
3
 
5
4
  module Contexts
@@ -49,8 +48,10 @@ module Contexts
49
48
  end
50
49
 
51
50
  def default_url_options
52
- options = super
53
- contexts.each{ |name, ctx| options[ctx.key] = ctx.url_option }
54
- options
51
+ contexts.inject({}) do |options, (name, ctx)|
52
+ option = ctx.respond_to?(:url_option) ? ctx.url_option : ctx.current
53
+ options[ctx.key] = option
54
+ options
55
+ end
55
56
  end
56
57
  end
@@ -1,3 +1,3 @@
1
1
  module Contexts
2
- VERSION = '0.1.1'
2
+ VERSION = '1.0.0'
3
3
  end
data/lib/mapper.rb CHANGED
@@ -14,14 +14,15 @@ module ActionDispatch::Routing
14
14
  name = "#{name}_context".classify.constantize unless name.is_a?(Class)
15
15
  ctx = name.new
16
16
  path = ":#{ctx.key}"
17
- path = "(#{path})" unless ctx.required?
17
+ path = "(#{path})" unless ctx.respond_to?(:required?) and ctx.required?
18
18
 
19
- constraints = lambda do |req|
20
- (req.env['contexts'] ||= {})[ctx.key] = ctx
21
- ctx.matches_constraints?(req)
22
- end
19
+ constraints ->(req) { (req.env['contexts'] ||= {})[ctx.key] = ctx } do
20
+ if ctx.respond_to?(:constraints)
21
+ options = { constraints: { ctx.key => ctx.constraints } }
22
+ end
23
23
 
24
- scope(path, constraints: constraints, &blk)
24
+ scope(path, options, &blk)
25
+ end
25
26
  end
26
27
  end
27
28
  end
@@ -1,60 +1,36 @@
1
1
  require 'spec_helper'
2
- require 'dummy/app/controllers/application_controller'
3
- require 'dummy/app/contexts/language_context'
4
-
5
- shared_examples 'context controller' do
6
- before(:all) do
7
- Rails.application.routes.draw do
8
- contexts :language do
9
- get '/', to: 'application#index', as: :root
10
- get '/hello', to: 'application#index', as: :hello_page
11
- end
12
- end
13
- end
14
-
15
- after(:all) do
16
- Rails.application.reload_routes!
17
- end
18
2
 
3
+ describe ApplicationController, type: [:controller, :request] do
19
4
  context 'when context has constraints' do
20
- class RequestBasedConstraint
21
- def self.matches?(req)
22
- req.params[:lang] =~ /ru|en/
23
- end
24
- end
25
-
26
- constraints = {
27
- regexp: /ru|en/,
28
- proc: ->(req) { req.params[:lang] =~ /ru|en/ },
29
- request_based: RequestBasedConstraint
30
- }
31
-
32
- constraints.each do |name, con|
33
- it "checks value via #{name}" do
34
- allow_any_instance_of(LanguageContext).to receive(:constraints).and_return(con)
35
-
36
- expect(get: '/en').to route_to('application#index', lang: 'en')
37
- expect(get: '/ru/hello').to route_to('application#index', lang: 'ru')
38
- expect(get: '/de').not_to be_routable
39
- end
5
+ it "checks value via regexp" do
6
+ expect(get: '/en').to route_to('application#index', lang: 'en')
7
+ expect(get: '/ru/lang').to route_to('application#index', lang: 'ru')
8
+ expect(get: '/de').not_to be_routable
40
9
  end
41
10
  end
42
11
 
43
12
  context 'when context has no constraints' do
44
13
  it 'allows any value' do
45
- allow_any_instance_of(LanguageContext).to receive(:constraints).and_return(nil)
46
-
47
- expect(get: '/').to route_to('application#index')
48
- expect(get: '/de/').to route_to('application#index', lang: 'de')
14
+ expect(get: '/ru/moscow/city').to route_to('application#index', lang: 'ru', city: 'moscow')
49
15
  end
50
16
  end
51
17
 
52
18
  context 'when context is required' do
53
19
  it 'requires value' do
54
- allow_any_instance_of(LanguageContext).to receive(:required?).and_return(true)
20
+ expect(get: '/moscow/city').to route_to('application#index', city: 'moscow')
21
+ end
22
+ end
23
+
24
+ context 'context locking' do
25
+ it 'overrides current context' do
26
+ get '/ru/lang'
27
+ expect(controller).not_to be_context_locked(:lang)
28
+
29
+ controller.lock_context(lang: :en)
55
30
 
56
- expect(get: '/').not_to be_routable
57
- expect(get: '/en/').to route_to('application#index', lang: 'en')
31
+ expect(controller).to be_context_locked(:lang)
32
+ expect(controller.locked_context(:lang)).to eq(:en)
33
+ expect(controller.locked_context).to eq('lang' => :en)
58
34
  end
59
35
  end
60
36
 
@@ -68,7 +44,7 @@ shared_examples 'context controller' do
68
44
 
69
45
  describe '#current_context' do
70
46
  it 'returns current context value' do
71
- get '/ru/hello'
47
+ get '/ru/lang'
72
48
 
73
49
  expect(controller.current_context(:lang)).to eq(:ru)
74
50
  expect(controller.current_context).to eq(lang: :ru)
@@ -77,43 +53,16 @@ shared_examples 'context controller' do
77
53
 
78
54
  context 'url helpers' do
79
55
  it 'includes context' do
80
- get '/ru'
81
-
82
- expect(controller.hello_page_path).to eq('/ru/hello')
83
- end
56
+ get '/ru/moscow/city'
84
57
 
85
- it 'uses default value' do
86
- get '/en'
87
-
88
- expect(controller.hello_page_path).to eq('/en/hello')
58
+ expect(controller.city_page_path).to eq('/ru/moscow/city')
89
59
  end
90
60
 
91
61
  it 'allows to customize url option' do
62
+ get '/ru/moscow/city'
92
63
  get '/en'
93
64
 
94
- ctx = controller.contexts[:lang]
95
- allow(ctx).to receive(:url_option) do
96
- ctx.current if ctx.current != ctx.default
97
- end
98
-
99
- expect(controller.hello_page_path).to eq('/hello')
65
+ expect(controller.lang_page_path).to eq('/lang')
100
66
  end
101
67
  end
102
-
103
- context 'context locking' do
104
- it 'overrides current context' do
105
- get '/ru/hello'
106
- expect(controller).not_to be_context_locked(:lang)
107
-
108
- controller.lock_context(lang: :en)
109
-
110
- expect(controller).to be_context_locked(:lang)
111
- expect(controller.locked_context(:lang)).to eq(:en)
112
- expect(controller.locked_context).to eq('lang' => :en)
113
- end
114
- end
115
- end
116
-
117
- describe ApplicationController, type: [:controller, :request] do
118
- it_behaves_like 'context controller'
119
68
  end
@@ -0,0 +1,17 @@
1
+ class CityContext
2
+ def key
3
+ :city
4
+ end
5
+
6
+ def required?
7
+ true
8
+ end
9
+
10
+ def current
11
+ @city
12
+ end
13
+
14
+ def apply(controller, value)
15
+ @city = value || controller.params[key].presence
16
+ end
17
+ end
@@ -1,4 +1,4 @@
1
- class LanguageContext < Contexts::Base
1
+ class LanguageContext
2
2
  def key
3
3
  :lang
4
4
  end
@@ -19,7 +19,11 @@ class LanguageContext < Contexts::Base
19
19
  I18n.locale
20
20
  end
21
21
 
22
- def apply(ctrl, value)
23
- I18n.locale = value || ctrl.params[key].presence || default
22
+ def apply(controller, value)
23
+ I18n.locale = value || controller.params[key].presence || default
24
+ end
25
+
26
+ def url_option
27
+ current unless current == default
24
28
  end
25
29
  end
@@ -1,8 +1,6 @@
1
1
  class ApplicationController < ActionController::Base
2
2
  include Contexts
3
3
 
4
- protect_from_forgery with: :exception
5
-
6
4
  def index
7
5
  render nothing: true
8
6
  end
@@ -0,0 +1,10 @@
1
+ Rails.application.routes.draw do
2
+ contexts :language do
3
+ get '/', to: 'application#index', as: :root
4
+ get '/lang', to: 'application#index', as: :lang_page
5
+
6
+ context :city do
7
+ get '/city/', to: 'application#index', as: :city_page
8
+ end
9
+ end
10
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,11 @@ ENV['RAILS_ENV'] = 'test'
6
6
  require File.expand_path('../dummy/config/environment.rb', __FILE__)
7
7
  require 'rspec/rails'
8
8
 
9
+ require 'dummy/app/controllers/application_controller'
10
+ require 'dummy/app/contexts/language_context'
11
+ require 'dummy/app/contexts/city_context'
12
+ require 'dummy/config/routes'
13
+
9
14
  RSpec.configure do |config|
10
15
  config.order = 'random'
11
16
  end
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: 0.1.1
4
+ version: 1.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-10-21 00:00:00.000000000 Z
11
+ date: 2014-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -110,16 +110,16 @@ files:
110
110
  - Rakefile
111
111
  - contexts.gemspec
112
112
  - lib/contexts.rb
113
- - lib/contexts/base.rb
114
113
  - lib/contexts/version.rb
115
114
  - lib/mapper.rb
116
- - spec/contexts_spec.rb
117
115
  - spec/controllers/application_controller_spec.rb
116
+ - spec/dummy/app/contexts/city_context.rb
118
117
  - spec/dummy/app/contexts/language_context.rb
119
118
  - spec/dummy/app/controllers/application_controller.rb
120
119
  - spec/dummy/config/application.rb
121
120
  - spec/dummy/config/boot.rb
122
121
  - spec/dummy/config/environment.rb
122
+ - spec/dummy/config/routes.rb
123
123
  - spec/spec_helper.rb
124
124
  homepage: https://github.com/kolesnikovde/contexts
125
125
  licenses:
@@ -146,11 +146,12 @@ signing_key:
146
146
  specification_version: 4
147
147
  summary: Simple way to reuse routes logic in rails apps
148
148
  test_files:
149
- - spec/contexts_spec.rb
150
149
  - spec/controllers/application_controller_spec.rb
150
+ - spec/dummy/app/contexts/city_context.rb
151
151
  - spec/dummy/app/contexts/language_context.rb
152
152
  - spec/dummy/app/controllers/application_controller.rb
153
153
  - spec/dummy/config/application.rb
154
154
  - spec/dummy/config/boot.rb
155
155
  - spec/dummy/config/environment.rb
156
+ - spec/dummy/config/routes.rb
156
157
  - spec/spec_helper.rb
data/lib/contexts/base.rb DELETED
@@ -1,38 +0,0 @@
1
- module Contexts
2
- class Base
3
- def key
4
- end
5
-
6
- def required?
7
- true
8
- end
9
-
10
- def current
11
- end
12
-
13
- def default
14
- end
15
-
16
- def constraints
17
- end
18
-
19
- def url_option
20
- current
21
- end
22
-
23
- def apply(controller, value)
24
- end
25
-
26
- def matches_constraints?(request)
27
- if (con = constraints).nil?
28
- true
29
- elsif con.respond_to?(:matches?)
30
- con.matches?(request)
31
- elsif con.is_a?(Proc)
32
- con.call(request)
33
- else
34
- con === request.params[key]
35
- end
36
- end
37
- end
38
- end
@@ -1,4 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Contexts do
4
- end