Skip to content

Commit

Permalink
Second attempt.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Eberlin committed Dec 23, 2015
1 parent 5721663 commit a97093c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
53 changes: 53 additions & 0 deletions app/helpers/woo/render_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Woo
module RenderHelper
DEFAULT_VIEW_CONTEXTS = [
Haml::Helpers,
StyleguideHelper
]

module FallbackDelegationHelper
def method_missing(name, *args, &block)
if respond_to?(name, true)
send(name, *args, &block)
elsif controller.respond_to?(name, true)
controller.send(name, *args, &block)
else
super(name, *args, &block)
end
end
end

def application_view_context
view_paths = Rails.application.config.paths['app/views']

@app_view_context ||= ActionView::Base.new(view_paths, {}, controller).tap do |context|
view_contexts.each { |mod| context.extend(mod) }
context.define_singleton_method(:protect_against_forgery?) { nil }
end
end

def application_view_paths
@app_view_paths ||= Rails.application.config.paths['app/views']
end

def engine_view_context
@engine_view_context ||= controller.view_context.tap do |context|
context.extend(FallbackDelegationHelper)
end
end

def haml_buffer
@haml_buffer ||= Haml::Buffer.new(nil, Haml::Options.new.for_buffer)
end

def render_haml_string(contents)
Haml::Engine.new(contents).render(engine_view_context)
rescue ActionView::MissingTemplate, NoMethodError, NameError
Haml::Engine.new(contents).render(application_view_context)
end

def view_contexts
@view_contexts ||= DEFAULT_VIEW_CONTEXTS + Rails.application.config.woo.view_contexts
end
end
end
18 changes: 1 addition & 17 deletions app/helpers/woo/styleguide_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Woo
module StyleguideHelper
include RenderHelper

BASE_PATH = "app/views/styleguide"

Expand All @@ -15,23 +16,6 @@ def current_page(folder, page)
page_hash(filepath)
end

def render_haml_string(contents)
context = ActionView::Base.new(controller.view_context, {}, controller)
context.singleton_class.send(:include, Rails.application.routes.url_helpers)

context.define_singleton_method(:method_missing) do |name, *args, &block|
if respond_to?(name, true)
send(name, *args, &block)
elsif controller.respond_to?(name, true)
controller.send(name, *args, &block)
else
super(name, *args, &block)
end
end

Haml::Engine.new(contents).render(context)
end

def load_notes(filepath)
haml_frontmatter = /^-{1,3}$\n(?<notes_contents>.*)^-{1,3}$\n/m
match = haml_frontmatter.match(File.read(filepath))
Expand Down
1 change: 1 addition & 0 deletions lib/woo/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Engine < ::Rails::Engine
config.woo = ActiveSupport::OrderedOptions.new
config.woo.stylesheets = %w(woo/application application)
config.woo.javascripts = %w(woo/application application)
config.woo.view_contexts = []

initializer 'precompile custom assets' do |app|
app.config.assets.precompile += app.config.woo.stylesheets
Expand Down

0 comments on commit a97093c

Please sign in to comment.