aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/admin_public_body_controller.rb34
-rwxr-xr-xapp/helpers/link_to_helper.rb2
-rw-r--r--app/views/admin_public_body/_form.rhtml39
-rw-r--r--spec/controllers/admin_public_body_controller_spec.rb64
4 files changed, 109 insertions, 30 deletions
diff --git a/app/controllers/admin_public_body_controller.rb b/app/controllers/admin_public_body_controller.rb
index 13232024c..245a5aa85 100644
--- a/app/controllers/admin_public_body_controller.rb
+++ b/app/controllers/admin_public_body_controller.rb
@@ -90,14 +90,30 @@ class AdminPublicBodyController < AdminController
end
def create
- @locale = self.locale_from_params()
- PublicBody.with_locale(@locale) do
- params[:public_body][:last_edit_editor] = admin_http_auth_user()
- @public_body = PublicBody.new(params[:public_body])
- if @public_body.save
- flash[:notice] = 'PublicBody was successfully created.'
- redirect_to admin_url('body/show/' + @public_body.id.to_s)
- else
+ # Start creating 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.new(params[:public_body])
+ @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
+
+ flash[:notice] = 'PublicBody was successfully created.'
+ redirect_to admin_url('body/show/' + @public_body.id.to_s)
+ end
+ rescue ActiveRecord::RecordInvalid
render :action => 'new'
end
end
@@ -157,7 +173,7 @@ class AdminPublicBodyController < AdminController
# Try with dry run first
csv_contents = params[:csv_file].read
- en = PublicBody.import_csv(csv_contents, params[:tag], true, admin_http_auth_user(), I18n.available_locales)
+ en = PublicBody.import_csv(csv_contents, params[:tag], true, admin_http_auth_user(), available_locales)
errors = en[0]
notes = en[1]
diff --git a/app/helpers/link_to_helper.rb b/app/helpers/link_to_helper.rb
index 445a96920..34354a79b 100755
--- a/app/helpers/link_to_helper.rb
+++ b/app/helpers/link_to_helper.rb
@@ -61,7 +61,7 @@ module LinkToHelper
# Public bodies
def public_body_url(public_body)
- return show_public_body_url(:url_name => public_body.url_name, :only_path => true)
+ public_body.url_name.nil? ? '' : show_public_body_url(:url_name => public_body.url_name, :only_path => true)
end
def public_body_link_short(public_body)
link_to h(public_body.short_or_long_name), public_body_url(public_body)
diff --git a/app/views/admin_public_body/_form.rhtml b/app/views/admin_public_body/_form.rhtml
index b1516be2e..1fb573994 100644
--- a/app/views/admin_public_body/_form.rhtml
+++ b/app/views/admin_public_body/_form.rhtml
@@ -21,14 +21,35 @@
<% end %>
</div>
-<p><label for="public_body_name">Name</label><br/>
-<%= text_field 'public_body', 'name', :size => 60 %></p>
+<h3>Localized Fields</h3>
-<p><label for="public_body_short_name">Short name <small>(only put in abbreviations which are really used, otherwise leave blank. Short or long name is used in the URL - don't worry about breaking URLs through renaming, as the history is used to redirect)</small></label><br/>
-<%= text_field 'public_body', 'short_name', :size => 60 %></p>
+<%
+ for locale in I18n.available_locales do
+ object_name = locale==:en ? 'public_body' : "public_body_#{locale.to_s}"
+%>
+<div>
+ <p>Locale: <%= locale_name(locale.to_s) %></p>
-<p><label for="public_body_request_email">Request email <small>(set to <strong>blank</strong> (empty string) if can't find an address; these emails are <strong>public</strong> as anyone can view with a CAPTCHA)</small></label><br/>
-<%= text_field 'public_body', 'request_email', :size => 40 %></p>
+ <p><label for="public_body_name">Name</label><br/>
+ <%= text_field object_name, 'name', :size => 60 %></p>
+
+ <p><label for="public_body_short_name">Short name <small>(only put in abbreviations which are really used, otherwise leave blank. Short or long name is used in the URL - don't worry about breaking URLs through renaming, as the history is used to redirect)</small></label><br/>
+ <%= text_field object_name, 'short_name', :size => 60 %></p>
+
+ <p><label for="public_body_request_email">Request email <small>(set to <strong>blank</strong> (empty string) if can't find an address; these emails are <strong>public</strong> as anyone can view with a CAPTCHA)</small></label><br/>
+ <%= text_field object_name, 'request_email', :size => 40 %></p>
+
+ <p><label for="public_body_publication_scheme">Publication scheme URL</label><br/>
+ <%= text_field object_name, 'publication_scheme', :size => 60 %></p>
+
+ <p><label for="public_body_notes">Public notes</label> <small>(HTML, for users to consider when making FOI requests to the authority)</small><br/>
+ <%= text_area object_name, 'notes', :rows => 3, :cols => 60 %></p>
+</div>
+<%
+ end
+%>
+
+<h3>Common Fields</h3>
<p><label for="public_body_tag_string">Tags <small>(space separated; see list of tags on the right; also <strong>not_apply</strong> if FOI and EIR no longer apply to authority, <strong>eir_only</strong> if EIR but not FOI applies to authority, <strong>defunct</strong> if the authority no longer exists; charity:NUMBER if a registered charity)</small></label><br/>
<%= text_field 'public_body', 'tag_string', :size => 60 %></p>
@@ -36,12 +57,6 @@
<p><label for="public_body_home_page">Home page <small>(of whole authority, not just their FOI page; set to <strong>blank</strong> (empty string) to guess it from the email)</small></label><br/>
<%= text_field 'public_body', 'home_page', :size => 60 %></p>
-<p><label for="public_body_publication_scheme">Publication scheme URL</label><br/>
-<%= text_field 'public_body', 'publication_scheme', :size => 60 %></p>
-
-<p><label for="public_body_notes">Public notes</label> <small>(HTML, for users to consider when making FOI requests to the authority)</small><br/>
-<%= text_area 'public_body', 'notes', :rows => 3, :cols => 60 %></p>
-
<p><label for="public_body_last_edit_comment"><strong>Comment</strong> for this edit</label> <small>(put URL or other source of new info)</small><br/>
<%= text_area 'public_body', 'last_edit_comment', :rows => 3, :cols => 60 %></p>
<!--[eoform:public_body]-->
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb
index 357564211..64eb45288 100644
--- a/spec/controllers/admin_public_body_controller_spec.rb
+++ b/spec/controllers/admin_public_body_controller_spec.rb
@@ -109,14 +109,6 @@ describe AdminPublicBodyController, "when administering public bodies with i18n"
get :show, {:id => 2, :locale => "es" }
end
- it "creates a new public body" do
- I18n.default_locale = :es
- PublicBody.count.should == 2
- post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' } }
- PublicBody.count.should == 3
- I18n.default_locale = :en
- end
-
it "edits a public body" do
I18n.default_locale = :es
get :edit, {:id => 3, :locale => :es}
@@ -148,3 +140,59 @@ describe AdminPublicBodyController, "when administering public bodies with i18n"
end
end
+
+describe AdminPublicBodyController, "when creating public bodies with i18n" do
+ integrate_views
+ fixtures :public_bodies, :public_body_translations
+
+ before do
+ username = MySociety::Config.get('ADMIN_USERNAME', '')
+ password = MySociety::Config.get('ADMIN_PASSWORD', '')
+ basic_auth_login @request
+ end
+
+ it "creates a new public body in one locale" do
+ PublicBody.count.should == 2
+ post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' } }
+ PublicBody.count.should == 3
+
+ body = PublicBody.find_by_name("New Quango")
+ response.should redirect_to("http://test.host/en/admin/body/show/#{body.id}")
+ end
+
+ it "creates a new public body with multiple locales" do
+ PublicBody.count.should == 2
+ post :create, {
+ :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' },
+ :public_body_es => { :name => "Nuevo Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' }
+ }
+ PublicBody.count.should == 3
+
+ body = PublicBody.find_by_name("New Quango")
+ body.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"]
+ PublicBody.with_locale(:en) do
+ body.name.should == "New Quango"
+ body.url_name.should == "new_quango"
+ end
+ PublicBody.with_locale(:es) do
+ body.name.should == "Nuevo Quango"
+ body.url_name.should == "nuevo_quango"
+ end
+
+ response.should redirect_to("http://test.host/en/admin/body/show/#{body.id}")
+ end
+
+ # when submitting the 'new' form, we should ignore a locale if the user hasn't set any value in it
+ it "doesn't create the public body if anything fails" do
+ PublicBody.count.should == 2
+ post :create, {
+ :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' },
+ :public_body_fr => { :name => "Neuf Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' },
+ :public_body_es => { :name => "" }, # invalid
+ }
+ response.should render_template('new')
+ PublicBody.count.should == 2
+ I18n.locale.should == :en # don't mess up the previous locale
+ end
+
+end