From 82f16e71d49350c1ad4aa48773db5d0f45a55c6c Mon Sep 17 00:00:00 2001 From: hukl Date: Mon, 2 Mar 2009 20:29:25 +0100 Subject: introducing page templates - need to add edit capabilities renamed the custom template folder forgot one fix for the new custom template path page templates refined renamed page attribute template to template_name as i suspected it to be a reserved word. it still didn't work until i discovered that simon defined the accessible attributes! That costed me 40 minutes of lifetime. But he apologized ;) tests for public and custom page templates --- app/controllers/content_controller.rb | 10 ++++- app/helpers/content_helper.rb | 4 +- app/helpers/nodes_helper.rb | 4 ++ app/models/page.rb | 51 +++++++++++++++++++++- app/views/content/render_page.html.erb | 7 --- .../public/no_date_and_author.html.erb | 6 +++ .../page_templates/public/render_page.html.erb | 7 +++ .../custom/partials/_sidebar_title_only.html.erb | 3 ++ .../partials/_sidebar_title_only.html.erb | 3 -- app/views/nodes/edit.html.erb | 5 ++- .../20090302184350_add_template_column_to_pages.rb | 9 ++++ ...90302215335_rename_template_to_template_name.rb | 9 ++++ test/functional/content_controller_test.rb | 22 ++++++++++ test/functional/nodes_controller_test.rb | 20 +++++++++ 14 files changed, 144 insertions(+), 16 deletions(-) delete mode 100644 app/views/content/render_page.html.erb create mode 100644 app/views/custom/page_templates/public/no_date_and_author.html.erb create mode 100644 app/views/custom/page_templates/public/render_page.html.erb create mode 100644 app/views/custom/partials/_sidebar_title_only.html.erb delete mode 100644 app/views/custom_templates/partials/_sidebar_title_only.html.erb create mode 100644 db/migrate/20090302184350_add_template_column_to_pages.rb create mode 100644 db/migrate/20090302215335_rename_template_to_template_name.rb 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 @page = Node.find_page(path) - # Replace with real 404 - unless @page + if @page + template = @page.valid_template + + render( + :file => template, + :layout => true + ) + else render( :file => File.join(RAILS_ROOT, 'public', '404.html'), :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 # partial def select_partial partial if partial && partial_exists?( partial ) - return "custom_templates/partials/#{partial}" + return "custom/partials/#{partial}" else return 'content/article' end @@ -83,7 +83,7 @@ module ContentHelper def partial_exists? partial File.exist?( File.join( - RAILS_ROOT, 'app', 'views', 'custom_templates', 'partials', "_#{partial}.html.erb" + RAILS_ROOT, 'app', 'views', 'custom', 'partials', "_#{partial}.html.erb" ) ) 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 truncate(node.draft.title, :length => 50) end end + + def custom_page_templates + Page.custom_templates.map {|x| [x.gsub("_", " ").titlecase, x]} + end end 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 @@ class Page < ActiveRecord::Base + PUBLIC_TEMPLATE_PATH = File.join(%w(custom page_templates public)) + FULL_PUBLIC_TEMPLATE_PATH = File.join(RAILS_ROOT, 'app', 'views', PUBLIC_TEMPLATE_PATH) + # Mixins and Plugins acts_as_taggable acts_as_list :column => :revision, :scope => :node_id @@ -11,7 +14,7 @@ class Page < ActiveRecord::Base belongs_to :user # Security - attr_accessible :title, :abstract, :body + attr_accessible :title, :abstract, :body, :template_name # Class Methods @@ -44,6 +47,12 @@ class Page < ActiveRecord::Base end + def self.custom_templates + files = Dir.entries(FULL_PUBLIC_TEMPLATE_PATH).select do |x| + x if x.gsub!(".html.erb", "") + end + end + # Instance Methods def clone_attributes_from page @@ -63,4 +72,44 @@ class Page < ActiveRecord::Base I18n.locale = locale_before end + + def public_template_path + File.join(PUBLIC_TEMPLATE_PATH, template_name) + end + + def full_public_template_path + File.join(FULL_PUBLIC_TEMPLATE_PATH, template_name) + end + + def template_exists? + File.exists? "#{full_public_template_path}.html.erb" + end + + def valid_template + + if template_name && template_exists? + public_template_path + else + File.join(PUBLIC_TEMPLATE_PATH, 'render_page') + end + end + + def clone_attributes_from page + return nil unless page + + self.tag_list = page.tag_list.join(", ") + self.template_name = page.template_name + + locale_before = I18n.locale + + I18n.available_locales.each do |l| + next if l == :root + I18n.locale = l + self.title = page.title + self.abstract = page.abstract + self.body = page.body + end + + I18n.locale = locale_before + end end \ No newline at end of file diff --git a/app/views/content/render_page.html.erb b/app/views/content/render_page.html.erb deleted file mode 100644 index 4c21667..0000000 --- a/app/views/content/render_page.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -
-

