Skip to content
Browse files

use AbstractController::Layouts

  • Loading branch information...
1 parent 94db268 commit a83e90b703c5e1c246ea7214b03c3066eba73045 @svenfuchs committed Mar 18, 2010
Showing with 134 additions and 59 deletions.
  1. +1 −1 Gemfile
  2. +24 −7 lib/slick/builder/base.rb
  3. +1 −2 lib/slick/builder/section.rb
  4. +3 −3 lib/slick/model/content.rb
  5. 0 public/{2010/3/15 → 2009/07/05}/ripper2_ruby__modify_and_recompile_your_ruby_code.html
  6. 0 public/{2010/3/15 → 2009/07/06}/using_ruby_1_9_ripper.html
  7. +2 −1 public/contact.html
  8. +1 −1 public/index.html
  9. 0 .../{fixtures/home → _root/data}/2009-07-05-ripper2ruby-modify-and-recompile-your-ruby-code.markdown
  10. 0 ...2009-07-05-using-ruby-1-9-ripper.markdown → _root/data/2009-07-06-using-ruby-1-9-ripper.markdown}
  11. +1 −1 test/{fixtures/home → _root/data}/articles.html
  12. 0 test/{fixtures/home → _root/data}/articles/_articles/article_1.html
  13. 0 test/{fixtures/home → _root/data}/articles/child.html
  14. +1 −1 test/{fixtures/home → _root/data}/contact.html
  15. 0 test/{fixtures/home → _root/data}/projects/i18.html
  16. +4 −0 test/_root/public/2009/07/05/ripper2_ruby__modify_and_recompile_your_ruby_code.html
  17. +4 −0 test/_root/public/2009/07/06/using_ruby_1_9_ripper.html
  18. +4 −0 test/_root/public/articles.html
  19. +4 −0 test/_root/public/articles/articles_child.html
  20. +5 −0 test/_root/public/contact.html
  21. +3 −0 test/_root/public/index.html
  22. +4 −0 test/_root/public/projects.html
  23. +4 −0 test/_root/public/projects/i18n.html
  24. +4 −0 test/_root/views/layouts/custom.html.erb
  25. +24 −9 test/builder_test.rb
  26. +4 −4 test/model/base_test.rb
  27. +1 −1 test/model/content_test.rb
  28. +12 −12 test/model/section_test.rb
  29. 0 test/model_test.rb
  30. +13 −6 test/test_helper.rb
  31. +10 −10 test/view_test.rb
