Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: igrigorik/contextual
base: 78771d88c6
...
head fork: igrigorik/contextual
compare: 83f63c4498
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 6 files changed
  • 0 commit comments
  • 2 contributors
Commits on Apr 05, 2012
@ratnikov ratnikov Made sure that all rails hooks are run on action_view load event.
This fixed the race condition when action_view may have been loaded later than contextual,
causing a "wrong superclass" exception.
4a61a66
Commits on Apr 09, 2012
@igrigorik Merge pull request #4 from ratnikov/master
Refactor rails hook
83f63c4
View
3  lib/contextual.rb
@@ -1,3 +1,4 @@
require "contextual/version"
require "contextual/contextual"
-require "contextual/rails_erubis" if defined? Rails
+
+require "contextual/rails" if defined? Rails
View
10 lib/contextual/rails.rb
@@ -0,0 +1,10 @@
+require 'contextual/safe_erubis'
+
+ActiveSupport.on_load(:action_view) do
+ ActionView::Template::Handlers::SafeErubis = Contextual::SafeErubis
+ ActionView::Template::Handlers::ERB.erb_implementation = Contextual::SafeErubis
+
+ # Make sure ActionView::OutputBuffer is loaded before we override it
+ require 'action_view/buffers'
+ ActionView::OutputBuffer = ::Erubis::ContextualBuffer
+end
View
96 lib/contextual/rails_erubis.rb
@@ -1,96 +0,0 @@
-require 'action_view'
-
-module ActionView
- class Template
- module Handlers
-
- # class Erubis < ::Erubis::Eruby
- # def add_preamble(src)
- # src << "@output_buffer = output_buffer || ActionView::OutputBuffer.new;"
- # end
- #
- # def add_text(src, text)
- # return if text.empty?
- # p [:add_text, :safe_concat, text]
- # src << "@output_buffer.safe_concat('" << escape_text(text) << "');"
- # end
- #
- # BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/
- #
- # def add_expr_literal(src, code)
- # if code =~ BLOCK_EXPR
- # p [:add_expr_literal, :block_append=, code]
- #
- # src << '@output_buffer.append= ' << code
- # else
- # p [:add_expr_literal, :append=, code]
- #
- # src << '@output_buffer.append= (' << code << ');'
- # end
- # end
- #
- # def add_expr_escaped(src, code)
- # if code =~ BLOCK_EXPR
- # p [:add_expr_escaped, :safe_append=, code]
- #
- # src << "@output_buffer.safe_append= " << code
- # else
- # p [:add_expr_escaped, :safe_concat, code]
- # src << "@output_buffer.safe_concat((" << code << ").to_s);"
- # end
- # end
- #
- # def add_postamble(src)
- # src << '@output_buffer.to_s'
- # end
- # end
-
- class SafeErubis < ::Erubis::Eruby
- BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/
-
- def add_preamble(src)
- src << "@output_buffer = output_buffer || Erubis::ContextualBuffer.new; "
- end
-
- def add_text(src, text)
- if !text.empty?
- src << "@output_buffer.concat('" << text.to_s.gsub("'", "\\\\'") << "');"
- end
- end
-
- def add_expr_literal(src, code)
- if code =~ BLOCK_EXPR
- src << '@output_buffer.append= ' << code
- else
- src << <<-SRC
- val = (#{code.to_s});
- if (val.html_safe?);
- @output_buffer.append=(val);
- else;
- @output_buffer.safe_append=(val);
- end;
- SRC
- end
- end
-
- def add_expr_escaped(src, code)
- if code =~ BLOCK_EXPR
- src << "@output_buffer.append= " << code
- else
- src << "@output_buffer.append(" << code << ");"
- end
- end
-
- def add_postamble(src)
- src << "@output_buffer.close \n"
- # src << "p [:CONTEXTUAL,@output_buffer, @output_buffer.to_s, @output_buffer.to_s.html_safe.html_safe?]\n"
- src << "@output_buffer.to_s.html_safe"
- end
- end
-
- ERB.erb_implementation = SafeErubis
- ActionView::OutputBuffer = ::Erubis::ContextualBuffer
-
- end
- end
-end
View
44 lib/contextual/safe_erubis.rb
@@ -0,0 +1,44 @@
+module Contextual
+ class SafeErubis < ::Erubis::Eruby
+ BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/
+
+ def add_preamble(src)
+ src << "@output_buffer = output_buffer || Erubis::ContextualBuffer.new; "
+ end
+
+ def add_text(src, text)
+ if !text.empty?
+ src << "@output_buffer.concat('" << text.to_s.gsub("'", "\\\\'") << "');"
+ end
+ end
+
+ def add_expr_literal(src, code)
+ if code =~ BLOCK_EXPR
+ src << '@output_buffer.append= ' << code
+ else
+ src << <<-SRC
+ val = (#{code.to_s});
+ if (val.html_safe?);
+ @output_buffer.append=(val);
+ else;
+ @output_buffer.safe_append=(val);
+ end;
+ SRC
+ end
+ end
+
+ def add_expr_escaped(src, code)
+ if code =~ BLOCK_EXPR
+ src << "@output_buffer.append= " << code
+ else
+ src << "@output_buffer.append(" << code << ");"
+ end
+ end
+
+ def add_postamble(src)
+ src << "@output_buffer.close \n"
+ # src << "p [:CONTEXTUAL,@output_buffer, @output_buffer.to_s, @output_buffer.to_s.html_safe.html_safe?]\n"
+ src << "@output_buffer.to_s.html_safe"
+ end
+ end
+end
View
21 spec/rails_spec.rb
@@ -0,0 +1,21 @@
+require 'rails'
+
+require 'contextual'
+
+# make sure railtie is loaded in case some other
+# test task required contextual before rails was loaded
+require 'contextual/rails'
+
+# Force the load hooks to be run for action_view
+require 'action_view'
+require 'action_view/base'
+
+describe "Contextual load hooks" do
+ it "should add SafeErubis as template handler" do
+ ActionView::Template::Handlers::SafeErubis.should == Contextual::SafeErubis
+ end
+
+ it "should be able to load action_view/buffers alright" do
+ require 'action_view/buffers'
+ end
+end
View
6 spec/rails_handler_spec.rb → spec/safe_erubis_spec.rb
@@ -1,10 +1,8 @@
-require 'rails'
-
-require 'contextual'
+require 'contextual/safe_erubis'
describe Contextual do
it 'should allow fixnums' do
- template = ActionView::Template::Handlers::SafeErubis.new <<-TEMPLATE
+ template = Contextual::SafeErubis.new <<-TEMPLATE
Number: <%= 42 %>
TEMPLATE

No commit comments for this range

Something went wrong with that request. Please try again.