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 authored and crowbot committed Feb 5, 2015
1 parent d389952 commit 8e5806f
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 38 deletions.
6 changes: 6 additions & 0 deletions app/controllers/admin_public_body_headings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class AdminPublicBodyHeadingsController < AdminController

def edit
@heading = PublicBodyHeading.find(params[:id])
I18n.available_locales.each do |locale|
@heading.translations.find_or_initialize_by_locale(locale)
end
render :formats => [:html]
end

Expand Down Expand Up @@ -37,6 +40,9 @@ def reorder_categories

def new
@heading = PublicBodyHeading.new
I18n.available_locales.each do |locale|
@heading.translations.build(:locale => locale)
end
render :formats => [:html]
end

Expand Down
22 changes: 18 additions & 4 deletions app/models/public_body_heading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#

class PublicBodyHeading < ActiveRecord::Base
attr_accessible :name, :display_order, :translated_versions
attr_accessible :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

validates_uniqueness_of :name, :message => 'Name is already taken'
validates_presence_of :name, :message => 'Name can\'t be blank'
Expand All @@ -35,12 +37,11 @@ def translated_versions
translations
end

def translated_versions=(translation_attrs)
def translations_attributes=(translation_attrs)
def empty_translation?(attrs)
attrs_with_values = attrs.select{ |key, value| value != '' and key != 'locale' }
attrs_with_values = attrs.select{ |key, value| value != '' and key.to_s != 'locale' }
attrs_with_values.empty?
end

if translation_attrs.respond_to? :each_value # Hash => updating
translation_attrs.each_value do |attrs|
next if empty_translation?(attrs)
Expand All @@ -49,6 +50,12 @@ def empty_translation?(attrs)
t.save!
end
else # Array => creating
warn "[DEPRECATION] PublicBodyHeading#translations_attributes= " \
"will no longer accept an Array as of release 0.22. " \
"Use Hash arguments instead. See " \
"spec/models/public_body_heading_spec.rb and " \
"app/views/admin_public_body_headings/_form.html.erb for more " \
"details."
translation_attrs.each do |attrs|
next if empty_translation?(attrs)
new_translation = PublicBodyHeading::Translation.new(attrs)
Expand All @@ -57,6 +64,13 @@ def empty_translation?(attrs)
end
end

def translated_versions=(translation_attrs)
warn "[DEPRECATION] PublicBodyHeading#translated_versions= will be replaced " \
"by PublicBodyHeading#translations_attributes= as of release 0.22"
self.translations_attributes = translation_attrs
end


def add_category(category)
unless public_body_categories.include?(category)
public_body_categories << category
Expand Down
47 changes: 18 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,26 @@

<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.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.translations.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 } %>
<% end %>
<% else %>
<%= f.fields_for(:translations, translation, :child_index => translation.locale) do |t| %>
<%= render :partial => 'locale_fields', :locals => { :t => t, :locale => translation.locale } %>
<% end %>
<% 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>
105 changes: 100 additions & 5 deletions spec/controllers/admin_public_body_headings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
assigns[:heading].should be_a(PublicBodyHeading)
end

it "builds new translations for all locales" do
get :new

translations = assigns[:heading].translations.map{ |t| t.locale.to_s }.sort
available = I18n.available_locales.map{ |l| l.to_s }.sort

expect(translations).to eq(available)
end

it 'renders the new template' do
get :new
expect(response).to render_template('new')
Expand All @@ -33,8 +42,10 @@
post :create, {
:public_body_heading => {
:name => 'New Heading',
:translated_versions => [{ :locale => "es",
:name => "Mi Nuevo Heading" }]
:translations_attributes => {
'es' => { :locale => "es",
:name => "Mi Nuevo Heading" }
}
}
}
PublicBodyHeading.count.should == n + 1
Expand Down Expand Up @@ -70,6 +81,11 @@
expect(assigns[:heading]).to eq(@heading)
end

it "builds new translations if the body does not already have a translation in the specified locale" do
get :edit, :id => @heading.id
expect(assigns[:heading].translations.map(&:locale)).to include(:fr)
end

