diff options
author | David Cabo <david@calibea.com> | 2011-08-17 02:25:36 +0200 |
---|---|---|
committer | David Cabo <david@calibea.com> | 2011-08-23 23:45:23 +0200 |
commit | 6af5f5cba5cf3c8478d676170faa66bca0ff90ab (patch) | |
tree | edacd0b0f9a87df87f8a82df88cc4286969a3b64 | |
parent | 4b1d6f2baea450949047288e4dc76f4197b85b80 (diff) |
Support editing of public bodies simultaneously in all locales (closes #143)
-rw-r--r-- | app/controllers/admin_public_body_controller.rb | 60 | ||||
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 27 |
2 files changed, 54 insertions, 33 deletions
diff --git a/app/controllers/admin_public_body_controller.rb b/app/controllers/admin_public_body_controller.rb index 245a5aa85..61e4aec54 100644 --- a/app/controllers/admin_public_body_controller.rb +++ b/app/controllers/admin_public_body_controller.rb @@ -89,6 +89,19 @@ class AdminPublicBodyController < AdminController end end + def update_localized_attributes(public_body, params) + I18n.available_locales.each do |locale| + PublicBody.with_locale(locale) do + unless (attr_values = params["public_body_#{locale}"]).nil? + # 'publication_scheme' can't be null in the current DB schema + attr_values[:publication_scheme] = attr_values[:publication_scheme] || '' + public_body.attributes = attr_values + public_body.save! + end + end + end + end + def create # Start creating the public body in the default locale PublicBody.with_locale(I18n.default_locale) do @@ -99,17 +112,8 @@ class AdminPublicBodyController < AdminController @public_body.save! # Next, save the translations in the additional locales - I18n.available_locales.each do |locale| - PublicBody.with_locale(locale) do - unless (attr_values = params["public_body_#{locale}"]).nil? - # 'publication_scheme' can't be null in the current DB schema - attr_values[:publication_scheme] = attr_values[:publication_scheme] || '' - @public_body.attributes = attr_values - @public_body.save! - end - end - end - + update_localized_attributes(@public_body, params) + flash[:notice] = 'PublicBody was successfully created.' redirect_to admin_url('body/show/' + @public_body.id.to_s) end @@ -120,23 +124,35 @@ class AdminPublicBodyController < AdminController end def edit - @locale = self.locale_from_params() - PublicBody.with_locale(@locale) do + PublicBody.with_locale(I18n.default_locale) do @public_body = PublicBody.find(params[:id]) @public_body.last_edit_comment = "" - render end + + # Additional locales (could remove the default, but won't make a difference) + I18n.available_locales.each do |locale| + instance_variable_set("@public_body_#{locale}", @public_body.translations.select{|t| t.locale==locale}.first) + end + + render end def update - @locale = self.locale_from_params() - PublicBody.with_locale(@locale) do - params[:public_body][:last_edit_editor] = admin_http_auth_user() - @public_body = PublicBody.find(params[:id]) - if @public_body.update_attributes(params[:public_body]) - flash[:notice] = 'PublicBody was successfully updated.' - redirect_to admin_url('body/show/' + @public_body.id.to_s) - else + # Start updating the public body in the default locale + PublicBody.with_locale(I18n.default_locale) do + begin + ActiveRecord::Base.transaction do + params[:public_body][:last_edit_editor] = admin_http_auth_user() + @public_body = PublicBody.find(params[:id]) + @public_body.update_attributes!(params[:public_body]) + + # Next, update the translations in the additional locales + update_localized_attributes(@public_body, params) + + flash[:notice] = 'PublicBody was successfully updated.' + redirect_to admin_url('body/show/' + @public_body.id.to_s) + end + rescue ActiveRecord::RecordInvalid render :action => 'edit' end end diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index 67832cb0e..8c0980db4 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -110,19 +110,25 @@ describe AdminPublicBodyController, "when administering public bodies with i18n" end it "edits a public body" do - I18n.default_locale = :es - get :edit, {:id => 3, :locale => :es} - response.body.should include('Baguette') - I18n.default_locale = :en + get :edit, {:id => 3, :locale => :en} + + # When editing a body, the controller returns all available translations + assigns[:public_body_es].name.should == 'El Department for Humpadinking' + assigns[:public_body].name.should == 'Department for Humpadinking' + response.should render_template('edit') end it "saves edits to a public body" do - I18n.default_locale = :es - pb = PublicBody.find(id=3) - pb.name.should == "El Department for Humpadinking" - post :update, { :id => 3, :public_body => { :name => "Renamed", :short_name => "", :tag_string => "some tags", :request_email => 'edited@localhost', :last_edit_comment => 'From test code' }} - response.flash[:notice].should include('successful') - I18n.default_locale = :en + PublicBody.with_locale(:es) do + pb = PublicBody.find(id=3) + pb.name.should == "El Department for Humpadinking" + post :update, { + :id => 3, + :public_body => { :name => "Department for Humpadinking", :short_name => "", :tag_string => "some tags", :request_email => 'edited@localhost', :last_edit_comment => 'From test code' }, + :public_body_es => { :name => "Renamed", :short_name => "", :tag_string => "some tags", :request_email => 'edited@localhost', :last_edit_comment => 'From test code' } + } + response.flash[:notice].should include('successful') + end pb = PublicBody.find(public_bodies(:humpadink_public_body).id) PublicBody.with_locale(:es) do @@ -196,5 +202,4 @@ describe AdminPublicBodyController, "when creating public bodies with i18n" do PublicBody.count.should == 2 I18n.locale.should == :en # don't mess up the previous locale end - end |