Permalink
Browse files

support ability to change template via the resource file

  • Loading branch information...
bdimcheff committed Jan 23, 2009
1 parent 745a440 commit 7f59dade94d7fdced9061b8a799412b1c661ad67
Showing with 41 additions and 10 deletions.
  1. +9 −4 glug.rb
  2. +32 −6 spec/glug_spec.rb
View
13 glug.rb
@@ -6,6 +6,7 @@
require 'rdiscount'
require 'sass'
require 'metaid'
+require 'extlib'
#require 'grit'
class Hash
@@ -102,18 +103,22 @@ def locate(*args)
end
attr_accessor :attributes
- page_attr_accessor :title, :author, :created_at, :updated_at, :category, :tags
+ page_attr_accessor :title, :author, :created_at, :updated_at, :category, :tags, :template
def initialize(raw_content = '')
super(raw_content)
self.attributes = {}
-
transform(raw_content)
end
def updated_at
attributes[:updated_at] || created_at
end
+
+ def template
+ attributes[:template].to_sym rescue nil ||
+ self.class.to_s.split('::').last.snake_case.to_sym
+ end
# Read the YAML frontmatter
# +base+ is the String path to the dir containing the file
@@ -224,13 +229,13 @@ def initialize
puts "repo in action: #{Resource.repo}"
@page = Page.find(params[:page])
- haml :page
+ haml @page.template
end
get %r!^/(\d{4})/(\d{2})/(\d{2})/(.*)$! do
@page = Post.find(*params[:captures])
- haml :post
+ haml @page.template
end
get '/styles/:style.css' do
View
@@ -130,10 +130,6 @@
end
end
- describe '#created_at' do
-
- end
-
describe '#updated_at' do
it 'should default to created_at if not present' do
p = create_page
@@ -143,6 +139,32 @@
p.updated_at.should == t
end
end
+
+ describe '#template' do
+ it 'defaults to the class name' do
+ p = create_page
+ p.template.should == :page
+ end
+
+ it 'defaults to the class name for subclasses of Page' do
+ p = create_post
+ p.template.should == :post
+ end
+
+ it 'underscores camel-cased class names' do
+ FooBar = Class.new(Glug::Page)
+ foobar = FooBar.new
+
+ foobar.template.should == :foo_bar
+ end
+
+ it 'converts templates to symbols' do
+ p = create_page
+ p.template = 'foo'
+
+ p.template.should == :foo
+ end
+ end
end
describe 'Glug::Post' do
@@ -155,12 +177,16 @@
end
end
-def create_page(options = {})
- p = Glug::Page.new
+def create_page(content = '', options = {})
+ p = Glug::Page.new(content)
p
end
+def create_post(content = '', options = {})
+ Glug::Post.new(content)
+end
+
def temp(&block)
temp = File.expand_path(File.join(File.dirname(__FILE__), 'tmp'))
FileUtils.rm_r(temp) if File.exist? temp

0 comments on commit 7f59dad

Please sign in to comment.