it "renders the edit template" do
get :edit, :id => @heading.id
expect(assigns[:heading]).to render_template('edit')
Expand All @@ -96,9 +112,9 @@
:id => @heading.id,
:public_body_heading => {
:name => @name,
:translated_versions => {
@heading.id => {:locale => "es",
:name => "Renamed"}
:translations_attributes => {
'es' => { :locale => "es",
:name => "Renamed" }
}
}
}
Expand All @@ -114,6 +130,85 @@
end
end

it 'adds a new translation' do
put :update, {
:id => @heading.id,
:public_body_heading => {
:name => @heading.name,
:translations_attributes => {
'es' => { :locale => "es",
:name => "Example Public Body Heading ES"}
}
}
}

request.flash[:notice].should include('successful')

pbh = PublicBodyHeading.find(@heading.id)

I18n.with_locale(:es) do
expect(pbh.name).to eq('Example Public Body Heading ES')
end
end

it 'adds new translations' do
post :update, {
:id => @heading.id,
:public_body_heading => {
:name => @heading.name,
:translations_attributes => {
'es' => { :locale => "es",
:name => "Example Public Body Heading ES" },
'fr' => { :locale => "fr",
:name => "Example Public Body Heading FR" },
}
}
}

request.flash[:notice].should include('successful')

pbh = PublicBodyHeading.find(@heading.id)

I18n.with_locale(:es) do
expect(pbh.name).to eq('Example Public Body Heading ES')
end
I18n.with_locale(:fr) do
expect(pbh.name).to eq('Example Public Body Heading FR')
end
end

it 'updates an existing translation and adds a third translation' do
@heading.translations.create(:locale => 'es',
:name => 'Example Public Body Heading ES')
@heading.reload

post :update, {
:id => @heading.id,
:public_body_heading => {
:name => @heading.name,
:translations_attributes => {
# Update existing translation
'es' => { :locale => "es",
:name => "Renamed Example Public Body Heading ES" },
# Add new translation
'fr' => { :locale => "fr",
:name => "Example Public Body Heading FR" }
}
}
}

request.flash[:notice].should include('successful')

pbh = PublicBodyHeading.find(@heading.id)

I18n.with_locale(:es) do
expect(pbh.name).to eq('Renamed Example Public Body Heading ES')
end
I18n.with_locale(:fr) do
expect(pbh.name).to eq('Example Public Body Heading FR')
end
end

it "redirects to the edit page after a successful update" do
post :update, { :id => @heading.id,
:public_body_heading => { :name => "Renamed" } }
Expand Down
58 changes: 58 additions & 0 deletions spec/integration/admin_public_body_heading_edit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/alaveteli_dsl')

describe 'Editing a Public Body Heading' do
before do
AlaveteliConfiguration.stub!(:skip_admin_auth).and_return(false)

confirm(:admin_user)
@admin = login(:admin_user)
@heading = FactoryGirl.create(:public_body_heading)
end

it 'can edit the default locale' do
@admin.visit edit_admin_heading_path(@heading)
@admin.fill_in 'public_body_heading_name__en', :with => 'New Heading EN'
@admin.click_button 'Save'

@heading.reload
expect(@heading.name).to eq('New Heading EN')
end

it 'can add a translation for a single locale' do
expect(@heading.find_translation_by_locale('fr')).to be_nil

@admin.visit edit_admin_heading_path(@heading)
@admin.fill_in 'public_body_heading_translations_attributes_fr_name__fr', :with => 'New Heading FR'
@admin.click_button 'Save'

@heading.reload
I18n.with_locale(:fr) do
expect(@heading.name).to eq('New Heading FR')
end
end

it 'can add a translation for multiple locales' do
# Add FR translation
expect(@heading.find_translation_by_locale('fr')).to be_nil
@admin.visit edit_admin_heading_path(@heading)
@admin.fill_in 'public_body_heading_translations_attributes_fr_name__fr', :with => 'New Heading FR'
@admin.click_button 'Save'

# Add ES translation
expect(@heading.find_translation_by_locale('es')).to be_nil
@admin.visit edit_admin_heading_path(@heading)
@admin.fill_in 'public_body_heading_translations_attributes_es_name__es', :with => 'New Heading ES'
@admin.click_button 'Save'

@heading.reload
I18n.with_locale(:fr) do
expect(@heading.name).to eq('New Heading FR')
end

I18n.with_locale(:es) do
expect(@heading.name).to eq('New Heading ES')
end
end

end
Loading

0 comments on commit 8e5806f

Please sign in to comment.