Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use context => self for mote_vars (@djanowski).

This also solves the problem of having to use
`Rack::Session::Cookie` when you don't care about
sessions entirely.

Also goes hand in hand with the latest mote style
which evals on the TOPLEVEL_BINDING instead of
on the current context.
  • Loading branch information...
commit 3f997070d7cc42e3979167885a94c4edb5c0a8ed 1 parent 0ce272d
@cyx authored
Showing with 29 additions and 12 deletions.
  1. +10 −5 lib/cuba/contrib/mote.rb
  2. +19 −7 test/mote.rb
View
15 lib/cuba/contrib/mote.rb
@@ -5,28 +5,33 @@ module Mote
include ::Mote::Helpers
def self.setup(app)
- app.settings[:views] ||= File.expand_path("views", Dir.pwd)
- app.settings[:layout] ||= "layout"
+ app.settings[:mote] ||= {}
+ app.settings[:mote][:views] ||= File.expand_path("views", Dir.pwd)
+ app.settings[:mote][:layout] ||= "layout"
end
def partial(template, locals = {})
mote(mote_path(template), locals)
end
- def view(template, locals = {}, layout = settings[:layout])
+ def view(template, locals = {}, layout = settings[:mote][:layout])
raise NoLayout.new(self) unless layout
partial(layout, locals.merge(mote_vars(partial(template, locals))))
end
+ def render(template, locals = {}, layout = settings[:mote][:layout])
+ res.write view(template, locals, layout)
+ end
+
def mote_path(template)
return template if template.end_with?(".mote")
- File.join(settings[:views], "#{template}.mote")
+ File.join(settings[:mote][:views], "#{template}.mote")
end
def mote_vars(content)
- { content: content, session: session }
+ { context: self, content: content }
end
class NoLayout < StandardError
View
26 test/mote.rb
@@ -1,19 +1,20 @@
require_relative "helper"
test "doesn't override already existing settings" do
- Cuba.settings[:views] = "./templates"
- Cuba.settings[:layout] = "guest"
+ Cuba.settings[:mote] ||= {}
+ Cuba.settings[:mote][:views] = "./templates"
+ Cuba.settings[:mote][:layout] = "guest"
Cuba.plugin Cuba::Mote
- assert_equal "./templates", Cuba.settings[:views]
- assert_equal "guest", Cuba.settings[:layout]
+ assert_equal "./templates", Cuba.settings[:mote][:views]
+ assert_equal "guest", Cuba.settings[:mote][:layout]
end
Cuba.plugin Cuba::Mote
Cuba.use Rack::Session::Cookie
-Cuba.settings[:views] = "./test/views"
-Cuba.settings[:layout] = "layout"
+Cuba.settings[:mote][:views] = "./test/views"
+Cuba.settings[:mote][:layout] = "layout"
Cuba.define do
on "frag" do
@@ -24,6 +25,10 @@
res.write view("home", title: "Hola")
end
+ on "render" do
+ render("home", title: "Hola")
+ end
+
on "abs_path" do
res.write view("./test/views/custom/abs_path.mote", title: "Absolute")
end
@@ -35,6 +40,13 @@
assert_response body, ["<title>Hola</title>\n<h1>Home</h1>\n\n"]
end
+test "render" do
+ _, _, body = Cuba.call({ "PATH_INFO" => "/render", "SCRIPT_NAME" => "" })
+
+ assert_response body, ["<title>Hola</title>\n<h1>Home</h1>\n\n"]
+end
+
+
test "partial" do
_, _, body = Cuba.call({ "PATH_INFO" => "/frag", "SCRIPT_NAME" => "" })
@@ -48,7 +60,7 @@
end
test "use of absolute mote path for the layout" do
- Cuba.settings[:layout] = "./test/views/custom_layout.mote"
+ Cuba.settings[:mote][:layout] = "./test/views/custom_layout.mote"
_, _, body = Cuba.call({ "PATH_INFO" => "/", "SCRIPT_NAME" => "" })
Please sign in to comment.
Something went wrong with that request. Please try again.