hobbit-contrib 0.1.0 → 0.2.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: b7f3320b3437d3168749e5581b999a443a48e001
4
- data.tar.gz: 4cc4a32c36aeb3068cfc17aca524be92ecbee78d
3
+ metadata.gz: 774186fdc100731a772b7134afc48cae8d824154
4
+ data.tar.gz: 96380c2994f125c6594170f75b52e16434e2cee3
5
5
  SHA512:
6
- metadata.gz: a3464d83f2d94c32c9a32d29afa4a5a7548539d9fa00876ba20b9301b43b306aa4b56f219b4854006fb9f50b96963634a586eedcff26825eeb49e67c9c2b9c39
7
- data.tar.gz: 0248e6223440165e6f2c0efe3ab91ea95c50c3af3c67496d4061edce43a866c7607a4b86da1008b45cffa9ab8ab76af3139da33ceeb60bf96b0136ba8d5223e1
6
+ metadata.gz: 8e969363d5fd33c76f57c147e498abf4efa1a284290420ff9f1b96136f2c8e9823793e9f7357dc30207337481e93fd17241944a03602f1eab926623aa34704ad
7
+ data.tar.gz: e9336cee1b8c292a8d6e82913b6a1f6c813105416c25b8a6b0353722ab915a9299002baf3a59e730f9a112a265472f4c5dae254fab30f23c0a2a4c124c25559b
data/README.md CHANGED
@@ -118,41 +118,6 @@ above).
118
118
  an url, it returns the given url. If you pass an arbitrary string, it returns
119
119
  `/stylesheets/#{url}.css`.
120
120
 
121
- ### Hobbit::EnhancedRender
122
-
123
- This module extends the functionality of `Hobbit::Render`. To use this extension
124
- just include the module:
125
-
126
- ```ruby
127
- require 'hobbit'
128
- require 'hobbit/contrib'
129
-
130
- class App < Hobbit::Base
131
- include Hobbit::EnhancedRender
132
-
133
- get '/' do
134
- # index will become views/index.erb
135
- # use a layout (named 'layout'. It will become views/layouts/layout.erb)
136
- render 'index', {}, layout: 'layout'
137
- end
138
- end
139
- ```
140
-
141
- #### Available methods
142
-
143
- * `template_engine`: Returns the default template engine being used for this
144
- application. By default is `erb`.
145
- * `layout_path`: Returns the layout path according to the given template. For
146
- instance, `layout_path('layout')` will become `views/layouts/layout.erb`
147
- * `view_path`: Returns the view path according to the given template. For
148
- instance, `view_path('index')` will become `views/index.erb`
149
- * `render`: It's the same as in `Hobbit::Render`, but you can render templates
150
- with a layout (See the example above).
151
- * `partial`: It's the same as `render`, but expands the template name to the
152
- views path, except that the template name will start with an underscore. For
153
- instance, if you call `partial 'partial'`, the path will become
154
- `views/_partial.erb`
155
-
156
121
  ### Hobbit::Environment
157
122
 
158
123
  This extension allows you to control the application environment by using the
@@ -265,20 +230,27 @@ class App < Hobbit::Base
265
230
  include Hobbit::Render
266
231
 
267
232
  get '/' do
268
- render 'views/index.erb'
269
- end
270
-
271
- get '/with-layout' do
272
- render 'views/layout.erb' do
273
- render 'views/index.erb'
274
- end
233
+ # will render views/index.erb using views/layouts/application.erb as layout
234
+ render 'index'
275
235
  end
276
236
  end