<%= @page.title %>

-

<%= date_for_page @page %>, <%= @page.user.try(:login) %>

-
-

<%= @page.abstract %>

- <%= aggregate?(@page.body) %> -
\ 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 @@ +
+

<%= @page.title %>

+
+

<%= @page.abstract %>

+ <%= aggregate?(@page.body) %> +
\ No newline at end of file diff --git a/app/views/custom/page_templates/public/render_page.html.erb b/app/views/custom/page_templates/public/render_page.html.erb new file mode 100644 index 0000000..4c21667 --- /dev/null +++ b/app/views/custom/page_templates/public/render_page.html.erb @@ -0,0 +1,7 @@ +
+

<%= @page.title %>

+

<%= date_for_page @page %>, <%= @page.user.try(:login) %>

+
+

<%= @page.abstract %>

+ <%= aggregate?(@page.body) %> +
\ No newline at end of file diff --git a/app/views/custom/partials/_sidebar_title_only.html.erb b/app/views/custom/partials/_sidebar_title_only.html.erb new file mode 100644 index 0000000..819d9ae --- /dev/null +++ b/app/views/custom/partials/_sidebar_title_only.html.erb @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/app/views/custom_templates/partials/_sidebar_title_only.html.erb b/app/views/custom_templates/partials/_sidebar_title_only.html.erb deleted file mode 100644 index 819d9ae..0000000 --- a/app/views/custom_templates/partials/_sidebar_title_only.html.erb +++ /dev/null @@ -1,3 +0,0 @@ - \ No newline at end of file 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 @@ <%= f.error_messages %> <% fields_for @draft do |d| %> -

+

+ <%= d.label :template_name %> + <%= d.select :template_name, custom_page_templates %> +

<%= d.label :title %>
<%= 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 @@ +class AddTemplateColumnToPages < ActiveRecord::Migration + def self.up + add_column :pages, :template, :string + end + + def self.down + remove_column :pages, :template + end +end 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 @@ +class RenameTemplateToTemplateName < ActiveRecord::Migration + def self.up + rename_column :pages, :template, :template_name + end + + def self.down + rename_column :pages, :template_name, :template + end +end 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 assert_select(".sidebar_headline", "two") end + def test_nonexistant_custom_template_defaults_to_standard_template + new_node = create_node_under_root "fnord" + draft = new_node.find_or_create_draft @user1 + draft.template_name = "huchibu" + draft.save + new_node.publish_draft! + + get :render_page, :locale => 'de', :page_path => ["fnord"] + assert_response :success + assert_template "custom/page_templates/public/render_page.html.erb" + end + def test_custom_template_no_date_and_author + new_node = create_node_under_root "fnord" + draft = new_node.find_or_create_draft @user1 + draft.template_name = "no_date_and_author" + draft.save + new_node.publish_draft! + + get :render_page, :locale => 'de', :page_path => ["fnord"] + assert_response :success + assert_template "custom/page_templates/public/no_date_and_author.html.erb" + end protected 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 assert_equal "Hello", test_node.draft.title assert_equal "There", test_node.draft.body end + + def test_update_a_draft_with_changing_the_template + test_node = Node.create! :slug => "test_node" + test_node.move_to_child_of Node.root + + login_as :quentin + put :update, { + :id => test_node.id, + :page => { + :title => "Hello", + :body => "There", + :template_name => "Foobar" + } + } + + test_node.reload + assert_equal "Hello", test_node.draft.title + assert_equal "There", test_node.draft.body + assert_equal "Foobar", test_node.draft.template_name + end end -- cgit v1.3