rango 0.2.5.1 → 0.2.6
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.
- data/lib/rango/mixins/conventional_rendering.rb +1 -1
- data/lib/rango/orm/tasks/datamapper.nake +6 -3
- data/lib/rango/router/adapters/http_router.rb +48 -0
- data/lib/rango/tasks/spec.nake +1 -2
- data/lib/rango/template.rb +10 -0
- data/lib/rango/version.rb +1 -1
- data/spec/rango/mixins/render_spec.rb +2 -2
- data/stubs/stack/content/Gemfile.rbt +1 -2
- data/stubs/stack/content/lib/%name%/views.rb.rbt +1 -1
- data/stubs/stack/content/spec/spec_helper.rb +4 -0
- metadata +11 -50
- data/lib/rango/templates/exts/haml.rb +0 -33
- data/lib/rango/templates/exts/tilt.rb +0 -29
- data/lib/rango/templates/helpers.rb +0 -147
- data/lib/rango/templates/template.rb +0 -87
- data/spec/rango/templates/exts/haml_spec.rb +0 -0
- data/spec/rango/templates/exts/tilt_spec.rb +0 -0
- data/spec/rango/templates/helpers_spec.rb +0 -139
- data/spec/rango/templates/template_spec.rb +0 -61
- data/spec/stubs/templates/block/block.html.haml +0 -2
- data/spec/stubs/templates/block/blocks.html.haml +0 -5
- data/spec/stubs/templates/block/error.html.haml +0 -2
- data/spec/stubs/templates/block/getter.html.haml +0 -3
- data/spec/stubs/templates/block/value.html.haml +0 -1
- data/spec/stubs/templates/context_id.html.haml +0 -1
- data/spec/stubs/templates/enhance_block/basic.html.haml +0 -5
- data/spec/stubs/templates/enhance_block/error.html.haml +0 -3
- data/spec/stubs/templates/enhance_block/name_error.html.haml +0 -1
- data/spec/stubs/templates/enhance_block/nil.html.haml +0 -3
- data/spec/stubs/templates/enhance_block/standalone.html.haml +0 -2
- data/spec/stubs/templates/extend_block/basic.html.haml +0 -5
- data/spec/stubs/templates/extend_block/error.html.haml +0 -5
- data/spec/stubs/templates/extend_block/error2.html.haml +0 -1
- data/spec/stubs/templates/extend_block/name_error.html.haml +0 -1
- data/spec/stubs/templates/extend_block/nil.html.haml +0 -4
- data/spec/stubs/templates/includes/base.html.haml +0 -1
- data/spec/stubs/templates/includes/basic.html.haml +0 -1
- data/spec/stubs/templates/includes/includes.html.haml +0 -7
- data/spec/stubs/templates/includes/integration.html.haml +0 -4
- data/spec/stubs/templates/includes/integration2.html.haml +0 -4
- data/spec/stubs/templates/index.html.haml +0 -1
- data/spec/stubs/templates/inheritance/basic/base.html.haml +0 -2
- data/spec/stubs/templates/inheritance/basic/index.html.haml +0 -2
- data/spec/stubs/templates/inheritance/capture/haml/base.html.haml +0 -2
- data/spec/stubs/templates/inheritance/capture/haml/index.html.haml +0 -3
- data/spec/stubs/templates/library.html.haml +0 -3
- data/spec/stubs/templates/test.html.haml +0 -1
- data/spec/stubs/templates/variables.html.haml +0 -1
@@ -1,5 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
# lazy-loading for datamapper
|
4
|
+
autoload "DataMapper", "dm-core/migrations"
|
5
|
+
|
3
6
|
# @since 0.0.2.1
|
4
7
|
# ./tasks.rb db:automigrate test
|
5
8
|
Task.new("db:automigrate") do |task|
|
@@ -7,7 +10,7 @@ Task.new("db:automigrate") do |task|
|
|
7
10
|
task.define do |environment = "development", options|
|
8
11
|
Task[:environment].call(environment)
|
9
12
|
Rango.logger.info("[#{Rango.environment}] Migrating databases ...")
|
10
|
-
result = DataMapper.auto_migrate!
|
13
|
+
result = ::DataMapper.auto_migrate!
|
11
14
|
Rango.logger.debug("Result: #{result.inspect}")
|
12
15
|
end
|
13
16
|
end
|
@@ -19,7 +22,7 @@ Task.new("db:autoupgrade") do |task|
|
|
19
22
|
task.define do |environment = "development", options|
|
20
23
|
Task[:environment].call(environment)
|
21
24
|
Rango.logger.info("[#{Rango.environment}] Upgrading databases ...")
|
22
|
-
result = DataMapper.auto_upgrade!
|
25
|
+
result = ::DataMapper.auto_upgrade!
|
23
26
|
Rango.logger.debug("Result: #{result.inspect}")
|
24
27
|
end
|
25
28
|
end
|
@@ -31,7 +34,7 @@ Task.new("db:report") do |task|
|
|
31
34
|
task.define do |environment = "development", options|
|
32
35
|
Task[:environment].call(environment)
|
33
36
|
ObjectSpace.classes.each do |klass|
|
34
|
-
if klass.included(DataMapper::Resource)
|
37
|
+
if klass.included(::DataMapper::Resource)
|
35
38
|
puts "#{model_class}: #{model_class.count}"
|
36
39
|
end
|
37
40
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "http_router"
|
5
|
+
rescue LoadError
|
6
|
+
raise LoadError, "You have to install http_router gem!"
|
7
|
+
end
|
8
|
+
|
9
|
+
Rango::Router.implement(:http_router) do |env|
|
10
|
+
env["rango.router.params"] = env["router.params"] || Hash.new # TODO: nil
|
11
|
+
end
|
12
|
+
|
13
|
+
module Rango
|
14
|
+
module UrlHelper
|
15
|
+
# url(:login)
|
16
|
+
def url(*args)
|
17
|
+
generator = Rango::Router.app.router.generator
|
18
|
+
route_name = args.shift
|
19
|
+
route = generator.usher.named_routes[route_name]
|
20
|
+
raise "No route found" if route.nil? # TODO: add RouteNotFound to usher and use it here as well
|
21
|
+
if args.empty?
|
22
|
+
generator.generate(route_name) # TODO: usher should probably have path.to_url
|
23
|
+
else
|
24
|
+
alts = route.paths.map(&:dynamic_keys) # one route can have multiple paths as /:id or /:id.:format
|
25
|
+
keys = alts.first
|
26
|
+
# FIXME: take a look at other alts as well !!!!
|
27
|
+
# keys = alts.find.with_index { |item, index| }
|
28
|
+
|
29
|
+
# TODO: optional args
|
30
|
+
keys_generator = keys.each
|
31
|
+
args_generator = args.each
|
32
|
+
opts = Hash.new
|
33
|
+
|
34
|
+
keys.length.times do |index|
|
35
|
+
key = keys_generator.next
|
36
|
+
arg = args_generator.next
|
37
|
+
if arg.respond_to?(key) # post instance
|
38
|
+
opts[key] = arg.send(key)
|
39
|
+
else # it's already a slug
|
40
|
+
opts[key] = arg
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
generator.generate(route_name, opts)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/rango/tasks/spec.nake
CHANGED
@@ -0,0 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "template-inheritance"
|
5
|
+
rescue LoadError
|
6
|
+
raise LoadError, "You have to install the template-inheritance gem if you want to use templates!"
|
7
|
+
end
|
8
|
+
|
9
|
+
TemplateInheritance::Template.paths << Rango.root.join("templates").to_s
|
10
|
+
TemplateInheritance.logger = Rango.logger
|
data/lib/rango/version.rb
CHANGED
@@ -41,8 +41,8 @@ describe Rango::RenderMixin do
|
|
41
41
|
body.should match(/Hi\!/)
|
42
42
|
end
|
43
43
|
|
44
|
-
it "should raise TemplateNotFound if template wasn't found" do
|
45
|
-
-> { render "idonotexist.html" }.should raise_error(
|
44
|
+
it "should raise TemplateInheritance::TemplateNotFound if template wasn't found" do
|
45
|
+
-> { render "idonotexist.html" }.should raise_error(TemplateInheritance::TemplateNotFound)
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
@@ -40,8 +40,7 @@ gem "rack-mount"#, git: "git://github.com/josh/rack-mount.git"
|
|
40
40
|
gem "rack-router"#, git: "git://github.com/carllerche/rack-router.git"
|
41
41
|
<% end %>
|
42
42
|
|
43
|
-
gem "
|
44
|
-
gem "haml"#, git: "git://github.com/nex3/haml.git"
|
43
|
+
gem "template-inheritance"#, git: "git://github.com/botanicus/template-inheritance.git"
|
45
44
|
gem "helpers"#, git: "git://github.com/botanicus/helpers.git"
|
46
45
|
gem "formidable"#, git: "git://github.com/botanicus/formidable.git"
|
47
46
|
gem "pupu"#, git: "git://github.com/botanicus/pupu.git"
|
metadata
CHANGED
@@ -5,22 +5,22 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 0.2.5.1
|
8
|
+
- 6
|
9
|
+
version: 0.2.6
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- "Jakub \xC5\xA0\xC5\xA5astn\xC3\xBD aka Botanicus"
|
14
13
|
autorequire:
|
15
14
|
bindir: bin
|
16
15
|
cert_chain:
|
17
|
-
date: 2010-
|
16
|
+
date: 2010-10-04 00:00:00 +01:00
|
18
17
|
default_executable: rango
|
19
18
|
dependencies:
|
20
19
|
- !ruby/object:Gem::Dependency
|
21
20
|
name: simple-templater
|
22
21
|
prerelease: false
|
23
22
|
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
@@ -36,6 +36,7 @@ dependencies:
|
|
36
36
|
name: bundler
|
37
37
|
prerelease: false
|
38
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
39
40
|
requirements:
|
40
41
|
- - ">="
|
41
42
|
- !ruby/object:Gem::Version
|
@@ -119,6 +120,7 @@ files:
|
|
119
120
|
- lib/rango/rest_controller.rb
|
120
121
|
- lib/rango/router.rb
|
121
122
|
- lib/rango/router/adapters/crudtree.rb
|
123
|
+
- lib/rango/router/adapters/http_router.rb
|
122
124
|
- lib/rango/router/adapters/rack_mount.rb
|
123
125
|
- lib/rango/router/adapters/urlmap.rb
|
124
126
|
- lib/rango/router/adapters/usher.rb
|
@@ -129,10 +131,7 @@ files:
|
|
129
131
|
- lib/rango/support/cucumber/steps/when_steps.rb
|
130
132
|
- lib/rango/tasks/spec.nake
|
131
133
|
- lib/rango/tasks/spec.rake
|
132
|
-
- lib/rango/
|
133
|
-
- lib/rango/templates/exts/tilt.rb
|
134
|
-
- lib/rango/templates/helpers.rb
|
135
|
-
- lib/rango/templates/template.rb
|
134
|
+
- lib/rango/template.rb
|
136
135
|
- lib/rango/utils.rb
|
137
136
|
- lib/rango/version.rb
|
138
137
|
- rango.gemspec
|
@@ -184,10 +183,6 @@ files:
|
|
184
183
|
- spec/rango/support/cucumber/steps/given_steps_spec.rb
|
185
184
|
- spec/rango/support/cucumber/steps/then_steps_spec.rb
|
186
185
|
- spec/rango/support/cucumber/steps/when_steps_spec.rb
|
187
|
-
- spec/rango/templates/exts/haml_spec.rb
|
188
|
-
- spec/rango/templates/exts/tilt_spec.rb
|
189
|
-
- spec/rango/templates/helpers_spec.rb
|
190
|
-
- spec/rango/templates/template_spec.rb
|
191
186
|
- spec/rango/utils_spec.rb
|
192
187
|
- spec/rango/version_spec.rb
|
193
188
|
- spec/rango_spec.rb
|
@@ -195,35 +190,6 @@ files:
|
|
195
190
|
- spec/spec_helper.rb
|
196
191
|
- spec/stubs/config.ru
|
197
192
|
- spec/stubs/flat.rb
|
198
|
-
- spec/stubs/templates/block/block.html.haml
|
199
|
-
- spec/stubs/templates/block/blocks.html.haml
|
200
|
-
- spec/stubs/templates/block/error.html.haml
|
201
|
-
- spec/stubs/templates/block/getter.html.haml
|
202
|
-
- spec/stubs/templates/block/value.html.haml
|
203
|
-
- spec/stubs/templates/context_id.html.haml
|
204
|
-
- spec/stubs/templates/enhance_block/basic.html.haml
|
205
|
-
- spec/stubs/templates/enhance_block/error.html.haml
|
206
|
-
- spec/stubs/templates/enhance_block/name_error.html.haml
|
207
|
-
- spec/stubs/templates/enhance_block/nil.html.haml
|
208
|
-
- spec/stubs/templates/enhance_block/standalone.html.haml
|
209
|
-
- spec/stubs/templates/extend_block/basic.html.haml
|
210
|
-
- spec/stubs/templates/extend_block/error.html.haml
|
211
|
-
- spec/stubs/templates/extend_block/error2.html.haml
|
212
|
-
- spec/stubs/templates/extend_block/name_error.html.haml
|
213
|
-
- spec/stubs/templates/extend_block/nil.html.haml
|
214
|
-
- spec/stubs/templates/includes/base.html.haml
|
215
|
-
- spec/stubs/templates/includes/basic.html.haml
|
216
|
-
- spec/stubs/templates/includes/includes.html.haml
|
217
|
-
- spec/stubs/templates/includes/integration.html.haml
|
218
|
-
- spec/stubs/templates/includes/integration2.html.haml
|
219
|
-
- spec/stubs/templates/index.html.haml
|
220
|
-
- spec/stubs/templates/inheritance/basic/base.html.haml
|
221
|
-
- spec/stubs/templates/inheritance/basic/index.html.haml
|
222
|
-
- spec/stubs/templates/inheritance/capture/haml/base.html.haml
|
223
|
-
- spec/stubs/templates/inheritance/capture/haml/index.html.haml
|
224
|
-
- spec/stubs/templates/library.html.haml
|
225
|
-
- spec/stubs/templates/test.html.haml
|
226
|
-
- spec/stubs/templates/variables.html.haml
|
227
193
|
- stubs/features/content/env.rb
|
228
194
|
- stubs/features/content/shared/.gitignore
|
229
195
|
- stubs/features/metadata.yml
|
@@ -281,19 +247,13 @@ has_rdoc: true
|
|
281
247
|
homepage: http://github.com/botanicus/rango
|
282
248
|
licenses: []
|
283
249
|
|
284
|
-
post_install_message:
|
285
|
-
[\e[32mVersion 0.2.5\e[0m] Added RESTController class as a base class for REST controllers\n\
|
286
|
-
[\e[32mVersion 0.2.5\e[0m] Added CRUDMixin with controller CRUD methods\n\
|
287
|
-
[\e[32mVersion 0.2.5\e[0m] UrlHelper#url can work not just as url(:post, @post.id) but also as url(:post, @post)\n\
|
288
|
-
[\e[32mVersion 0.2.5\e[0m] Action args mixin doesn't raise argument error if there are some splat or block arguments\n\
|
289
|
-
[\e[32mVersion 0.2.5\e[0m] Template caching\n\
|
290
|
-
[\e[32mVersion 0.2.5\e[0m] Fixed gemspec to find executables\n\
|
291
|
-
[\e[32mVersion 0.2.5\e[0m] First simple MIME support\n"
|
250
|
+
post_install_message:
|
292
251
|
rdoc_options: []
|
293
252
|
|
294
253
|
require_paths:
|
295
254
|
- lib
|
296
255
|
required_ruby_version: !ruby/object:Gem::Requirement
|
256
|
+
none: false
|
297
257
|
requirements:
|
298
258
|
- - ~>
|
299
259
|
- !ruby/object:Gem::Version
|
@@ -302,6 +262,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
302
262
|
- 9
|
303
263
|
version: "1.9"
|
304
264
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
265
|
+
none: false
|
305
266
|
requirements:
|
306
267
|
- - ">="
|
307
268
|
- !ruby/object:Gem::Version
|
@@ -311,7 +272,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
311
272
|
requirements: []
|
312
273
|
|
313
274
|
rubyforge_project: rango
|
314
|
-
rubygems_version: 1.3.
|
275
|
+
rubygems_version: 1.3.7
|
315
276
|
signing_key:
|
316
277
|
specification_version: 3
|
317
278
|
summary: Rango is ultralightweight, ultracustomizable, ultracool web framework deeply inspired by Django.
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Option default_attributes
|
4
|
-
#
|
5
|
-
# A hash of default attributes for tags (`{tag => {attribute => default_value}}`).
|
6
|
-
# Attributes of each tag will reverse merged with his default attributes, so you
|
7
|
-
# don't have to write over and over that script tag has attribute `type` with value
|
8
|
-
# `text/javascript`. For example, `%script` compiles to `<script type="text/javascript"></script>`.
|
9
|
-
# Defaults to `{script: {type: "text/javascript"}, form: {method: "POST"}}`
|
10
|
-
|
11
|
-
module Haml
|
12
|
-
module Precompiler
|
13
|
-
alias_method :__prerender_tag__, :prerender_tag
|
14
|
-
def prerender_tag(name, self_close, attributes)
|
15
|
-
# merge given attributes with default attributes from options
|
16
|
-
defaults = Tilt::HamlTemplate.options[:default_attributes][name.to_sym]
|
17
|
-
attributes = defaults.merge(attributes) if defaults
|
18
|
-
__prerender_tag__(name, self_close, attributes)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
class Buffer
|
23
|
-
alias_method :__open_tag__, :open_tag
|
24
|
-
def open_tag(name, self_closing, try_one_line, preserve_tag, escape_html, class_id,
|
25
|
-
nuke_outer_whitespace, nuke_inner_whitespace, obj_ref, content, *attributes_hashes)
|
26
|
-
defaults = Tilt::HamlTemplate.options[:default_attributes][name.to_sym]
|
27
|
-
attributes_hashes.unshift(defaults) if defaults
|
28
|
-
|
29
|
-
__open_tag__(name, self_closing, try_one_line, preserve_tag, escape_html, class_id,
|
30
|
-
nuke_outer_whitespace, nuke_inner_whitespace, obj_ref, content, *attributes_hashes)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "tilt"
|
4
|
-
|
5
|
-
module Rango
|
6
|
-
module TiltExtensions
|
7
|
-
# Tilt::HamlTemplate.options[:default_attributes] = {script: {type: "text/javascript"}, form: {method: "POST"}}
|
8
|
-
module Haml
|
9
|
-
def self.included(klass)
|
10
|
-
klass.send(:remove_method, :initialize_engine)
|
11
|
-
def klass.options
|
12
|
-
@options ||= Hash.new
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def initialize_engine
|
17
|
-
require_template_library 'haml' unless defined? ::Haml::Engine
|
18
|
-
require "rango/templates/exts/haml" if self.class.options[:default_attributes]
|
19
|
-
end
|
20
|
-
|
21
|
-
def initialize(*args)
|
22
|
-
super
|
23
|
-
self.options.merge!(self.class.options)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
Tilt::HamlTemplate.send(:include, Rango::TiltExtensions::Haml)
|
@@ -1,147 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "rango"
|
4
|
-
require "rango/templates/template"
|
5
|
-
|
6
|
-
module Rango
|
7
|
-
SubtemplateNotFound = Class.new(StandardError)
|
8
|
-
|
9
|
-
module TemplateHelpers
|
10
|
-
def self.extended(scope)
|
11
|
-
class << scope
|
12
|
-
attr_accessor :template
|
13
|
-
attr_accessor :context
|
14
|
-
# @example Capture being used in a .html.erb page:
|
15
|
-
# <% @foo = capture do %>
|
16
|
-
# <p>Some Foo content!</p>
|
17
|
-
# <% end %>
|
18
|
-
#
|
19
|
-
# @params [*args] Arguments to pass to the block.
|
20
|
-
# @params [&block] The template block to call.
|
21
|
-
# @return [String] The output of the block.
|
22
|
-
# @api private
|
23
|
-
def capture(*args, &block)
|
24
|
-
capture_method = "capture_#{self.template.adapter}"
|
25
|
-
if self.respond_to?(capture_method) # tilt doesn't support @_out_buf for haml
|
26
|
-
self.send("capture_#{self.template.adapter}", *args, &block)
|
27
|
-
else
|
28
|
-
# @_out_buf comes from tilt
|
29
|
-
unless self.instance_variable_defined?("@_out_buf")
|
30
|
-
raise "Adapter #{self.template.adapter} doesn't support capturing"
|
31
|
-
end
|
32
|
-
_old_buf, @_out_buf = @_out_buf, ""
|
33
|
-
block.call(*args)
|
34
|
-
@_out_buf = _old_buf.chomp.strip
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def concat(string)
|
39
|
-
concat_method = "concat_#{self.template.adapter}"
|
40
|
-
if self.respond_to?(concat_method) # tilt doesn't support @_out_buf for haml
|
41
|
-
self.send("concat_#{self.template.adapter}", string)
|
42
|
-
else
|
43
|
-
# @_out_buf comes from tilt
|
44
|
-
unless self.instance_variable_defined?("@_out_buf")
|
45
|
-
raise "Adapter #{self.template.adapter} doesn't support concating"
|
46
|
-
end
|
47
|
-
@_out_buf << string
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.included(scope_class)
|
54
|
-
scope_class.class_eval { attr_accessor :template }
|
55
|
-
end
|
56
|
-
|
57
|
-
# post/show.html: it's block is the block we like to see in output
|
58
|
-
# post/base.html
|
59
|
-
# base.html: here it will be rendered, so we need block to returns the correct block code
|
60
|
-
# @since 0.0.2
|
61
|
-
# @version 0.2
|
62
|
-
def block(name, value = nil, &block)
|
63
|
-
raise ArgumentError, "Block has to have a name!" if name.nil?
|
64
|
-
raise ArgumentError, "You have to provide value or block, not both of them!" if value && block
|
65
|
-
self.template.blocks[name] ||= block ? self.template.scope.capture(&block) : value
|
66
|
-
return self.template.blocks[name]
|
67
|
-
end
|
68
|
-
|
69
|
-
# - extend_block(:head) do
|
70
|
-
# = pupu :lighter, syntax: "html", theme: "standard"
|
71
|
-
# = block(:head)
|
72
|
-
def extend_block(name, value = nil, &block)
|
73
|
-
unless self.template.blocks[name]
|
74
|
-
raise NameError, "Block #{name.inspect} wasn't defined yet, you can't extend it!"
|
75
|
-
end
|
76
|
-
self.enhance_block(name, value, &block)
|
77
|
-
end
|
78
|
-
|
79
|
-
def enhance_block(name, value = nil, &block)
|
80
|
-
raise ArgumentError, "Block has to have a name!" if name.nil?
|
81
|
-
raise ArgumentError, "You have to provide value or block, not both of them!" if value && block
|
82
|
-
value = self.template.scope.capture(&block) if value.nil? && block
|
83
|
-
self.template.blocks[name] = value if value
|
84
|
-
return self.template.blocks[name]
|
85
|
-
end
|
86
|
-
|
87
|
-
# Clears default content of given block.
|
88
|
-
#
|
89
|
-
# @example
|
90
|
-
# clear_block(:flyout)
|
91
|
-
def clear_block(name)
|
92
|
-
raise ArgumentError, "You need to specify name of block to clear." if name.nil?
|
93
|
-
self.template.blocks[name] = String.new
|
94
|
-
end
|
95
|
-
|
96
|
-
# Low-level rendering method for templates.
|
97
|
-
#
|
98
|
-
# @since 0.2
|
99
|
-
# @example
|
100
|
-
# render "base.html"
|
101
|
-
# render "./base.html"
|
102
|
-
# render "../base.html"
|
103
|
-
def render(path, context = Hash.new)
|
104
|
-
full_path = normalize_template_path(path)
|
105
|
-
original_template = self.template
|
106
|
-
template = Rango::Template.new(full_path, self) # self is scope
|
107
|
-
self.template = original_template
|
108
|
-
return template.render(context)
|
109
|
-
rescue Exceptions::TemplateNotFound # FIXME: this doesn't work
|
110
|
-
raise SubtemplateNotFound, "Template #{path} doesn't exist in #{full_path}"
|
111
|
-
end
|
112
|
-
|
113
|
-
# partial "products/list"
|
114
|
-
# @since 0.0.2
|
115
|
-
# @version 0.2.1
|
116
|
-
def partial(template, extra_context = Hash.new)
|
117
|
-
# NOTE: we can't use File.split because it normalize the path,
|
118
|
-
# so "./base.html" will be the same as "base.html", but it shouldn't be
|
119
|
-
*path, basename = template.split("/")
|
120
|
-
render File.join(path.join("/"), "_#{basename}"), self.template.context.merge(extra_context)
|
121
|
-
end
|
122
|
-
|
123
|
-
# @since 0.2
|
124
|
-
def includes(template, context = Hash.new)
|
125
|
-
render normalize_template_path(template), context
|
126
|
-
return true
|
127
|
-
end
|
128
|
-
|
129
|
-
# extends "base.html"
|
130
|
-
# @since 0.0.2
|
131
|
-
def extends(path)
|
132
|
-
# we can't just create a new template, because it has to do it after it reads the whole file
|
133
|
-
self.template.supertemplate = normalize_template_path(path)
|
134
|
-
end
|
135
|
-
|
136
|
-
# @since 0.2
|
137
|
-
def normalize_template_path(template)
|
138
|
-
if template.start_with?("./")
|
139
|
-
File.expand_path(File.join(File.dirname(self.template.fullpath), template))
|
140
|
-
elsif template.start_with?("../")
|
141
|
-
File.expand_path(File.join(File.dirname(self.template.fullpath), "..", template))
|
142
|
-
else
|
143
|
-
template
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
@@ -1,87 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "rango/templates/exts/tilt"
|
4
|
-
require "rango/core_ext" # String#snake_case
|
5
|
-
require "rango/templates/helpers"
|
6
|
-
require "rango/exceptions"
|
7
|
-
|
8
|
-
module Rango
|
9
|
-
module Exceptions # so we can catch it with other HTTP errors
|
10
|
-
TemplateNotFound = Class.new(NotFound) { self.name = "Template Not Found" }
|
11
|
-
end
|
12
|
-
|
13
|
-
class Template
|
14
|
-
def self.template_paths
|
15
|
-
@@template_paths ||= [Rango.root.join("templates").to_s]
|
16
|
-
end
|
17
|
-
|
18
|
-
# template -> supertemplate is the same relationship as class -> superclass
|
19
|
-
# @since 0.0.2
|
20
|
-
attr_accessor :path, :scope, :supertemplate, :context
|
21
|
-
|
22
|
-
# @since 0.0.2
|
23
|
-
attr_writer :blocks
|
24
|
-
def blocks
|
25
|
-
@blocks ||= Hash.new
|
26
|
-
end
|
27
|
-
|
28
|
-
# @since 0.0.2
|
29
|
-
def initialize(path, scope = Object.new)
|
30
|
-
self.path = path#[scope.class.template_prefix.chomp("/"), template].join("/")
|
31
|
-
self.scope = scope
|
32
|
-
self.scope.extend(TemplateHelpers)
|
33
|
-
# this enables template caching
|
34
|
-
unless Rango.development?
|
35
|
-
self.scope.extend(Tilt::CompileSite)
|
36
|
-
end
|
37
|
-
self.scope.template = self
|
38
|
-
end
|
39
|
-
|
40
|
-
# @since 0.0.2
|
41
|
-
def fullpath
|
42
|
-
@fullpath ||= begin
|
43
|
-
if self.path.match(/^(\/|\.)/) # /foo or ./foo
|
44
|
-
Dir[self.path, "#{self.path}.*"].find {|file| !File.directory?(file)}
|
45
|
-
else
|
46
|
-
self.find_in_template_paths
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def adapter
|
52
|
-
self.template.class.name.split("::").last.snake_case.sub("_template", "")
|
53
|
-
end
|
54
|
-
|
55
|
-
def extension # haml, erb ...
|
56
|
-
File.extname(path)[1..-1]
|
57
|
-
end
|
58
|
-
|
59
|
-
def template(options = Hash.new)
|
60
|
-
@template ||= Tilt.new(self.fullpath, nil, options)
|
61
|
-
end
|
62
|
-
|
63
|
-
# @since 0.0.2
|
64
|
-
def render(context = Hash.new)
|
65
|
-
raise Exceptions::TemplateNotFound.new("Template #{self.path} wasn't found in these template_paths: #{self.class.template_paths.inspect}") if self.fullpath.nil?
|
66
|
-
Rango.logger.info("Rendering template #{self.path} with context keys #{context.keys.inspect}")
|
67
|
-
self.scope.context = self.context = context # so we can access context in the scope object as well
|
68
|
-
value = self.template.render(self.scope, context)
|
69
|
-
Rango.logger.debug("Available blocks: #{self.blocks.keys.inspect}")
|
70
|
-
if self.supertemplate
|
71
|
-
Rango.logger.debug("Extends call: #{self.supertemplate}")
|
72
|
-
supertemplate = self.class.new(self.supertemplate, self.scope)
|
73
|
-
supertemplate.blocks = self.blocks
|
74
|
-
return supertemplate.render(context)
|
75
|
-
end
|
76
|
-
value
|
77
|
-
end
|
78
|
-
|
79
|
-
protected
|
80
|
-
def find_in_template_paths
|
81
|
-
self.class.template_paths.each do |directory|
|
82
|
-
path = File.join(directory, self.path)
|
83
|
-
return Dir[path, "#{path}.*"].find {|file| !File.directory?(file)}
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
File without changes
|
File without changes
|
@@ -1,139 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require_relative "../../spec_helper"
|
4
|
-
require "rango/templates/helpers"
|
5
|
-
|
6
|
-
Rango::Template.template_paths.clear.push(File.join(STUBS_ROOT, "templates"))
|
7
|
-
|
8
|
-
describe Rango::TemplateHelpers do
|
9
|
-
include Rango::TemplateHelpers
|
10
|
-
describe "#partial" do
|
11
|
-
it "should work" do
|
12
|
-
pending "This can't work because self.template doesn't exist, we have to use render mixin"
|
13
|
-
partial "basic.html"
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should share context with the parent template" do
|
17
|
-
pending
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should be able to specify additional context which isn't propagated to the parent template" do
|
21
|
-
pending
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "#render" do
|
26
|
-
it "should consider 'path.html' as a path relative to Template.template_paths" do
|
27
|
-
pending
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should consider './path.html' as a path relative to the current template" do
|
31
|
-
pending
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should look for '../path.html' in the parent directory of directory with current template" do
|
35
|
-
pending
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe "#includes" do
|
40
|
-
require "rango/mixins/render"
|
41
|
-
it "should return true" do
|
42
|
-
Rango::RenderMixin.render("includes/basic.html").strip.should eql("true")
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should work with blocks" do
|
46
|
-
Rango::RenderMixin.render("includes/includes.html")
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should work with extends" do
|
50
|
-
output = Rango::RenderMixin.render("includes/integration.html")
|
51
|
-
output.strip.should eql("Greeting by Jakub Stastny")
|
52
|
-
end
|
53
|
-
|
54
|
-
it "should work with extends" do
|
55
|
-
output = Rango::RenderMixin.render("includes/integration2.html")
|
56
|
-
output.strip.should eql("Greeting by Jakub Stastny")
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "extends" do
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "block" do
|
64
|
-
it "should raise argument error if name isn't specified" do
|
65
|
-
-> { block(nil) }.should raise_error(ArgumentError)
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should work as a setter if a value is provided" do
|
69
|
-
output = Rango::RenderMixin.render("block/value.html")
|
70
|
-
output.strip.should eql("a value")
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should work as a setter if a block is provided" do
|
74
|
-
output = Rango::RenderMixin.render("block/block.html")
|
75
|
-
output.strip.should eql("a block")
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should work as a getter" do
|
79
|
-
output = Rango::RenderMixin.render("block/getter.html")
|
80
|
-
output.strip.should eql("Hello World!")
|
81
|
-
end
|
82
|
-
|
83
|
-
it "should raise argument error if both value and block is provided" do
|
84
|
-
-> { Rango::RenderMixin.render("block/error.html") }.should raise_error(ArgumentError)
|
85
|
-
end
|
86
|
-
|
87
|
-
it "should store the first non-nil value" do
|
88
|
-
output = Rango::RenderMixin.render("block/blocks.html")
|
89
|
-
output.strip.should eql("first")
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
describe "extend_block" do
|
94
|
-
it "should raise argument error if name isn't specified" do
|
95
|
-
-> { Rango::RenderMixin.render("extend_block/name_error.html") }.should raise_error(NameError)
|
96
|
-
end
|
97
|
-
|
98
|
-
it "should raise argument error if both value and block is provided" do
|
99
|
-
-> { Rango::RenderMixin.render("extend_block/error.html") }.should raise_error(ArgumentError)
|
100
|
-
end
|
101
|
-
|
102
|
-
it "should raise argument error if block of given name doesn't exist so far" do
|
103
|
-
-> { Rango::RenderMixin.render("extend_block/error2.html") }.should raise_error
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should work with super()-like inheritance" do
|
107
|
-
Rango::RenderMixin.render("extend_block/basic.html")
|
108
|
-
end
|
109
|
-
|
110
|
-
it "should do nothing if the value or block is nil" do
|
111
|
-
output = Rango::RenderMixin.render("extend_block/nil.html")
|
112
|
-
output.strip.should eql("Original")
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
describe "enhance_block" do
|
117
|
-
it "should work even if the block isn't defined so far" do
|
118
|
-
output = Rango::RenderMixin.render("enhance_block/standalone.html")
|
119
|
-
output.strip.should eql("Hello World!")
|
120
|
-
end
|
121
|
-
|
122
|
-
it "should not raise argument error if name isn't specified" do
|
123
|
-
-> { Rango::RenderMixin.render("enhance_block/name_error.html") }.should_not raise_error(NameError)
|
124
|
-
end
|
125
|
-
|
126
|
-
it "should raise argument error if both value and block is provided" do
|
127
|
-
-> { Rango::RenderMixin.render("enhance_block/error.html") }.should raise_error(ArgumentError)
|
128
|
-
end
|
129
|
-
|
130
|
-
it "should work with super()-like inheritance" do
|
131
|
-
Rango::RenderMixin.render("enhance_block/basic.html")
|
132
|
-
end
|
133
|
-
|
134
|
-
it "should do nothing if the value or block is nil" do
|
135
|
-
output = Rango::RenderMixin.render("enhance_block/nil.html")
|
136
|
-
output.strip.should eql("Original")
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require_relative "../../spec_helper"
|
4
|
-
require "rango/templates/template"
|
5
|
-
|
6
|
-
Rango::Template.template_paths.clear.push(File.join(STUBS_ROOT, "templates"))
|
7
|
-
|
8
|
-
describe Rango::Template do
|
9
|
-
describe "#initialize" do
|
10
|
-
it "should take path as a first argument" do
|
11
|
-
template = Rango::Template.new("test.html")
|
12
|
-
template.path.should eql("test.html")
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should take scope as an optional second argument" do
|
16
|
-
scope = Object.new
|
17
|
-
template = Rango::Template.new("test.html", scope)
|
18
|
-
template.scope.should eql(scope)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "#fullpath" do
|
23
|
-
it "should find" do
|
24
|
-
template = Rango::Template.new("test.html")
|
25
|
-
fullpath = File.join(STUBS_ROOT, "templates", "test.html.haml")
|
26
|
-
template.fullpath.should eql(fullpath)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
describe "#render" do
|
31
|
-
it "should raise TemplateNotFound if template can't be found" do
|
32
|
-
template = Rango::Template.new("idonotexist.html")
|
33
|
-
-> { template.render }.should raise_error(Rango::Exceptions::TemplateNotFound)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should render" do
|
37
|
-
template = Rango::Template.new("test.html")
|
38
|
-
template.render.should eql("<html></html>\n")
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should have template inheritance" do
|
42
|
-
template = Rango::Template.new("inheritance/basic/index.html")
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should capture haml" do
|
46
|
-
template = Rango::Template.new("inheritance/capture/haml/index.html")
|
47
|
-
template.render
|
48
|
-
template.blocks[:content].should match("Hello!")
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe "variables" do
|
53
|
-
before(:each) do
|
54
|
-
@template = Rango::Template.new("variables.html")
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should capture erb" do
|
58
|
-
# @template.render(title: "Hi!").should match("Hi!")
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1 +0,0 @@
|
|
1
|
-
= block(:one, "a value")
|
@@ -1 +0,0 @@
|
|
1
|
-
= self.object_id
|
@@ -1 +0,0 @@
|
|
1
|
-
- enhance_block(nil)
|
@@ -1 +0,0 @@
|
|
1
|
-
= extend_block(:content, "foo")
|
@@ -1 +0,0 @@
|
|
1
|
-
- extend_block(nil)
|
@@ -1 +0,0 @@
|
|
1
|
-
== #{block(:title)} by #{block(:author)}
|
@@ -1 +0,0 @@
|
|
1
|
-
= includes "library.html"
|
@@ -1 +0,0 @@
|
|
1
|
-
= title
|
@@ -1 +0,0 @@
|
|
1
|
-
%html
|
@@ -1 +0,0 @@
|
|
1
|
-
= title
|