Skip to content

Commit

Permalink
Fix translating Public Body Headings
Browse files Browse the repository at this point in the history
Fixes submission of form containing both existing and new
translations
  • Loading branch information
garethrees committed Mar 3, 2015
1 parent 10e9c82 commit 205000f
Show file tree
Hide file tree
Showing 7 changed files with 654 additions and 183 deletions.
63 changes: 33 additions & 30 deletions app/controllers/admin_public_body_headings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
class AdminPublicBodyHeadingsController < AdminController

def new
@heading = PublicBodyHeading.new
@heading.build_all_translations
end

def create
I18n.with_locale(I18n.default_locale) do
@heading = PublicBodyHeading.new(params[:public_body_heading])
if @heading.save
flash[:notice] = 'Heading was successfully created.'
redirect_to admin_categories_url
else
@heading.build_all_translations
render :action => 'new'
end
end
end

def edit
@heading = PublicBodyHeading.find(params[:id])
render :formats => [:html]
@heading.build_all_translations
end

def update
@heading = PublicBodyHeading.find(params[:id])

I18n.with_locale(I18n.default_locale) do
@heading = PublicBodyHeading.find(params[:id])
if @heading.update_attributes(params[:public_body_heading])
flash[:notice] = 'Category heading was successfully updated.'
flash[:notice] = 'Heading was successfully updated.'
redirect_to edit_admin_heading_path(@heading)
else
@heading.build_all_translations
render :action => 'edit'
end
end
end

def destroy
@locale = self.locale_from_params
I18n.with_locale(@locale) do
heading = PublicBodyHeading.find(params[:id])
heading.destroy
flash[:notice] = "Heading was successfully destroyed."
redirect_to admin_categories_url
end
end

def reorder
transaction = reorder_headings(params[:headings])
if transaction[:success]
Expand All @@ -35,33 +65,6 @@ def reorder_categories
end
end

def new
@heading = PublicBodyHeading.new
render :formats => [:html]
end

def create
I18n.with_locale(I18n.default_locale) do
@heading = PublicBodyHeading.new(params[:public_body_heading])
if @heading.save
flash[:notice] = 'Category heading was successfully created.'
redirect_to admin_categories_url
else
render :action => 'new'
end
end
end

def destroy
@locale = self.locale_from_params()
I18n.with_locale(@locale) do
heading = PublicBodyHeading.find(params[:id])
heading.destroy
flash[:notice] = "Category heading was successfully destroyed."
redirect_to admin_categories_url
end
end

protected

def reorder_headings(headings)
Expand Down
41 changes: 23 additions & 18 deletions app/models/public_body_heading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
#

class PublicBodyHeading < ActiveRecord::Base
attr_accessible :name, :display_order, :translated_versions
attr_accessible :locale, :name, :display_order, :translated_versions,
:translations_attributes

has_many :public_body_category_links, :dependent => :destroy
has_many :public_body_categories, :order => :category_display_order, :through => :public_body_category_links
default_scope order('display_order ASC')

translates :name
accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params?

validates_uniqueness_of :name, :message => 'Name is already taken'
validates_presence_of :name, :message => 'Name can\'t be blank'
Expand All @@ -37,24 +39,18 @@ def translated_versions
end

def translated_versions=(translation_attrs)
def empty_translation?(attrs)
attrs_with_values = attrs.select{ |key, value| value != '' and key != 'locale' }
attrs_with_values.empty?
end
warn "[DEPRECATION] PublicBodyHeading#translated_versions= will be replaced " \
"by PublicBodyHeading#translations_attributes= as of release 0.22"
self.translations_attributes = translation_attrs
end

def ordered_translations
translations.sort_by { |t| I18n.available_locales.index(t.locale) }
end

if translation_attrs.respond_to? :each_value # Hash => updating
translation_attrs.each_value do |attrs|
next if empty_translation?(attrs)
t = translation_for(attrs[:locale]) || PublicBodyHeading::Translation.new
t.attributes = attrs
t.save!
end
else # Array => creating
translation_attrs.each do |attrs|
next if empty_translation?(attrs)
new_translation = PublicBodyHeading::Translation.new(attrs)
translations << new_translation
end
def build_all_translations
I18n.available_locales.each do |locale|
translations.build(:locale => locale) unless translations.detect{ |t| t.locale == locale }
end
end

Expand All @@ -72,4 +68,13 @@ def self.next_display_order
end
end

private

def empty_translation_in_params?(attributes)
attrs_with_values = attributes.select do |key, value|
value != '' and key.to_s != 'locale'
end
attrs_with_values.empty?
end

end
41 changes: 12 additions & 29 deletions app/views/admin_public_body_headings/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,20 @@

<div id="div-locales">
<ul class="locales nav nav-tabs">
<% I18n.available_locales.each_with_index do |locale, i| %>
<li><a href="#div-locale-<%=locale.to_s%>" data-toggle="tab" ><%=locale_name(locale.to_s) || "Default locale"%></a></li>
<% end %>
<% @heading.ordered_translations.each do |translation| %>
<li>
<a href="#div-locale-<%= translation.locale.to_s %>" data-toggle="tab" >
<%= locale_name(translation.locale.to_s) || _("Default locale") %>
</a>
</li>
<% end %>
</ul>
<div class="tab-content">
<%
for locale in I18n.available_locales do
if locale==I18n.default_locale # The default locale is submitted as part of the bigger object...
prefix = 'public_body_heading'
object = @heading
else # ...but additional locales go "on the side"
prefix = "public_body_heading[translated_versions][]"
object = @heading.new_record? ?
PublicBodyHeading::Translation.new :
@heading.find_translation_by_locale(locale.to_s) || PublicBodyHeading::Translation.new
end
%>
<%= fields_for prefix, object do |t| %>
<div class="tab-pane" id="div-locale-<%=locale.to_s%>">
<div class="control-group">
<%= t.hidden_field :locale, :value => locale.to_s %>
<label for="<%= form_tag_id(t.object_name, :name, locale) %>" class="control-label">Name</label>
<div class="controls">
<%= t.text_field :name, :id => form_tag_id(t.object_name, :name, locale), :class => "span4" %>
</div>
</div>
</div>
<%
end
end
%>
<% @heading.ordered_translations.each do |translation| %>
<%= f.fields_for(:translations, translation, :child_index => translation.locale) do |t| %>
<%= render :partial => 'locale_fields', :locals => { :t => t, :locale => translation.locale } %>
<% end %>
<% end %>
</div>
</div>

Expand Down
9 changes: 9 additions & 0 deletions app/views/admin_public_body_headings/_locale_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="tab-pane" id="div-locale-<%=locale.to_s%>">
<div class="control-group">
<%= t.hidden_field :locale, :value => locale.to_s %>
<label for="<%= form_tag_id(t.object_name, :name, locale) %>" class="control-label">name</label>
<div class="controls">
<%= t.text_field :name, :id => form_tag_id(t.object_name, :name, locale), :class => "span4" %>
</div>
</div>
</div>
Loading

0 comments on commit 205000f

Please sign in to comment.