From 8fcb8ef8def264ada2ae9287e3909c3f3ba329cd Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Mon, 14 May 2012 21:43:10 +0800 Subject: [PATCH] Change the way links are displayed in menus in front-end --- app/controllers/pages_controller.rb | 2 +- app/models/link.rb | 12 +++++++++++- lib/parsers/parser_common.rb | 6 ++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 45299450..52c39c01 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -21,7 +21,7 @@ class PagesController < ApplicationController when 'Page' render_page when 'Link' - redirect_to "http://#{@item[:url]}" + redirect_to @item[:url] end else render :file => "#{Rails.root}/public/404.html", :status => :not_found diff --git a/app/models/link.rb b/app/models/link.rb index 4301ead4..5b8c15a6 100644 --- a/app/models/link.rb +++ b/app/models/link.rb @@ -2,10 +2,20 @@ class Link < Item field :url - validates_presence_of :url + validates :url, :presence => true, :format => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix + + before_validation :add_http def link ApplicationController.helpers.link_to(self.name, self.url) end + + protected + + def add_http + unless self.url[/^http?s:\/\//] + self.url = 'http://' + self.url + end + end end diff --git a/lib/parsers/parser_common.rb b/lib/parsers/parser_common.rb index d727e68c..32820a6f 100644 --- a/lib/parsers/parser_common.rb +++ b/lib/parsers/parser_common.rb @@ -27,7 +27,8 @@ module ParserCommon res << "_#{i}" if i res << " active" if (current_page.id.eql?(page.id) || current_page.descendant_of?(page)) res << "'>" - res << "#{page.i18n_variable[I18n.locale]}" + root = "/" + res << "#{page.i18n_variable[I18n.locale]}" if page.visible_children.size > 0 && current <= menu.levels res << "" res << menu_level(page, current_page, current + 1, menu, edit) @@ -119,7 +120,8 @@ module ParserCommon res << ""