module Spinebox
class RenderError < StandardError; end
module ERBContext
# Includes javascript tags in the erb. If concatenation is disabled it returns multiple tags,
# otherwise it returns a single application tag.
def javascript_include_tag source
if Spinebox.config.concatenate
javascript_tag_for(asset_for(source, 'js'))
else
asset_for(source, 'js').to_a.map{ |asset| javascript_tag_for asset, :body => 1 }.join("\n")
end
end
# Includes javascript tags in the erb. If concatenation is disabled it returns multiple tags,
# otherwise it returns a single application tag.
def stylesheet_link_tag source, options = {}
if Spinebox.config.concatenate
stylesheet_tag_for(asset_for(source, 'css'), options)
else
asset_for(source, 'css').to_a.map{ |asset| stylesheet_tag_for asset, options.merge!({ :body => 1 }) }.join("\n")
end
end
# Renders, e.g. a partial into another view
def render params = {}
raise RenderError, "Render without :partial called" if params[:partial].nil?
raise RenderError, "Partial needs a file extension" if params[:partial].split(".").size == 0
partial = Spinebox.views["_#{params[:partial]}"]
raise RenderError, "Partial not found" if partial.nil?
partial.to_s
end
private
# Access an asset from the assets
def asset_for source, extension
Spinebox.assets[source.include?(".#{extension}") ? source : "#{source}.#{extension}"]
end
# Build a javascript tag with body options
def javascript_tag_for asset, options = {}
""
end
# Build a stylesheet link with body options
def stylesheet_tag_for asset, new_options = {}
options = { :media => "all", :rel => "stylesheet" }.merge!(new_options)
""
end
end
class << self
# Load helpers and include into sprockets context
def load_helpers!
Spinebox::ERBContext.class_eval do
Dir["app/helpers/*"].each do |helper|
require("./#{helper}")
include Spinebox.const_get(File.basename(helper, ".rb").classify)
end
end
::Sprockets::Context.instance_eval do
include Spinebox::ERBContext
end
end
end
end