From 00010867a48da6f0c371c5ebea2a3ad00620071d Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 30 Oct 2013 18:53:23 +0800 Subject: [PATCH] Fix link in structure --- app/controllers/admin/items_controller.rb | 1 + app/controllers/admin/links_controller.rb | 23 ++++++++----------- app/models/link.rb | 23 +++++++++++++------ app/views/admin/items/_form_link.html.erb | 11 +++++---- .../{_form_error.js.erb => form_error.js.erb} | 0 5 files changed, 32 insertions(+), 26 deletions(-) rename app/views/admin/items/{_form_error.js.erb => form_error.js.erb} (100%) diff --git a/app/controllers/admin/items_controller.rb b/app/controllers/admin/items_controller.rb index bd7c7b4ac..4aa7ae179 100644 --- a/app/controllers/admin/items_controller.rb +++ b/app/controllers/admin/items_controller.rb @@ -1,4 +1,5 @@ class Admin::ItemsController < OrbitBackendController + include ActionView::Helpers::TagHelper include ActionView::Helpers::DynamicForm layout "structure" diff --git a/app/controllers/admin/links_controller.rb b/app/controllers/admin/links_controller.rb index 99d8dca71..63744182f 100644 --- a/app/controllers/admin/links_controller.rb +++ b/app/controllers/admin/links_controller.rb @@ -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 diff --git a/app/models/link.rb b/app/models/link.rb index 5f2f28080..5246e3304 100644 --- a/app/models/link.rb +++ b/app/models/link.rb @@ -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 diff --git a/app/views/admin/items/_form_link.html.erb b/app/views/admin/items/_form_link.html.erb index 7a448095d..570696242 100644 --- a/app/views/admin/items/_form_link.html.erb +++ b/app/views/admin/items/_form_link.html.erb @@ -7,15 +7,16 @@ <%= f.text_field :name, class: 'input-xlarge', placeholder: t(:name), id: 'name' %> <%= t("front_page.name_field_helper") %> - <%= 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 %> -