From 1de97a5ac6f4621f8013d6f73c6c9edfae445b70 Mon Sep 17 00:00:00 2001 From: hukl Date: Sun, 15 Nov 2009 14:31:52 +0100 Subject: removed date and author from the standard template and introduced a dedicated update template which will get auto assigned if a node is created under updates// Added tests and convenient method --- app/models/node.rb | 6 +++++- app/models/page.rb | 15 ++++++++++----- .../page_templates/public/standard_template.html.erb | 1 - app/views/custom/page_templates/public/update.html.erb | 9 +++++++++ test/unit/node_test.rb | 13 +++++++++++++ test/unit/page_test.rb | 9 +++++++++ 6 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 app/views/custom/page_templates/public/update.html.erb diff --git a/app/models/node.rb b/app/models/node.rb index 7e64fdd..e9d935a 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -123,7 +123,7 @@ class Node < ActiveRecord::Base # returns an array with all parts of a unique_name rather than a string def unique_path - unique_name.split("/") rescue unique_name + unique_name.split("/") rescue [unique_name] end # returns array with pages up to root excluding root @@ -165,6 +165,10 @@ class Node < ActiveRecord::Base head_id end + def update? + unique_path.length == 3 && unique_path[0] == "updates" + end + # Returns immutable node id for all new nodes so that the atom feed entry ids # stay the same eventhough the slug or positions changes. # Can be removed after a year or so ;) diff --git a/app/models/page.rb b/app/models/page.rb index b17c32b..22d2c69 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -37,6 +37,7 @@ class Page < ActiveRecord::Base # Filter before_create :set_page_title before_create :set_published_at + before_create :set_template before_save :rewrite_links_in_body # Validations @@ -128,9 +129,9 @@ class Page < ActiveRecord::Base self.reload # Clone untranslated attributes - self.tag_list = page.tag_list - self.template_name = page.template_name - self.published_at = page.published_at + self.tag_list = page.tag_list + self.template_name ||= page.template_name + self.published_at = page.published_at # Getting rid of the auto-generated empty translations self.globalize_translations.delete_all @@ -206,6 +207,12 @@ class Page < ActiveRecord::Base end end + def set_template + if node && node.update? + self.template_name = "update" + end + end + def rewrite_links_in_body begin if self.body @@ -220,8 +227,6 @@ class Page < ActiveRecord::Base aggregate_tags[0].parent.replace_with aggregate_tags[0] end - - links.each do |link| unless locales.include? link[:href].slice(1,2).to_sym link[:href] = link[:href].sub(/^\//, "/#{I18n.locale}/") diff --git a/app/views/custom/page_templates/public/standard_template.html.erb b/app/views/custom/page_templates/public/standard_template.html.erb index 5ea277a..8000af5 100644 --- a/app/views/custom/page_templates/public/standard_template.html.erb +++ b/app/views/custom/page_templates/public/standard_template.html.erb @@ -1,6 +1,5 @@

<%= @page.title %>

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

<%= sanitize( @page.abstract )%>

<%= headline_image %>
<%= aggregate?(@page.body) %>
diff --git a/app/views/custom/page_templates/public/update.html.erb b/app/views/custom/page_templates/public/update.html.erb new file mode 100644 index 0000000..5ea277a --- /dev/null +++ b/app/views/custom/page_templates/public/update.html.erb @@ -0,0 +1,9 @@ +
+

<%= @page.title %>

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

<%= sanitize( @page.abstract )%>

+
<%= headline_image %>
+
<%= aggregate?(@page.body) %>
+
+ +<%= will_paginate(@content_collection) if @content_collection %> \ No newline at end of file diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index dd010cd..a106856 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb @@ -263,6 +263,19 @@ class NodeTest < ActiveSupport::TestCase assert_equal "quentin", node.head.user.login end + + test "update?" do + Node.root.descendants.delete_all + updates = Node.root.children.create!( :slug => "updates" ) + assert !updates.update? + + updates2009 = updates.children.create!( :slug => "2009" ) + assert !updates2009.update? + + update = updates2009.children.create!( :slug => "my-first-update" ) + assert update.update? + end + def create_revisions node, count count.times do node.find_or_create_draft @user1 diff --git a/test/unit/page_test.rb b/test/unit/page_test.rb index c2599e3..bb82da0 100644 --- a/test/unit/page_test.rb +++ b/test/unit/page_test.rb @@ -134,4 +134,13 @@ class PageTest < ActiveSupport::TestCase assert_equal 1, Page.find_with_outdated_translations(:delta_time => 23.minutes).count assert_equal 2, Page.count end + + test "pages under /updates node get the update template assigned" do + Node.root.descendants.delete_all + updates = Node.root.children.create!( :slug => "updates" ) + updates2009 = updates.children.create!( :slug => "2009" ) + update = updates2009.children.create!( :slug => "my-first-update" ) + assert_equal "update", update.draft.template_name + end + end -- cgit v1.3