summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/content_controller.rb10
-rw-r--r--app/helpers/content_helper.rb4
-rw-r--r--app/helpers/nodes_helper.rb4
-rw-r--r--app/models/page.rb51
-rw-r--r--app/views/custom/page_templates/public/no_date_and_author.html.erb6
-rw-r--r--app/views/custom/page_templates/public/render_page.html.erb (renamed from app/views/content/render_page.html.erb)0
-rw-r--r--app/views/custom/partials/_sidebar_title_only.html.erb (renamed from app/views/custom_templates/partials/_sidebar_title_only.html.erb)0
-rw-r--r--app/views/nodes/edit.html.erb5
-rw-r--r--db/migrate/20090302184350_add_template_column_to_pages.rb9
-rw-r--r--db/migrate/20090302215335_rename_template_to_template_name.rb9
-rw-r--r--test/functional/content_controller_test.rb22
-rw-r--r--test/functional/nodes_controller_test.rb20
12 files changed, 134 insertions, 6 deletions
diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb
index 0fa9832..706cfcd 100644
--- a/app/controllers/content_controller.rb
+++ b/app/controllers/content_controller.rb
@@ -10,8 +10,14 @@ class ContentController < ApplicationController
10 10
11 @page = Node.find_page(path) 11 @page = Node.find_page(path)
12 12
13 # Replace with real 404 13 if @page
14 unless @page 14 template = @page.valid_template
15
16 render(
17 :file => template,
18 :layout => true
19 )
20 else
15 render( 21 render(
16 :file => File.join(RAILS_ROOT, 'public', '404.html'), 22 :file => File.join(RAILS_ROOT, 'public', '404.html'),
17 :status => 404 23 :status => 404
diff --git a/app/helpers/content_helper.rb b/app/helpers/content_helper.rb
index fba7737..a586142 100644
--- a/app/helpers/content_helper.rb
+++ b/app/helpers/content_helper.rb
@@ -73,7 +73,7 @@ module ContentHelper
73 # partial 73 # partial
74 def select_partial partial 74 def select_partial partial
75 if partial && partial_exists?( partial ) 75 if partial && partial_exists?( partial )
76 return "custom_templates/partials/#{partial}" 76 return "custom/partials/#{partial}"
77 else 77 else
78 return 'content/article' 78 return 'content/article'
79 end 79 end
@@ -83,7 +83,7 @@ module ContentHelper
83 def partial_exists? partial 83 def partial_exists? partial
84 File.exist?( 84 File.exist?(
85 File.join( 85 File.join(
86 RAILS_ROOT, 'app', 'views', 'custom_templates', 'partials', "_#{partial}.html.erb" 86 RAILS_ROOT, 'app', 'views', 'custom', 'partials', "_#{partial}.html.erb"
87 ) 87 )
88 ) 88 )
89 end 89 end
diff --git a/app/helpers/nodes_helper.rb b/app/helpers/nodes_helper.rb
index fc5b0a0..bd54d3d 100644
--- a/app/helpers/nodes_helper.rb
+++ b/app/helpers/nodes_helper.rb
@@ -7,4 +7,8 @@ module NodesHelper
7 truncate(node.draft.title, :length => 50) 7 truncate(node.draft.title, :length => 50)
8 end 8 end
9 end 9 end
10
11 def custom_page_templates
12 Page.custom_templates.map {|x| [x.gsub("_", " ").titlecase, x]}
13 end
10end 14end
diff --git a/app/models/page.rb b/app/models/page.rb
index 7ac8f35..7ac0b60 100644
--- a/app/models/page.rb
+++ b/app/models/page.rb
@@ -1,5 +1,8 @@
1class Page < ActiveRecord::Base 1class Page < ActiveRecord::Base
2 2
3 PUBLIC_TEMPLATE_PATH = File.join(%w(custom page_templates public))
4 FULL_PUBLIC_TEMPLATE_PATH = File.join(RAILS_ROOT, 'app', 'views', PUBLIC_TEMPLATE_PATH)
5
3 # Mixins and Plugins 6 # Mixins and Plugins
4 acts_as_taggable 7 acts_as_taggable
5 acts_as_list :column => :revision, :scope => :node_id 8 acts_as_list :column => :revision, :scope => :node_id
@@ -11,7 +14,7 @@ class Page < ActiveRecord::Base
11 belongs_to :user 14 belongs_to :user
12 15
13 # Security 16 # Security
14 attr_accessible :title, :abstract, :body 17 attr_accessible :title, :abstract, :body, :template_name
15 18
16 # Class Methods 19 # Class Methods
17 20
@@ -44,6 +47,12 @@ class Page < ActiveRecord::Base
44 47
45 end 48 end
46 49
50 def self.custom_templates
51 files = Dir.entries(FULL_PUBLIC_TEMPLATE_PATH).select do |x|
52 x if x.gsub!(".html.erb", "")
53 end
54 end
55
47 # Instance Methods 56 # Instance Methods
48 57
49 def clone_attributes_from page 58 def clone_attributes_from page
@@ -63,4 +72,44 @@ class Page < ActiveRecord::Base
63 72
64 I18n.locale = locale_before 73 I18n.locale = locale_before
65 end 74 end
75
76 def public_template_path
77 File.join(PUBLIC_TEMPLATE_PATH, template_name)
78 end
79
80 def full_public_template_path
81 File.join(FULL_PUBLIC_TEMPLATE_PATH, template_name)
82 end
83
84 def template_exists?
85 File.exists? "#{full_public_template_path}.html.erb"
86 end
87
88 def valid_template
89
90 if template_name && template_exists?
91 public_template_path
92 else
93 File.join(PUBLIC_TEMPLATE_PATH, 'render_page')
94 end
95 end
96
97 def clone_attributes_from page
98 return nil unless page
99
100 self.tag_list = page.tag_list.join(", ")
101 self.template_name = page.template_name
102
103 locale_before = I18n.locale
104
105 I18n.available_locales.each do |l|
106 next if l == :root
107 I18n.locale = l
108 self.title = page.title
109 self.abstract = page.abstract
110 self.body = page.body
111 end
112
113 I18n.locale = locale_before
114 end
66end \ No newline at end of file 115end \ No newline at end of file
diff --git a/app/views/custom/page_templates/public/no_date_and_author.html.erb b/app/views/custom/page_templates/public/no_date_and_author.html.erb
new file mode 100644
index 0000000..4eda744
--- /dev/null
+++ b/app/views/custom/page_templates/public/no_date_and_author.html.erb
@@ -0,0 +1,6 @@
1<div class="article">
2 <h2><%= @page.title %></h2>
3 <hr class="subtitle" />
4 <p><em><%= @page.abstract %></em></p>
5 <%= aggregate?(@page.body) %>
6</div> \ No newline at end of file
diff --git a/app/views/content/render_page.html.erb b/app/views/custom/page_templates/public/render_page.html.erb
index 4c21667..4c21667 100644
--- a/app/views/content/render_page.html.erb
+++ b/app/views/custom/page_templates/public/render_page.html.erb
diff --git a/app/views/custom_templates/partials/_sidebar_title_only.html.erb b/app/views/custom/partials/_sidebar_title_only.html.erb
index 819d9ae..819d9ae 100644
--- a/app/views/custom_templates/partials/_sidebar_title_only.html.erb
+++ b/app/views/custom/partials/_sidebar_title_only.html.erb
diff --git a/app/views/nodes/edit.html.erb b/app/views/nodes/edit.html.erb
index 64d4756..929bbf6 100644
--- a/app/views/nodes/edit.html.erb
+++ b/app/views/nodes/edit.html.erb
@@ -13,7 +13,10 @@
13 <%= f.error_messages %> 13 <%= f.error_messages %>
14 14
15 <% fields_for @draft do |d| %> 15 <% fields_for @draft do |d| %>
16 <p></p> 16 <p>
17 <%= d.label :template_name %>
18 <%= d.select :template_name, custom_page_templates %>
19 </p>
17 <p> 20 <p>
18 <%= d.label :title %><br /> 21 <%= d.label :title %><br />
19 <%= d.text_field :title %> 22 <%= d.text_field :title %>
diff --git a/db/migrate/20090302184350_add_template_column_to_pages.rb b/db/migrate/20090302184350_add_template_column_to_pages.rb
new file mode 100644
index 0000000..e3aac66
--- /dev/null
+++ b/db/migrate/20090302184350_add_template_column_to_pages.rb
@@ -0,0 +1,9 @@
1class AddTemplateColumnToPages < ActiveRecord::Migration
2 def self.up
3 add_column :pages, :template, :string
4 end
5
6 def self.down
7 remove_column :pages, :template
8 end
9end
diff --git a/db/migrate/20090302215335_rename_template_to_template_name.rb b/db/migrate/20090302215335_rename_template_to_template_name.rb
new file mode 100644
index 0000000..288209e
--- /dev/null
+++ b/db/migrate/20090302215335_rename_template_to_template_name.rb
@@ -0,0 +1,9 @@
1class RenameTemplateToTemplateName < ActiveRecord::Migration
2 def self.up
3 rename_column :pages, :template, :template_name
4 end
5
6 def self.down
7 rename_column :pages, :template_name, :template
8 end
9end
diff --git a/test/functional/content_controller_test.rb b/test/functional/content_controller_test.rb
index c374a83..2f390c6 100644
--- a/test/functional/content_controller_test.rb
+++ b/test/functional/content_controller_test.rb
@@ -67,7 +67,29 @@ class ContentControllerTest < ActionController::TestCase
67 assert_select(".sidebar_headline", "two") 67 assert_select(".sidebar_headline", "two")
68 end 68 end
69 69
70 def test_nonexistant_custom_template_defaults_to_standard_template
71 new_node = create_node_under_root "fnord"
72 draft = new_node.find_or_create_draft @user1
73 draft.template_name = "huchibu"
74 draft.save
75 new_node.publish_draft!
76
77 get :render_page, :locale => 'de', :page_path => ["fnord"]
78 assert_response :success
79 assert_template "custom/page_templates/public/render_page.html.erb"
80 end
70 81
82 def test_custom_template_no_date_and_author
83 new_node = create_node_under_root "fnord"
84 draft = new_node.find_or_create_draft @user1
85 draft.template_name = "no_date_and_author"
86 draft.save
87 new_node.publish_draft!
88
89 get :render_page, :locale => 'de', :page_path => ["fnord"]
90 assert_response :success
91 assert_template "custom/page_templates/public/no_date_and_author.html.erb"
92 end
71 93
72 protected 94 protected
73 95
diff --git a/test/functional/nodes_controller_test.rb b/test/functional/nodes_controller_test.rb
index 20090d0..fdeb6cb 100644
--- a/test/functional/nodes_controller_test.rb
+++ b/test/functional/nodes_controller_test.rb
@@ -56,4 +56,24 @@ class NodesControllerTest < ActionController::TestCase
56 assert_equal "Hello", test_node.draft.title 56 assert_equal "Hello", test_node.draft.title
57 assert_equal "There", test_node.draft.body 57 assert_equal "There", test_node.draft.body
58 end 58 end
59
60 def test_update_a_draft_with_changing_the_template
61 test_node = Node.create! :slug => "test_node"
62 test_node.move_to_child_of Node.root
63
64 login_as :quentin
65 put :update, {
66 :id => test_node.id,
67 :page => {
68 :title => "Hello",
69 :body => "There",
70 :template_name => "Foobar"
71 }
72 }
73
74 test_node.reload
75 assert_equal "Hello", test_node.draft.title
76 assert_equal "There", test_node.draft.body
77 assert_equal "Foobar", test_node.draft.template_name
78 end
59end 79end