277
237
  ```
278
238
 
279
239
  #### Available methods
280
240
 
281
- * `render`: Renders the given template using tilt.
241
+ * `default_layout`: Returns the name of the default layout (Override to use
242
+ another layout).
243
+ * `find_template`: Returns the path to a template (Override for multiple views
244
+ paths lookup).
245
+ * `layouts_path`: Returns the layouts path (Override to change the layouts
246
+ directory).
247
+ * `partial`: Renders the given template using tilt (without a layout).
248
+ * `render`: Renders the given template using tilt. You can pass a `layout`
249
+ option to change the layout (or set to `false` to not use one).
250
+ * `template_engine`: Returns the template engine beign used (Override to use
251
+ other template engine).
252
+ * `views_path`: Returns the views path (Override to change the views
253
+ directory).
282
254
 
283
255
  ### Hobbit::Session
284
256
 
@@ -291,6 +263,7 @@ require 'hobbit/contrib'
291
263
 
292
264
  class App < Hobbit::Base
293
265
  include Hobbit::Session
266
+ use Rack::Session::Cookie, secret: SecureRandom.hex(64)
294
267
 
295
268
  post '/' do
296
269
  session[:name] = 'hobbit'
@@ -316,4 +289,4 @@ end
316
289
 
317
290
  ## License
318
291
 
319
- See the [LICENSE](https://github.com/patriciomacadden/hobbit-contrib/blob/master/LICENSE).
292
+ See the [LICENSE](https://github.com/patriciomacadden/hobbit-contrib/blob/master/LICENSE).
@@ -1,6 +1,5 @@
1
1
  require 'hobbit/asset_tag'
2
2
  require 'hobbit/contrib/version'
3
- require 'hobbit/enhanced_render'
4
3
  require 'hobbit/environment'
5
4
  require 'hobbit/error_handling'
6
5
  require 'hobbit/filter'
@@ -1,5 +1,5 @@
1
1
  module Hobbit
2
2
  module Contrib
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -2,16 +2,55 @@ require 'tilt'
2
2
 
3
3
  module Hobbit
4
4
  module Render
5
+ def default_layout
6
+ "#{layouts_path}/application.#{template_engine}"
7
+ end
8
+
9
+ def find_template(template)
10
+ "#{views_path}/#{template}.#{template_engine}"
11
+ end
12
+
13
+ def layouts_path
14
+ "#{views_path}/layouts"
15
+ end
16
+
17
+ def partial(template, locals = {}, options = {}, &block)
18
+ template = find_template "_#{template}"
19
+ _render template, locals, options, &block
20
+ end
21
+
5
22
  def render(template, locals = {}, options = {}, &block)
6
- cache.fetch(template) do
7
- Tilt.new(template, options)
8
- end.render(self, locals, &block)
23
+ template = find_template template
24
+ layout = default_layout
25
+ if options.include? :layout
26
+ layout = options[:layout]
27
+ options.delete :layout
28
+ end
29
+ if layout
30
+ _render(layout) { _render(template) }
31
+ else
32
+ _render template
33
+ end
34
+ end
35
+
36
+ def template_engine
37
+ 'erb'
38
+ end
39
+
40
+ def views_path
41
+ 'views'
9
42
  end
10
43
 
11
44
  private
12
45
 
46
+ def _render(template, locals = {}, options = {}, &block)
47
+ cache.fetch template do
48
+ Tilt.new template, options
49
+ end.render self, locals, &block
50
+ end
51
+
13
52
  def cache
14
53
  Thread.current[:cache] ||= Tilt::Cache.new
15
54
  end
16
55
  end
17
- end
56
+ end
@@ -0,0 +1,5 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head><title>Hello World!</title></head>
4
+ <body>Hello World!</body>
5
+ </html>
@@ -1,5 +1 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head><title>Hello World!</title></head>
4
- <body>Hello World!</body>
5
- </html>
1
+ <h1>Hello World!</h1>
@@ -1,7 +1,7 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
- <title>From layout.erb</title>
4
+ <title>From application.erb</title>
5
5
  </head>
6
6
  <body>
7
7
  <%= yield %>
@@ -8,26 +8,80 @@ describe Hobbit::Render do
8
8
  mock_app do
9
9
  include Hobbit::Render
10
10
 
11
+ def views_path
12
+ File.expand_path '../fixtures/render/views/', __FILE__
13
+ end
14
+
11
15
  def name
12
16
  'Hobbit'
13
17
  end
14
18
 
15
- get('/') { render File.expand_path('../fixtures/render/views/index.erb', __FILE__) }
16
- get('/using-context') { render File.expand_path('../fixtures/render/views/hello.erb', __FILE__) }
19
+ get('/') { render 'index' }
20
+ get('/partial') { partial 'partial' }
21
+ get('/using-context') { render 'hello' }
22
+ get('/without-layout') { render '_partial', {}, layout: false }
23
+ end
24
+ end
25
+
26
+ describe '#default_layout' do
27
+ it 'defaults to application.erb' do
28
+ app.to_app.default_layout.must_equal "#{app.to_app.layouts_path}/application.erb"
29
+ end
30
+ end
31
+
32
+ describe '#find_template' do
33
+ it 'must return a template path' do
34
+ app.to_app.find_template('index').must_equal "#{app.to_app.views_path}/index.#{app.to_app.template_engine}"
35
+ end
36
+ end
37
+
38
+ describe '#layouts_path' do
39
+ it 'must return the path to the layouts directory' do
40
+ app.to_app.layouts_path.must_equal "#{app.to_app.views_path}/layouts"
41
+ end
42
+ end
43
+
44
+ describe '#partial' do
45
+ it 'must render a template without a layout' do
46
+ get '/partial'
47
+ last_response.must_be :ok?
48
+ last_response.body.wont_match /From application.erb/
49
+ last_response.body.must_match /Hello World!/
17
50
  end
18
51
  end
19
52
 
20
53
  describe '#render' do
21
- it 'must render a template' do
54
+ it 'must render a template (using a layout)' do
22
55
  get '/'
23
56
  last_response.must_be :ok?
57
+ last_response.body.must_match /From application.erb/
58
+ last_response.body.must_match /Hello World!/
59
+ end
60
+
61
+ it 'must render a template (not using a layout)' do
62
+ get '/without-layout'
63
+ last_response.must_be :ok?
64
+ last_response.body.wont_match /From application.erb/
24
65
  last_response.body.must_match /Hello World!/
25
66
  end
26
67
 
27
- it 'must use the app as context' do
68
+ it 'must render a template using the app as context' do
28
69
  get '/using-context'
29
70
  last_response.must_be :ok?
71
+ last_response.body.must_match /From application.erb/
30
72
  last_response.body.must_match /Hello Hobbit!/
31
73
  end
32
74
  end
75
+
76
+ describe '#template_engine' do
77
+ it 'defaults to erb' do
78
+ app.to_app.template_engine.must_equal 'erb'
79
+ end
80
+ end
81
+
82
+ describe '#views_path' do
83
+ it 'must return the path to the views directory' do
84
+ app.to_app.views_path.must_equal File.expand_path('../fixtures/render/views', __FILE__)
85
+ end
86
+ end
33
87
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hobbit-contrib
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
  - Patricio Mac Adden
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-09 00:00:00.000000000 Z
11
+ date: 2013-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -153,23 +153,20 @@ files:
153
153
  - lib/hobbit/asset_tag.rb
154
154
  - lib/hobbit/contrib.rb
155
155
  - lib/hobbit/contrib/version.rb
156
- - lib/hobbit/enhanced_render.rb
157
156
  - lib/hobbit/environment.rb
158
157
  - lib/hobbit/error_handling.rb
159
158
  - lib/hobbit/filter.rb
160
159
  - lib/hobbit/render.rb
161
160
  - lib/hobbit/session.rb
162
161
  - spec/asset_tag_spec.rb
163
- - spec/enhanced_render_spec.rb
164
162
  - spec/environment_spec.rb
165
163
  - spec/error_handling_and_filter_spec.rb
166
164
  - spec/error_handling_spec.rb
167
165
  - spec/filter_spec.rb
168
- - spec/fixtures/enhanced_render/views/_partial.erb
169
- - spec/fixtures/enhanced_render/views/index.erb
170
- - spec/fixtures/enhanced_render/views/layouts/layout.erb
166
+ - spec/fixtures/render/views/_partial.erb
171
167
  - spec/fixtures/render/views/hello.erb
172
168
  - spec/fixtures/render/views/index.erb
169
+ - spec/fixtures/render/views/layouts/application.erb
173
170
  - spec/minitest_helper.rb
174
171
  - spec/render_spec.rb
175
172
  - spec/session_spec.rb
@@ -199,16 +196,14 @@ specification_version: 4
199
196
  summary: Contributed Hobbit extensions
200
197
  test_files:
201
198
  - spec/asset_tag_spec.rb
202
- - spec/enhanced_render_spec.rb
203
199
  - spec/environment_spec.rb
204
200
  - spec/error_handling_and_filter_spec.rb
205
201
  - spec/error_handling_spec.rb
206
202
  - spec/filter_spec.rb
207
- - spec/fixtures/enhanced_render/views/_partial.erb
208
- - spec/fixtures/enhanced_render/views/index.erb
209
- - spec/fixtures/enhanced_render/views/layouts/layout.erb
203
+ - spec/fixtures/render/views/_partial.erb
210
204
  - spec/fixtures/render/views/hello.erb
211
205
  - spec/fixtures/render/views/index.erb
206
+ - spec/fixtures/render/views/layouts/application.erb
212
207
  - spec/minitest_helper.rb
213
208
  - spec/render_spec.rb
214
209
  - spec/session_spec.rb
@@ -1,33 +0,0 @@
1
- require 'hobbit/render'
2
-
3
- module Hobbit
4
- module EnhancedRender
5
- include Hobbit::Render
6
-
7
- def layout_path(template)
8
- "views/layouts/#{template}.#{template_engine}"
9
- end
10
-
11
- def partial(template, locals = {}, options = {}, &block)
12
- render "_#{template}", locals, options, &block
13
- end
14
-
15
- def render(template, locals = {}, options = {}, &block)
16
- template = view_path(template)
17
- layout = options.delete(:layout)
18
- if layout.nil?
19
- super
20
- else
21
- super(layout_path layout) { super }
22
- end
23
- end
24
-
25
- def template_engine
26
- 'erb'
27
- end
28
-
29
- def view_path(template)
30
- "views/#{template}.#{template_engine}"
31
- end
32
- end
33
- end
@@ -1,82 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Hobbit::EnhancedRender do
4
- include Hobbit::Contrib::Mock
5
- include Rack::Test::Methods
6
-
7
- def app
8
- mock_app do
9
- include Hobbit::EnhancedRender
10
-
11
- # we do this because it the layout path is relative to file being run
12
- def layout_path(template)
13
- File.expand_path("../fixtures/enhanced_render/#{super}", __FILE__)
14
- end
15
-
16
- # we do this because it the view path is relative to file being run
17
- def view_path(template)
18
- File.expand_path("../fixtures/enhanced_render/#{super}", __FILE__)
19
- end
20
-
21
- get '/' do
22
- render 'index', {}, layout: 'layout'
23
- end
24
-
25
- get '/without-layout' do
26
- render 'index'
27
- end
28
-
29
- get '/partial' do
30
- partial 'partial'
31
- end
32
- end
33
- end
34
-
35
- describe '#layout_path' do
36
- it 'must return a path' do
37
- path = File.expand_path('../fixtures/enhanced_render/views/layouts/layout.erb', __FILE__)
38
- app.to_app.layout_path('layout').must_equal path
39
- end
40
- end
41
-
42
- describe '#partial' do
43
- it 'must render' do
44
- get '/partial'
45
- last_response.must_be :ok?
46
- last_response.body.wont_match /From layout.erb/
47
- last_response.body.wont_match /From index.erb/
48
- last_response.body.must_match /From _partial.erb/
49
- end
50
- end
51
-
52
- describe '#render' do
53
- it 'must render with a layout' do
54
- get '/'
55
- last_response.must_be :ok?
56
- last_response.body.must_match /From layout.erb/
57
- last_response.body.must_match /From index.erb/
58
- last_response.body.must_match /From _partial.erb/
59
- end
60
-
61
- it 'must render without a layout' do
62
- get '/without-layout'
63
- last_response.must_be :ok?
64
- last_response.body.wont_match /From layout.erb/
65
- last_response.body.must_match /From index.erb/
66
- last_response.body.must_match /From _partial.erb/
67
- end
68
- end
69
-
70
- describe '#template_engine' do
71
- it 'must be erb by default' do
72
- app.to_app.template_engine.must_equal 'erb'
73
- end
74
- end
75
-
76
- describe '#view_path' do
77
- it 'must return a path' do
78
- path = File.expand_path('../fixtures/enhanced_render/views/index.erb', __FILE__)
79
- app.to_app.view_path('index').must_equal path
80
- end
81
- end
82
- end
@@ -1 +0,0 @@
1
- <h2>From _partial.erb</h2>
@@ -1,2 +0,0 @@
1
- <h1>From index.erb</h1>
2
- <%= partial 'partial' %>