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 ++- 10 files changed, 84 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 (limited to 'app') 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 %> -- cgit v1.3