Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render with context #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 1 addition & 4 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,10 +16,6 @@ def current_page(folder, page)
page_hash(filepath)
end

def render_haml_string(contents)
Haml::Engine.new(contents).render
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
2 changes: 1 addition & 1 deletion lib/woo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Woo
VERSION = "0.1.8"
VERSION = "0.1.9"
end
2 changes: 1 addition & 1 deletion woo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |s|
s.files = Dir['{app,config,lib}/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md']
s.test_files = Dir['spec/**/*']

s.add_dependency 'rails', '~> 4.1.6'
s.add_dependency 'rails', '~> 4.2'
s.add_dependency 'bourbon'
s.add_dependency 'decent_exposure'
s.add_dependency 'haml-rails'
Expand Down