Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

inject things into views

  • Loading branch information...
commit f964a9cc3f674140bb3884bde4a4df2d75252442 1 parent 33ffbca
@tcrayford authored
View
7 lib/raptor/injector.rb
@@ -29,10 +29,13 @@ def args(method)
end.reject do |type, name|
type == :opt && sources[name].nil?
end.map do |type, name|
- source_proc = sources[name] or raise UnknownInjectable.new(name)
- source_proc.call
+ inject_name(name)
end
end
+ def inject_name(name)
+ source_proc = sources[name] or raise UnknownInjectable.new(name)
+ source_proc.call
+ end
def parameters(method)
method.parameters
View
3  lib/raptor/responders.rb
@@ -65,7 +65,8 @@ def self.action_template(app, presenter_name, parent_path, template_name)
def respond(route, subject, injector)
presenter = create_presenter(subject, injector)
- Rack::Response.new(render(presenter))
+ context = Raptor::ViewContext.new(presenter, injector)
+ Rack::Response.new(render(context))
end
def render(presenter)
View
15 lib/raptor/templates.rb
@@ -17,8 +17,8 @@ def self.find(path)
end
class NullLayout
- def self.render(inner, presenter)
- inner.render(presenter)
+ def self.render(inner, context)
+ inner.render(context)
end
end
@@ -37,17 +37,17 @@ def ==(other)
other.tilt == tilt
end
- def render(inner, presenter)
- context = ViewContext.new(presenter)
+ def render(inner, context)
rendered = inner.render(context)
@tilt.render(context) { rendered }
end
end
class ViewContext < BasicObject
- def initialize(presenter)
+ def initialize(presenter, injector)
@presenter = presenter
@areas = {}
+ @injector = injector
end
def content_for(name, &block)
@@ -59,6 +59,11 @@ def content_for(name, &block)
end
end
+ def inject(name)
+ ::Raptor.log("Injecting #{name.inspect} into view")
+ @injector.inject_name(name)
+ end
+
def method_missing(name, *args, &block)
@presenter.send(name, *args, &block)
end
View
6 spec/injector_spec.rb
@@ -97,6 +97,12 @@ def takes_watermelon(watermelon); watermelon; end
injector.call(method(:takes_watermelon)).should == "fruity"
end
end
+
+ it "can be asked for an injection by name" do
+ shins = stub
+ injector = Raptor::Injector.new([Raptor::Injectables::Fixed.new(:shins, shins)])
+ injector.inject_name(:shins).should == shins
+ end
end
class ObjectWithInitializerTakingId
View
19 spec/template_spec.rb
@@ -1,6 +1,6 @@
require "rack"
require_relative "spec_helper"
-require_relative "../lib/raptor/templates"
+require_relative "../lib/raptor"
describe Raptor::Template do
let(:presenter) { stub("presenter") }
@@ -26,6 +26,7 @@
describe Raptor::Layout do
let(:presenter) { stub }
+ let(:injector) { Raptor::Injector.new([]) }
it "renders a yielded template" do
inner = stub(:render => 'inner')
@@ -37,28 +38,36 @@
it "integrates content_for" do
inner = Raptor::Template.from_path("../spec/fixtures/provides_content_for.html.erb")
layout = Raptor::Layout.from_path('spec/fixtures/layout_with_content_for.html.erb')
- rendered = layout.render(inner, presenter)
+ rendered = layout.render(inner, Raptor::ViewContext.new(presenter, injector))
rendered.strip.should include("<script></script")
end
end
describe Raptor::ViewContext do
+ let(:injector) { Raptor::Injector.new([]) }
it "delegates to the presenter" do
- Raptor::ViewContext.new(stub(:cheese => 'walrus')).cheese.should == 'walrus'
+ Raptor::ViewContext.new(stub(:cheese => 'walrus'), injector).cheese.should == 'walrus'
end
it "stores content_for" do
- context = Raptor::ViewContext.new(stub)
+ context = Raptor::ViewContext.new(stub, injector)
context.content_for(:head) { 'what' }
context.content_for(:head).should == 'what'
end
it "appends multiple content_for blocks" do
- context = Raptor::ViewContext.new(stub)
+ context = Raptor::ViewContext.new(stub, injector)
context.content_for(:head) { 'what' }
context.content_for(:head) { 'what' }
context.content_for(:head).should == 'whatwhat'
end
+
+ it "injects from the injector" do
+ current_user = stub
+ injector = Raptor::Injector.new([Raptor::Injectables::Fixed.new(:current_user, current_user)])
+ context = Raptor::ViewContext.new(stub, injector)
+ context.inject(:current_user).should == current_user
+ end
end
describe Raptor::FindsLayouts do
Please sign in to comment.
Something went wrong with that request. Please try again.