Fix link in structure when the item is a link

Add internal links creation
This commit is contained in:
chris 2013-07-30 11:49:28 +08:00
parent c85629d3b3
commit 74b2bbf4b7
3 changed files with 55 additions and 4 deletions

View File

@ -18,7 +18,12 @@ class Admin::LinksController < Admin::ItemsController
def create def create
@item = Link.new(params[:link]) @item = Link.new(params[:link])
if @item.save if @item.save(params[:link])
success = true
else
success = check_valid_url
end
if success
flash.now[:notice] = t('create.success.link') flash.now[:notice] = t('create.success.link')
respond_to do |format| respond_to do |format|
format.js { render 'admin/items/reload_items' } format.js { render 'admin/items/reload_items' }
@ -33,6 +38,11 @@ class Admin::LinksController < Admin::ItemsController
@item = Link.find(params[:id]) @item = Link.find(params[:id])
if @item.update_attributes(params[:link]) if @item.update_attributes(params[:link])
success = true
else
success = check_valid_url
end
if success
flash.now[:notice] = t('update.success.link') flash.now[:notice] = t('update.success.link')
respond_to do |format| respond_to do |format|
format.js { render 'admin/items/reload_items' } format.js { render 'admin/items/reload_items' }
@ -43,4 +53,41 @@ class Admin::LinksController < Admin::ItemsController
end end
end end
protected
def check_valid_url
if @item.errors.include?(:url) && !@item.errors.added?(:url, :blank) && @item.errors.added?(:url, :invalid)
begin
url = @item.url
url.gsub!('http://', '').slice!(/^\//)
path = Rails.application.routes.recognize_path(url)
if path.has_key?(:page_name)
if Page.where(path: path[:page_name]).first
new_url = "#{request.base_url}/#{url}"
else
success = false
end
else
new_url = "#{request.base_url}/#{url}"
end
if @item.errors.count == 1
@item.url = new_url
if @item.save
success = true
else
success = false
end
else
@item.url = new_url
success = false
end unless success == false
rescue
success = false
end
else
success = false
end
success
end
end end

View File

@ -2,7 +2,7 @@ class Link < Item
field :url field :url
validates :url, :presence => true, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?/i validates :url, :presence => true, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|localhost(:[0-9]{1,5})?(\/.*)?/i
before_validation :add_http before_validation :add_http
@ -13,7 +13,7 @@ class Link < Item
protected protected
def add_http def add_http
unless self.url[/^http:\/\//] || self.url[/^https:\/\//] unless self.url[/^(http|https):\/\//] || self.url.blank?
self.url = 'http://' + self.url self.url = 'http://' + self.url
end end
end end

View File

@ -11,7 +11,11 @@
<% end %> <% end %>
<div class="item-title"> <div class="item-title">
<%= content_tag(:em, node.url, class: "muted") if node.class.to_s.eql?('Link') %> <%= content_tag(:em, node.url, class: "muted") if node.class.to_s.eql?('Link') %>
<% if node.class.to_s.eql?('Page') %>
<%= link_to node.title, "/" + node.path %> <%= link_to node.title, "/" + node.path %>
<% else %>
<%= link_to node.title, node.url %>
<% end %>
<div class="item-menu"> <div class="item-menu">
<%= link_to content_tag(:i, nil, class: "icon-eye-open"), eval("admin_#{node.class.to_s.downcase}_path(node)"), class: "view-page open-slide tip", title: t(:view) if node.class.to_s.eql?('Page') %> <%= link_to content_tag(:i, nil, class: "icon-eye-open"), eval("admin_#{node.class.to_s.downcase}_path(node)"), class: "view-page open-slide tip", title: t(:view) if node.class.to_s.eql?('Page') %>
<%= link_to content_tag(:i, nil, class: "icon-edit"), eval("edit_admin_#{node.class.to_s.downcase}_path(node)"), class: "open-slide tip", title: t(:edit) %> <%= link_to content_tag(:i, nil, class: "icon-edit"), eval("edit_admin_#{node.class.to_s.downcase}_path(node)"), class: "open-slide tip", title: t(:edit) %>