diff options
| author | erdgeist <erdgeist@bauklotz.local> | 2009-03-10 19:59:02 +0100 |
|---|---|---|
| committer | erdgeist <erdgeist@bauklotz.local> | 2009-03-10 19:59:02 +0100 |
| commit | d6049aeffc7de43393a9a7a1d2f95f26422a046f (patch) | |
| tree | 7036f509aa4f4a518a00ddb0e12fe8a6eb4d563a /app/models | |
| parent | 3b3158199f147646348fae0008d3f63062967a87 (diff) | |
| parent | 14ada6b405dac2bea27a2959f6f73a7398776b0b (diff) | |
Merge branch 'master' of ssh://git@svn.medienhaus.udk-berlin.de/usr/local/git/cccms
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/node.rb | 5 | ||||
| -rw-r--r-- | app/models/page.rb | 67 |
2 files changed, 33 insertions, 39 deletions
diff --git a/app/models/node.rb b/app/models/node.rb index 7f2bca6..d2db4ba 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -59,7 +59,6 @@ class Node < ActiveRecord::Base | |||
| 59 | def create_new_draft user | 59 | def create_new_draft user |
| 60 | empty_page = self.pages.new | 60 | empty_page = self.pages.new |
| 61 | empty_page.user = user | 61 | empty_page.user = user |
| 62 | |||
| 63 | empty_page.clone_attributes_from self.head | 62 | empty_page.clone_attributes_from self.head |
| 64 | 63 | ||
| 65 | self.draft = empty_page | 64 | self.draft = empty_page |
| @@ -70,11 +69,9 @@ class Node < ActiveRecord::Base | |||
| 70 | def publish_draft! | 69 | def publish_draft! |
| 71 | if self.draft | 70 | if self.draft |
| 72 | self.head = self.draft | 71 | self.head = self.draft |
| 72 | self.head.save! | ||
| 73 | self.draft = nil | 73 | self.draft = nil |
| 74 | self.save! | 74 | self.save! |
| 75 | |||
| 76 | self.head.published_at = Time.now | ||
| 77 | self.head.save! | ||
| 78 | else | 75 | else |
| 79 | nil | 76 | nil |
| 80 | end | 77 | end |
diff --git a/app/models/page.rb b/app/models/page.rb index a4817a3..af820e3 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | require 'xml' | ||
| 2 | |||
| 1 | class Page < ActiveRecord::Base | 3 | class Page < ActiveRecord::Base |
| 2 | 4 | ||
| 3 | PUBLIC_TEMPLATE_PATH = File.join(%w(custom page_templates public)) | 5 | PUBLIC_TEMPLATE_PATH = File.join(%w(custom page_templates public)) |
| @@ -17,7 +19,7 @@ class Page < ActiveRecord::Base | |||
| 17 | before_save :rewrite_links_in_body | 19 | before_save :rewrite_links_in_body |
| 18 | 20 | ||
| 19 | # Security | 21 | # Security |
| 20 | attr_accessible :title, :abstract, :body, :template_name | 22 | attr_accessible :title, :abstract, :body, :template_name, :published_at |
| 21 | 23 | ||
| 22 | # Class Methods | 24 | # Class Methods |
| 23 | 25 | ||
| @@ -57,24 +59,6 @@ class Page < ActiveRecord::Base | |||
| 57 | end | 59 | end |
| 58 | 60 | ||
| 59 | # Instance Methods | 61 | # Instance Methods |
| 60 | |||
| 61 | def clone_attributes_from page | ||
| 62 | return nil unless page | ||
| 63 | |||
| 64 | self.tag_list = page.tag_list.join(", ") | ||
| 65 | |||
| 66 | locale_before = I18n.locale | ||
| 67 | |||
| 68 | I18n.available_locales.each do |l| | ||
| 69 | next if l == :root | ||
| 70 | I18n.locale = l | ||
| 71 | self.title = page.title | ||
| 72 | self.abstract = page.abstract | ||
| 73 | self.body = page.body | ||
| 74 | end | ||
| 75 | |||
| 76 | I18n.locale = locale_before | ||
| 77 | end | ||
| 78 | 62 | ||
| 79 | def public_template_path | 63 | def public_template_path |
| 80 | File.join(PUBLIC_TEMPLATE_PATH, template_name) | 64 | File.join(PUBLIC_TEMPLATE_PATH, template_name) |
| @@ -104,8 +88,13 @@ class Page < ActiveRecord::Base | |||
| 104 | def clone_attributes_from page | 88 | def clone_attributes_from page |
| 105 | return nil unless page | 89 | return nil unless page |
| 106 | 90 | ||
| 91 | # Clone untranslated attributes | ||
| 92 | |||
| 107 | self.tag_list = page.tag_list.join(", ") | 93 | self.tag_list = page.tag_list.join(", ") |
| 108 | self.template_name = page.template_name | 94 | self.template_name = page.template_name |
| 95 | self.published_at = page.published_at | ||
| 96 | |||
| 97 | # Clone translated attributes | ||
| 109 | 98 | ||
| 110 | locale_before = I18n.locale | 99 | locale_before = I18n.locale |
| 111 | 100 | ||
| @@ -120,27 +109,35 @@ class Page < ActiveRecord::Base | |||
| 120 | I18n.locale = locale_before | 109 | I18n.locale = locale_before |
| 121 | end | 110 | end |
| 122 | 111 | ||
| 112 | def public? | ||
| 113 | published_at.nil? ? true : published_at < Time.now | ||
| 114 | end | ||
| 115 | |||
| 123 | private | 116 | private |
| 124 | 117 | ||
| 125 | def rewrite_links_in_body | 118 | def rewrite_links_in_body |
| 126 | if self.body | 119 | begin |
| 127 | tmp_body = "<div>#{self.body}</div>" | 120 | if self.body |
| 128 | xml_string = XML::Parser.string( tmp_body ) | 121 | tmp_body = "<div>#{self.body}</div>" |
| 129 | xml_doc = xml_string.parse | 122 | xml_string = XML::Parser.string( tmp_body ) |
| 130 | links = xml_doc.find("a[not(starts-with(@href, 'http://'))]") | 123 | xml_doc = xml_string.parse |
| 131 | 124 | links = xml_doc.find("//a[not(starts-with(@href, 'http://'))]") | |
| 132 | locales = I18n.available_locales.reject {|l| l == :root} | 125 | |
| 133 | 126 | locales = I18n.available_locales.reject {|l| l == :root} | |
| 134 | links.each do |link| | 127 | |
| 135 | unless locales.include? link[:href].slice(1,2).to_sym | 128 | links.each do |link| |
| 136 | link[:href] = link[:href].sub(/^\//, "/#{I18n.locale}/") | 129 | unless locales.include? link[:href].slice(1,2).to_sym |
| 130 | link[:href] = link[:href].sub(/^\//, "/#{I18n.locale}/") | ||
| 131 | end | ||
| 137 | end | 132 | end |
| 133 | |||
| 134 | tmp_body = xml_doc.to_s.gsub(/(\n\<div\>|\<\/div\>\n)/, "") | ||
| 135 | tmp_body.gsub!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "") | ||
| 136 | |||
| 137 | self.body = tmp_body | ||
| 138 | end | 138 | end |
| 139 | 139 | rescue | |
| 140 | tmp_body = xml_doc.to_s.gsub(/(\n\<div\>|\<\/div\>\n)/, "") | 140 | nil |
| 141 | tmp_body.gsub!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "") | ||
| 142 | |||
| 143 | self.body = tmp_body | ||
| 144 | end | 141 | end |
| 145 | end | 142 | end |
| 146 | 143 | ||
