From 13846d2702639c018507a5231b2efcfc5ff6683f Mon Sep 17 00:00:00 2001 From: hukl Date: Thu, 5 Mar 2009 21:11:46 +0100 Subject: rewriting url in body fields to include the locale before saving the page. xpath magick for the win! added corresponding test of course --- app/models/page.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'app') 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 belongs_to :node belongs_to :user + # Filter + before_save :rewrite_links_in_body + # Security attr_accessible :title, :abstract, :body, :template_name @@ -116,4 +119,29 @@ class Page < ActiveRecord::Base I18n.locale = locale_before end + + private + + def rewrite_links_in_body + if self.body + tmp_body = "
#{self.body}
" + xml_string = XML::Parser.string( tmp_body ) + xml_doc = xml_string.parse + links = xml_doc.find("a[not(starts-with(@href, 'http://'))]") + + locales = I18n.available_locales.reject {|l| l == :root} + + links.each do |link| + unless locales.include? link[:href].slice(1,2).to_sym + link[:href] = link[:href].sub(/^\//, "/#{I18n.locale}/") + end + end + + tmp_body = xml_doc.to_s.gsub(/(\n\|\<\/div\>\n)/, "") + tmp_body.gsub!("", "") + + self.body = tmp_body + end + end + end \ No newline at end of file -- cgit v1.3