From 3c77b85cff2d4866f01a0d2e7ec9217efd51b976 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 24 Feb 2015 16:11:02 +0000 Subject: [PATCH] fixup! f5cb71a78b41f2164fd90ae1eb5e60d6499352fa headings --- .../admin_public_body_headings_controller.rb | 25 ++++++++++++++++--- .../admin_public_body_headings/_form.html.erb | 4 +-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin_public_body_headings_controller.rb b/app/controllers/admin_public_body_headings_controller.rb index caac209287..1037a0ec95 100644 --- a/app/controllers/admin_public_body_headings_controller.rb +++ b/app/controllers/admin_public_body_headings_controller.rb @@ -16,7 +16,20 @@ def update redirect_to edit_admin_heading_path(@heading) else I18n.available_locales.each do |locale| - @heading.translations.find_or_initialize_by_locale(locale) + if locale == I18n.default_locale + next + end + + next if @heading.translations.map(&:locale).include?(locale) + + translation_params = params[:public_body_heading]. + fetch(:translations_attributes, {}). + fetch(locale, nil) + + if !@heading.translations.where(:locale => locale).first && translation_params + @heading.translations.build(translation_params) + end + end render :action => 'edit' end @@ -57,13 +70,17 @@ def create redirect_to admin_categories_url else I18n.available_locales.each do |locale| + if locale == I18n.default_locale + @heading.translations.build(:locale => locale) + next + end + translation_params = params[:public_body_heading]. fetch(:translations_attributes, {}). fetch(locale, nil) - if translation_params + + if !@heading.translations.where(:locale => locale).first && translation_params @heading.translations.build(translation_params) - else - @heading.translations.build(:locale => locale) end end render :action => 'new' diff --git a/app/views/admin_public_body_headings/_form.html.erb b/app/views/admin_public_body_headings/_form.html.erb index 1ad31f3e0c..dce3365a80 100644 --- a/app/views/admin_public_body_headings/_form.html.erb +++ b/app/views/admin_public_body_headings/_form.html.erb @@ -4,7 +4,7 @@
- <% @heading.translations.each do |translation| %> + <% @heading.translations.select{ |t| I18n.available_locales.include?(t.locale) }.sort_by{ |t| I18n.available_locales.index(t.locale) }.each do |translation| %> <% if translation.locale.to_s == I18n.default_locale.to_s %> <%= fields_for 'public_body_heading', @heading do |t| %> <%= render :partial => 'locale_fields', :locals => { :t => t, :locale => translation.locale } %>