diff options
| author | hukl <contact@smyck.org> | 2009-03-05 21:11:46 +0100 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-03-05 21:11:46 +0100 |
| commit | 13846d2702639c018507a5231b2efcfc5ff6683f (patch) | |
| tree | 554041d14687d91336d070e9f0bce26869d03c28 /app | |
| parent | a006c9c54bf19273fca2f0607c82c069623a4125 (diff) | |
rewriting url in body fields to include the locale before saving the page. xpath magick for the win!
added corresponding test of course
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/page.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/app/models/page.rb b/app/models/page.rb index 950d9f2..a4817a3 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -13,6 +13,9 @@ class Page < ActiveRecord::Base | |||
| 13 | belongs_to :node | 13 | belongs_to :node |
| 14 | belongs_to :user | 14 | belongs_to :user |
| 15 | 15 | ||
| 16 | # Filter | ||
| 17 | before_save :rewrite_links_in_body | ||
| 18 | |||
| 16 | # Security | 19 | # Security |
| 17 | attr_accessible :title, :abstract, :body, :template_name | 20 | attr_accessible :title, :abstract, :body, :template_name |
| 18 | 21 | ||
| @@ -116,4 +119,29 @@ class Page < ActiveRecord::Base | |||
| 116 | 119 | ||
| 117 | I18n.locale = locale_before | 120 | I18n.locale = locale_before |
| 118 | end | 121 | end |
| 122 | |||
| 123 | private | ||
| 124 | |||
| 125 | def rewrite_links_in_body | ||
| 126 | if self.body | ||
| 127 | tmp_body = "<div>#{self.body}</div>" | ||
| 128 | xml_string = XML::Parser.string( tmp_body ) | ||
| 129 | xml_doc = xml_string.parse | ||
| 130 | links = xml_doc.find("a[not(starts-with(@href, 'http://'))]") | ||
| 131 | |||
| 132 | locales = I18n.available_locales.reject {|l| l == :root} | ||
| 133 | |||
| 134 | links.each do |link| | ||
| 135 | unless locales.include? link[:href].slice(1,2).to_sym | ||
| 136 | link[:href] = link[:href].sub(/^\//, "/#{I18n.locale}/") | ||
| 137 | end | ||
| 138 | end | ||
| 139 | |||
| 140 | tmp_body = xml_doc.to_s.gsub(/(\n\<div\>|\<\/div\>\n)/, "") | ||
| 141 | tmp_body.gsub!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "") | ||
| 142 | |||
| 143 | self.body = tmp_body | ||
| 144 | end | ||
| 145 | end | ||
| 146 | |||
| 119 | end \ No newline at end of file | 147 | end \ No newline at end of file |
