Skip to content

Commit

Permalink
Move WebConsole::Template::Context into WebConsole::View
Browse files Browse the repository at this point in the history
  • Loading branch information
sh19910711 committed Aug 14, 2015
1 parent b561e8a commit ad17a5a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
1 change: 1 addition & 0 deletions lib/web_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require 'web_console/whitelist'
require 'web_console/request'
require 'web_console/response'
require 'web_console/view'

module WebConsole
mattr_accessor :logger
Expand Down
31 changes: 2 additions & 29 deletions lib/web_console/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,6 @@ module WebConsole
# It introduces template helpers to ease the inclusion of scripts only on
# Rails error pages.
class Template
class Context < ActionView::Base
# Execute a block only on error pages.
#
# The error pages are special, because they are the only pages that
# currently require multiple bindings. We get those from exceptions.
def only_on_error_page(*args)
yield if @env['web_console.exception'].present?
end

# Render JavaScript inside a script tag and a closure.
#
# This one lets write JavaScript that will automatically get wrapped in a
# script tag and enclosed in a closure, so you don't have to worry for
# leaking globals, unless you explicitly want to.
def render_javascript(template)
render(template: template, layout: 'layouts/javascript')
end

# Render inlined string to be used inside of JavaScript code.
#
# The inlined string is returned as an actual JavaScript string. You
# don't need to wrap the result yourself.
def render_inlined_string(template)
render(template: template, layout: 'layouts/inlined_string')
end
end

# Lets you customize the default templates folder location.
cattr_accessor :template_paths
@@template_paths = [ File.expand_path('../templates', __FILE__) ]
Expand All @@ -43,8 +16,8 @@ def initialize(env, session)

# Render a template (inferred from +template_paths+) as a plain string.
def render(template)
context = Context.new(template_paths, instance_values)
context.render(template: template, layout: false)
view = View.new(template_paths, instance_values)
view.render(template: template, layout: false)
end
end
end
15 changes: 2 additions & 13 deletions lib/web_console/testing/fake_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'json'
require 'web_console/whitelist'
require 'web_console/request'
require 'web_console/view'

module WebConsole
module Testing
Expand All @@ -20,7 +21,7 @@ def call(env)
end

def view
@view ||= create_view
@view ||= View.new(@view_path)
end

private
Expand All @@ -33,18 +34,6 @@ def req_path(env)
def render(template)
view.render(template: template, layout: nil)
end

def create_view
lookup_context = ActionView::LookupContext.new(@view_path)
lookup_context.cache = false
FakeView.new(lookup_context)
end

class FakeView < ActionView::Base
def render_inlined_string(template)
render(template: template, layout: "layouts/inlined_string")
end
end
end
end
end
33 changes: 33 additions & 0 deletions lib/web_console/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module WebConsole
class View < ActionView::Base
# Execute a block only on error pages.
#
# The error pages are special, because they are the only pages that
# currently require multiple bindings. We get those from exceptions.
def only_on_error_page(*args)
yield if @env['web_console.exception'].present?
end

# Render JavaScript inside a script tag and a closure.
#
# This one lets write JavaScript that will automatically get wrapped in a
# script tag and enclosed in a closure, so you don't have to worry for
# leaking globals, unless you explicitly want to.
def render_javascript(template)
render(template: template, layout: 'layouts/javascript')
end

# Render inlined string to be used inside of JavaScript code.
#
# The inlined string is returned as an actual JavaScript string. You
# don't need to wrap the result yourself.
def render_inlined_string(template)
render(template: template, layout: 'layouts/inlined_string')
end

# Escaped alias for "ActionView::Helpers::TranslationHelper.t".
def t(key, options = {})
super.gsub("\n", "\\n")
end
end
end

0 comments on commit ad17a5a

Please sign in to comment.