Skip to content

Commit

Permalink
set dependencies for sprockets to invalidate caches; fixes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenburger committed Feb 1, 2014
1 parent d919bdd commit 07f4956
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/livingstyleguide/engine.rb
@@ -1,13 +1,14 @@
module LivingStyleGuide

class Engine
attr_accessor :markdown, :options
attr_accessor :markdown, :files, :options

def initialize(source, options, sass_options)
@source = source
@options = options
@sass_options = sass_options
@markdown = ''
@files = []
end

def render
Expand Down
15 changes: 10 additions & 5 deletions lib/livingstyleguide/importer.rb
Expand Up @@ -6,26 +6,31 @@ def initialize(root)

def find_relative(name, base, options, absolute = false)
@options = options
find_markdown(File.join(File.dirname(base), name))
super(name, base, options)
engine = super(name, base, options)
find_markdown(options[:filename])
engine
end

def find(name, options)
@options = options
find_markdown(name)
super(name, options)
end

private
def find_markdown(sass_filename)
files << sass_filename
glob = "#{sass_filename.sub(/\.s[ac]ss$/, '')}.md"
glob.sub!(/(.*)\//, '\\1/{_,}') unless glob =~ /\/_/
glob = '{_,}' + glob unless glob =~ /\//
Dir.glob(glob) do |markdown_filename|
files << markdown_filename
markdown << File.read(markdown_filename)
end
end

private
def files
@options[:living_style_guide].files
end

private
def markdown
@options[:living_style_guide].markdown
Expand Down
36 changes: 30 additions & 6 deletions lib/livingstyleguide/tilt_template.rb
Expand Up @@ -11,6 +11,7 @@ def prepare
end

def evaluate(scope, locals, &block)
@scope = scope
parse_options(data)
generate_sass
render_living_style_guide
Expand All @@ -25,10 +26,11 @@ def sass_options
options[:template_location].each do |path, short|
options[:load_paths] << ::LivingStyleGuide::Importer.new(path)
end
options[:filename] = eval_file
options[:line] = line
options[:syntax] = @options[:syntax]
options[:importer] = LivingStyleGuide::Importer.new('.')
options[:filename] = eval_file
options[:line] = line
options[:syntax] = @options[:syntax]
options[:importer] = LivingStyleGuide::Importer.new('.')
options[:sprockets] = { context: @scope }
options
end

Expand All @@ -51,6 +53,26 @@ def generate_sass
].flatten.join(@options[:syntax] == :sass ? "\n" : ';')
end

private
def configure_cache
return unless @scope.respond_to?(:depend_on)
test = /^#{root}/
@engine.files.uniq.each do |file|
if file =~ test
@scope.depend_on file
end
end
end

private
def root
if @scope.respond_to?(:environment)
@scope.environment.root
else
File.dirname(@file)
end
end

private
def style_variables
return unless @options.has_key?(:style)
Expand All @@ -61,8 +83,10 @@ def style_variables

private
def render_living_style_guide
engine = ::LivingStyleGuide::Engine.new(@sass, @options, sass_options)
engine.render
@engine = ::LivingStyleGuide::Engine.new(@sass, @options, sass_options)
html = @engine.render
configure_cache
html
end
end

Expand Down
19 changes: 19 additions & 0 deletions test/integration/sprockets_test.rb
@@ -0,0 +1,19 @@
require 'test_helper'
require 'tilt'

describe "Sprockets integration" do

describe "sprockets should know when to invalidate cache" do
template = Tilt.new('test/fixtures/standalone/styleguide.html.lsg')
context = Minitest::Mock.new
%w(style.scss modules/_buttons.scss modules/_buttons.md styleguide.html.lsg).each do |file|
context.expect :depend_on, nil, ["test/fixtures/standalone/#{file}"]
end

template.render context

context.verify
end

end

0 comments on commit 07f4956

Please sign in to comment.