View
2 Gemfile
@@ -1,5 +1,5 @@
source 'http://gemcutter.org'
gem "rails", :git => "git://github.com/rails/rails.git"
-# gem "rails", '>=3.0.0beta1'
+# gem "rails", '>=3.0.0beta1'
View
31 lib/slick/builder/base.rb
@@ -14,28 +14,45 @@ def controller_path
self.view_paths = [File.expand_path('../../views', __FILE__)]
self.layout 'application'
+ attr_reader :config
+
+ def initialize(data, parent = nil)
+ @parent = parent
+ @data = data
+ @config = data[:config] || {}
+ end
+
protected
- def document_root
- @@document_root ||= File.expand_path('../../../../public', __FILE__)
+ def root_dir
+ @config[:root] ||= File.expand_path('../../../..', __FILE__)
+ end
+
+ def public_dir
+ "#{root_dir}/public"
+ end
+
+ def view_paths
+ p "KEKSE"
end
def render(template, target)
- write(target, super(:template => template, :layout => layout, :locals => data))
+ html = super(:template => template, :layout => layout, :locals => data)
+ write(target, html) && html
end
def write(file_name, html)
- path = [document_root, path, file_name].compact.join('/')
+ path = [public_dir, path, file_name].compact.join('/')
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w+') { |f| f.write(html) }
end
public
# TODO can this be made smarter in AbstractController::Layouts? doesn't even seem to be used anywhere?
- def config
- {}
- end
+ # def config
+ # {}
+ # end
# TODO can this be made smarter in AbstractController::Layouts? just pass it through for now
def response_body=(body)
View
3 lib/slick/builder/section.rb
@@ -3,8 +3,7 @@ class Section < Base
attr_reader :data, :site, :section, :parent
def initialize(data, parent = nil)
- @parent = parent
- @data = data
+ super
@site, @section = data.values_at(:site, :section)
end
View
6 lib/slick/model/content.rb
@@ -7,16 +7,16 @@ class Content < Base
PUBLISHED_AT_PATTERN = %r((?:^|\/)([\d]{4}[-_][\d]{2}[-_][\d]{2}))
def permalink # TODO
- date = published_at
+ date = published_at.to_s.gsub('-', '/')
slug = title.underscore.gsub(/[\W]/, '_')
- [date.year, date.month, date.day, slug].join('/')
+ [date, slug].join('/')
end
protected
def read
attributes = super
- attributes.merge(:published_at => published_at_from_filename) unless attributes.key?(:published_at)
+ attributes.merge!(:published_at => published_at_from_filename) unless attributes.key?(:published_at)
attributes
end
View
0 ..._modify_and_recompile_your_ruby_code.html → ..._modify_and_recompile_your_ruby_code.html
File renamed without changes.
View
0 public/2010/3/15/using_ruby_1_9_ripper.html → public/2009/07/06/using_ruby_1_9_ripper.html
File renamed without changes.
View
3 public/contact.html
@@ -1,4 +1,5 @@
-page layout:
+custom layout:
Page: Contact
+
View
2 public/index.html
@@ -1,3 +1,3 @@
application layout:
-Home
+Data
View
0 ...ify-and-recompile-your-ruby-code.markdown → ...ify-and-recompile-your-ruby-code.markdown
File renamed without changes.
View
0 ...2009-07-05-using-ruby-1-9-ripper.markdown → ...2009-07-06-using-ruby-1-9-ripper.markdown
File renamed without changes.
View
2 test/fixtures/home/articles.html → test/_root/data/articles.html
@@ -1,5 +1,5 @@
---
-title: "Articles"
+title: Articles
layout: page
---
Articles Content
View
0 ...es/home/articles/_articles/article_1.html → ...ot/data/articles/_articles/article_1.html
File renamed without changes.
View
0 test/fixtures/home/articles/child.html → test/_root/data/articles/child.html
File renamed without changes.
View
2 test/fixtures/home/contact.html → test/_root/data/contact.html
@@ -1,5 +1,5 @@
---
-layout: page
+layout: custom
title: Contact
---
http://github.com/svenfuchs
View
0 test/fixtures/home/projects/i18.html → test/_root/data/projects/i18.html
File renamed without changes.
View
4 test/_root/public/2009/07/05/ripper2_ruby__modify_and_recompile_your_ruby_code.html
@@ -0,0 +1,4 @@
+application layout:
+Post: Ripper2Ruby: modify and recompile your Ruby code
+
+
View
4 test/_root/public/2009/07/06/using_ruby_1_9_ripper.html
@@ -0,0 +1,4 @@
+application layout:
+Post: Using Ruby 1.9 Ripper
+
+
View
4 test/_root/public/articles.html
@@ -0,0 +1,4 @@
+page layout:
+Page: Articles
+
+
View
4 test/_root/public/articles/articles_child.html
@@ -0,0 +1,4 @@
+page layout:
+Page: Articles Child
+
+
View
5 test/_root/public/contact.html
@@ -0,0 +1,5 @@
+custom layout:
+Page: Contact
+
+
+
View
3 test/_root/public/index.html
@@ -0,0 +1,3 @@
+application layout:
+Data
+
View
4 test/_root/public/projects.html
@@ -0,0 +1,4 @@
+page layout:
+Page: Projects
+
+
View
4 test/_root/public/projects/i18n.html
@@ -0,0 +1,4 @@
+page layout:
+Page: I18n
+
+
View
4 test/_root/views/layouts/custom.html.erb
@@ -0,0 +1,4 @@
+custom layout:
+<%= yield %>
+
+
View
33 test/builder_test.rb
@@ -3,12 +3,12 @@
class BuilderTest < Test::Unit::TestCase
def setup
- @section = Slick::Model::Section.new(HOME_DIR)
- FileUtils.mkdir_p(PUBLIC_DIR)
- end
-
- def teardown
+ @config = {
+ :root => TEST_ROOT
+ }
+ @section = Slick::Model::Section.new(DATA_DIR)
# FileUtils.rm_r(PUBLIC_DIR) rescue Errno::ENOENT
+ FileUtils.mkdir_p(PUBLIC_DIR)
end
test 'path for a root section is index' do
@@ -35,15 +35,30 @@ def teardown
assert_equal 'projects', nested.send(:path)
end
- test 'foo' do
- root = Slick::Builder::Blog.new(:section => @section)
- root.build
+ test 'build' do
+ Slick::Builder::Blog.new(:config => @config, :section => @section).build
+
assert File.exists?(PUBLIC_DIR + '/index.html')
+ assert File.exists?(PUBLIC_DIR + '/2009/07/06/using_ruby_1_9_ripper.html')
assert File.exists?(PUBLIC_DIR + '/articles.html')
assert File.exists?(PUBLIC_DIR + '/articles/articles_child.html')
- # assert File.exists?(PUBLIC_DIR + '/articles/article_1.html')
assert File.exists?(PUBLIC_DIR + '/projects.html')
assert File.exists?(PUBLIC_DIR + '/projects/i18n.html')
assert File.exists?(PUBLIC_DIR + '/contact.html')
end
+
+ test 'build w/ the application (default) layout' do
+ @section.attributes['layout'] = 'does_not_exist'
+ assert_match /application layout/, Slick::Builder.create(nil, @section, :section => @section).send(:build_index)
+ end
+
+ test 'build w/ a builder specific layout' do
+ section = @section.children.detect { |child| child.layout == nil }
+ assert_match /page layout/, Slick::Builder.create(nil, section, :section => section).send(:build_index)
+ end
+
+ test 'build w/ a custom layout' do
+ section = @section.children.detect { |child| child.layout == 'custom' }
+ assert_match /custom layout/, Slick::Builder.create(nil, section, :section => section).send(:build_index)
+ end
end
View
8 test/model/base_test.rb
@@ -6,23 +6,23 @@ def model(path)
end
test "slug is the underscored title (so it can be used in urls)" do
- model = self.model(HOME_DIR + '/articles_page_foo_bar')
+ model = self.model(DATA_DIR + '/articles_page_foo_bar')
model.attributes[:title] = 'Articles Page foo Bar'
assert_equal 'articles_page_foo_bar', model.slug
end
test "title_from_path is the titleized filename's basename" do
- model = self.model(HOME_DIR + '/articles_page_foo_bar')
+ model = self.model(DATA_DIR + '/articles_page_foo_bar')
assert_equal 'Articles Page Foo Bar', model.send(:title_from_path)
end
test "read adds the file's yaml preamble to the attributes" do
- model = self.model(HOME_DIR + '/articles.html')
+ model = self.model(DATA_DIR + '/articles.html')
assert_equal 'page', model.layout
end
test "read stores the file's main content to the content attribute" do
- model = self.model(HOME_DIR + '/articles.html')
+ model = self.model(DATA_DIR + '/articles.html')
assert_equal 'Articles Content', model.content
end
end
View
2 test/model/content_test.rb
@@ -5,7 +5,7 @@ def content
end
test "published_at_from_file_name parses a date from the file's basename" do
- content = Slick::Model::Content.new(HOME_DIR + '/_posts/2009-07-05-foo.html')
+ content = Slick::Model::Content.new(DATA_DIR + '/_posts/2009-07-05-foo.html')
date = content.send(:published_at_from_filename)
assert_equal '2009-07-05', date.to_s
end
View
24 test/model/section_test.rb
@@ -4,26 +4,26 @@ class ModelSectionTest < Test::Unit::TestCase
include Slick::Model
test 'determine_type returns Page for a page file' do
- assert_equal 'page', Section.new(HOME_DIR + '/contact.html').send(:determine_type)
+ assert_equal 'page', Section.new(DATA_DIR + '/contact.html').send(:determine_type)
end
test 'determine_type returns Page for a page directory' do
- assert_equal 'page', Section.new(HOME_DIR + '/projects').send(:determine_type)
+ assert_equal 'page', Section.new(DATA_DIR + '/projects').send(:determine_type)
end
test 'determine_type returns Blog for a blog directory' do
- assert_equal 'blog', Section.new(HOME_DIR).send(:determine_type)
+ assert_equal 'blog', Section.new(DATA_DIR).send(:determine_type)
end
test 'building a Page from a page file' do
- model = Section.new(HOME_DIR + '/contact.html')
+ model = Section.new(DATA_DIR + '/contact.html')
assert_equal 'page', model.type
- assert_equal 'page', model.layout
+ assert_equal 'custom', model.layout
assert_equal 'http://github.com/svenfuchs', model.content
end
test 'building a Page from a page directory' do
- model = Section.new(HOME_DIR + '/projects')
+ model = Section.new(DATA_DIR + '/projects')
assert_equal 'page', model.type
assert_equal 'Projects', model.title
@@ -32,7 +32,7 @@ class ModelSectionTest < Test::Unit::TestCase
end
test 'building a Page from a page directory and file' do
- model = Section.new(HOME_DIR + '/articles.html')
+ model = Section.new(DATA_DIR + '/articles.html')
assert_equal 'page', model.type
assert_equal 'Articles', model.title
@@ -41,7 +41,7 @@ class ModelSectionTest < Test::Unit::TestCase
end
test 'building a Blog from the home directory' do
- model = Section.new(HOME_DIR)
+ model = Section.new(DATA_DIR)
assert_equal 'blog', model.type
assert_equal 2, model.contents.size
@@ -53,9 +53,9 @@ class ModelSectionTest < Test::Unit::TestCase
end
test 'child_paths does not contain both a section dir and a section file but skips the section dir' do
- paths = Section.new(HOME_DIR).send(:child_paths)
- assert paths.include?(HOME_DIR + '/projects')
- assert paths.include?(HOME_DIR + '/articles.html')
- assert !paths.include?(HOME_DIR + '/articles')
+ paths = Section.new(DATA_DIR).send(:child_paths)
+ assert paths.include?(DATA_DIR + '/projects')
+ assert paths.include?(DATA_DIR + '/articles.html')
+ assert !paths.include?(DATA_DIR + '/articles')
end
end
View
0 test/model_test.rb
No changes.
View
19 test/test_helper.rb
@@ -1,13 +1,20 @@
$: << File.expand_path('../../lib', __FILE__)
-
-require 'bundler'
-Bundler.setup
+begin
+ require File.expand_path('../../.bundle/environment', __FILE__)
+rescue LoadError
+ require 'rubygems'
+ require 'bundler'
+ Bundler.setup
+end
require 'test/unit'
require 'pp'
require 'slick'
require File.expand_path('../test_declarative.rb', __FILE__)
-HOME_DIR = File.expand_path('../fixtures/home', __FILE__)
-VIEWS_DIR = File.expand_path('../../lib/slick/views', __FILE__)
-PUBLIC_DIR = File.expand_path('../../public', __FILE__) # TODO move to config
+TEST_ROOT = File.expand_path('../_root', __FILE__)
+DATA_DIR = TEST_ROOT + '/data'
+PUBLIC_DIR = TEST_ROOT + '/public'
+VIEWS_DIR = TEST_ROOT + '/views'
+
+Slick::Builder::Base.prepend_view_path(VIEWS_DIR)
View
20 test/view_test.rb
@@ -1,11 +1,11 @@
-require File.expand_path('../test_helper', __FILE__)
-
-class ViewTest < Test::Unit::TestCase
- test 'simple render' do
- section = Slick::Model::Section.new('path/to/foo')
- section.attributes = { :title => 'da blog' }
- html = ActionView::Base.new([VIEWS_DIR]).render(:file => 'blog/index', :locals => { :section => section })
- assert_match /#{section.title}/, html
- end
-end
+# require File.expand_path('../test_helper', __FILE__)
+#
+# class ViewTest < Test::Unit::TestCase
+# test 'simple render' do
+# section = Slick::Model::Section.new('path/to/foo')
+# section.attributes = { :title => 'da blog' }
+# html = ActionView::Base.new([VIEWS_DIR]).render(:file => 'blog/index', :locals => { :section => section })
+# assert_match /#{section.title}/, html
+# end
+# end

0 comments on commit a83e90b

Please sign in to comment.
Something went wrong with that request. Please try again.