Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Add layout to digests #64

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
bin
test/tmp
test/tmp
51 changes: 18 additions & 33 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,51 +1,36 @@
PATH
remote: .
specs:
cache_digests (0.3.0)
cache_digests (0.3.1)
actionpack (>= 3.2)
thread_safe

GEM
remote: https://rubygems.org/
specs:
actionpack (3.2.13)
activemodel (= 3.2.13)
activesupport (= 3.2.13)
builder (~> 3.0.0)
actionpack (4.0.12)
activesupport (= 4.0.12)
builder (~> 3.1.0)
erubis (~> 2.7.0)
journey (~> 1.0.4)
rack (~> 1.4.5)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.2.1)
activemodel (3.2.13)
activesupport (= 3.2.13)
builder (~> 3.0.0)
activesupport (3.2.13)
i18n (= 0.6.1)
multi_json (~> 1.0)
atomic (1.1.8)
builder (3.0.4)
rack (~> 1.5.2)
rack-test (~> 0.6.2)
activesupport (4.0.12)
i18n (~> 0.6, >= 0.6.9)
minitest (~> 4.2)
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
builder (3.1.4)
erubis (2.7.0)
hike (1.2.2)
i18n (0.6.1)
journey (1.0.4)
i18n (0.7.0)
minitest (4.7.3)
multi_json (1.7.2)
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
multi_json (1.10.1)
rack (1.5.2)
rack-test (0.6.2)
rack (>= 1.0)
rake (10.0.4)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
thread_safe (0.1.0)
atomic
tilt (1.3.7)
thread_safe (0.3.4)
tzinfo (0.3.42)

PLATFORMS
ruby
Expand Down
2 changes: 1 addition & 1 deletion lib/cache_digests/dependency_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ERBTracker
RENDER_DEPENDENCY = /
render\s* # render, followed by optional whitespace
\(? # start an optional parenthesis for the render call
(partial:|:partial\s+=>)?\s* # naming the partial, used with collection -- 1st capture
(partial:|:partial\s+=>|layout:|:layout\s+=>)?\s* # naming the partial, used with collection -- 1st capture
([@a-z"'][@\w\/\."']+) # the template name itself -- 2nd capture
/x

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/layouts/_shared.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="layout-container">
<%=yield %>
</div>
3 changes: 3 additions & 0 deletions test/fixtures/layouts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= render layout: "layouts/shared" do %>
Hello world
<% end %>
44 changes: 31 additions & 13 deletions test/template_digestor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MissingTemplate < StandardError

class FixtureTemplate
attr_reader :source, :handler

def initialize(template_path, handler = :erb)
@source = File.read(template_path)
@handler = handler
Expand All @@ -20,7 +20,7 @@ def initialize(template_path, handler = :erb)
class FixtureFinder
FIXTURES_DIR = "#{File.dirname(__FILE__)}/fixtures"
TMP_DIR = "#{File.dirname(__FILE__)}/tmp"

def find(logical_name, keys, partial, options)
FixtureTemplate.new("#{TMP_DIR}/#{partial ? logical_name.gsub(%r|/([^/]+)$|, '/_\1') : logical_name}.#{options[:formats].first}.erb")
end
Expand All @@ -31,7 +31,7 @@ def setup
FileUtils.cp_r FixtureFinder::FIXTURES_DIR, FixtureFinder::TMP_DIR
CacheDigests::DependencyTracker.register_tracker :erb, CacheDigests::DependencyTracker::ERBTracker
end

def teardown
FileUtils.rm_r FixtureFinder::TMP_DIR
CacheDigests::TemplateDigestor.cache.clear
Expand Down Expand Up @@ -88,7 +88,13 @@ def test_third_level_dependency
change_template("comments/_comment")
end
end


def test_layout_dependency
assert_digest_difference("layouts/index") do
change_template("layouts/_shared")
end
end

def test_directory_depth_dependency
assert_digest_difference("level/below/index") do
change_template("level/below/_header")
Expand All @@ -112,11 +118,11 @@ def test_nested_template_directory
change_template("messages/actions/_move")
end
end

def test_recursion_in_renders
assert digest("level/recursion")
end

def test_dont_generate_a_digest_for_missing_templates
assert_equal '', digest("nothing/there")
end
Expand All @@ -130,24 +136,29 @@ def test_collection_dependency
change_template("events/_event")
end
end

def test_collection_derived_from_record_dependency
assert_digest_difference("messages/show") do
change_template("events/_event")
end
end

def test_layout_source_changed
assert_digest_difference("layouts/index") do
change_string_in_template("layouts/index", "<% end %>", "hi!\n<% end %>")
end
end

private
def assert_logged(message)
log = StringIO.new
CacheDigests::TemplateDigestor.logger = Logger.new(log)

yield

log.rewind
assert_match message, log.read

CacheDigests::TemplateDigestor.logger = nil
end

Expand All @@ -160,14 +171,21 @@ def assert_digest_difference(template_name)
assert previous_digest != digest(template_name), "digest didn't change"
CacheDigests::TemplateDigestor.cache.clear
end

def digest(template_name, options={})
CacheDigests::TemplateDigestor.digest(template_name, :html, FixtureFinder.new, options)
end
def change_template(template_name)
File.open("#{FixtureFinder::TMP_DIR}/#{template_name}.html.erb", "w") do |f|

def change_template(template_name, ext = 'erb')
File.open("#{FixtureFinder::TMP_DIR}/#{template_name}.html.#{ext}", "w") do |f|
f.write "\nTHIS WAS CHANGED!"
end
end

def change_string_in_template(template_name, original_string, replace_with, ext = 'erb')
file_name = "#{FixtureFinder::TMP_DIR}/#{template_name}.html.#{ext}"
text = File.read(file_name)
new_contents = text.gsub(original_string, replace_with)
File.open(file_name, "w") {|file| file.puts new_contents }
end
end