Fix link in structure

This commit is contained in:
chris 2013-10-30 18:53:23 +08:00
parent a885313e75
commit 00010867a4
5 changed files with 32 additions and 26 deletions

View File

@ -1,4 +1,5 @@
class Admin::ItemsController < OrbitBackendController
include ActionView::Helpers::TagHelper
include ActionView::Helpers::DynamicForm
layout "structure"

View File

@ -1,5 +1,6 @@
class Admin::LinksController < Admin::ItemsController
def new
@item = Link.new
@item.parent = Page.find(params[:parent_id]) rescue nil
@ -13,27 +14,21 @@ class Admin::LinksController < Admin::ItemsController
def create
@item = Link.new(params[:link])
if @item.save(params[:link])
success = true
if @item.save
render 'admin/items/reload_items'
else
success = check_valid_url
end
if success
flash.now[:notice] = t('create.success.link')
respond_to do |format|
format.js { render 'admin/items/reload_items' }
end
else
flash.now[:error] = t('create.error.link')
render :action => "new"
@error = error_messages_for(@item)
render 'admin/items/form_error'
end
end
def update
@item = Link.find(params[:id])
unless @item.update_attributes(params[:link])
if @item.update_attributes(params[:link])
render 'admin/items/reload_items'
else
@error = error_messages_for(@item)
render 'admin/items/form_error'
end
end

View File

@ -1,6 +1,8 @@
class Link < Item
field :url, localize: true
validates :url, at_least_one: true
# 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
@ -11,20 +13,27 @@ class Link < Item
end
def urls
self.url_translations.inject({}) do |result, (key, value)|
result["url-#{key}"] = value
result
end
if self.url_translations.present?
self.url_translations.inject({}) do |result, (key, value)|
result["url-#{key}"] = value
result
end
else
{}
end
end
protected
def add_http
if self.url_translations.present?
self.url_translations.each_value do |u|
unless u[/^(http|https):\/\//] || u.blank?
u = 'http://' + u
self.url_translations = self.url_translations.inject({}) do |translations, (key, value)|
if value.blank? || value[/^(http|https):\/\//]
translations[key] = value
else
translations[key] = 'http://' + value
end
translations
end
end
end

View File

@ -7,15 +7,16 @@
<%= f.text_field :name, class: 'input-xlarge', placeholder: t(:name), id: 'name' %>
<span class="help-block"><%= t("front_page.name_field_helper") %></span>
<%= f.fields_for :title_translations do |f| %>
<% @site_valid_locales.each do |locale| %>
<% @site_valid_locales.each do |locale| %>
<%= f.fields_for :title_translations do |f| %>
<%= f.label :locale, "#{t(:title)} #{I18nVariable.from_locale(locale)}" %>
<%= f.text_field locale, class: 'input-xlarge', placeholder: "#{t(:title)} #{I18nVariable.from_locale(locale)}", id: locale %>
<%= f.label :url, "#{t(:url)} #{I18nVariable.from_locale(locale)}" %>
<%= f.text_field :url, :class => 'input-xlarge', placeholder: "#{t(:url)} #{I18nVariable.from_locale(locale)}", id: "url-#{locale}" %>
<% end %>
<%= f.fields_for :url_translations do |f| %>
<%= f.label :locale, "#{t(:url)} #{I18nVariable.from_locale(locale)}" %>
<%= f.text_field locale, :class => 'input-xlarge', placeholder: "#{t(:url)} #{I18nVariable.from_locale(locale)}", id: "url-#{locale}" %>
<% end %>
<% end %>
<div class="checkbox-groups" id="link-is-published">
<%= f.label :is_published, t(:is_published) %>
<p>