aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/concerns/public_body_derived_fields.rb47
-rw-r--r--app/models/public_body.rb40
-rw-r--r--spec/controllers/admin_public_body_controller_spec.rb5
-rw-r--r--spec/models/public_body_spec.rb25
4 files changed, 84 insertions, 33 deletions
diff --git a/app/models/concerns/public_body_derived_fields.rb b/app/models/concerns/public_body_derived_fields.rb
new file mode 100644
index 000000000..f389e3cbf
--- /dev/null
+++ b/app/models/concerns/public_body_derived_fields.rb
@@ -0,0 +1,47 @@
+module PublicBodyDerivedFields
+
+ extend ActiveSupport::Concern
+
+ included do
+ before_save :set_first_letter
+
+ # When name or short name is changed, also change the url name
+ def short_name=(short_name)
+ super
+ update_url_name
+ end
+
+ def name=(name)
+ super
+ update_url_name
+ end
+
+ end
+
+ # Return the short name if present, or else long name
+ def short_or_long_name
+ if self.short_name.nil? || self.short_name.empty?
+ self.name.nil? ? "" : self.name
+ else
+ self.short_name
+ end
+ end
+
+ # Set the first letter, which is used for faster queries
+ def set_first_letter
+ unless name.blank?
+ # we use a regex to ensure it works with utf-8/multi-byte
+ first_letter = Unicode.upcase name.scan(/^./mu)[0]
+ if first_letter != self.first_letter
+ self.first_letter = first_letter
+ end
+ end
+ end
+
+ def update_url_name
+ if changed.include?('name') || changed.include?('short_name')
+ self.url_name = MySociety::Format.simplify_url_part(self.short_or_long_name, 'body')
+ end
+ end
+
+end
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index b648ded0f..33be49052 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -55,11 +55,11 @@ class PublicBody < ActiveRecord::Base
has_tag_string
before_save :set_api_key,
- :set_default_publication_scheme,
- :set_first_letter
+ :set_default_publication_scheme
after_save :purge_in_cache
after_update :reindex_requested_from
+
# Every public body except for the internal admin one is visible
scope :visible, lambda {
{
@@ -70,6 +70,11 @@ class PublicBody < ActiveRecord::Base
translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme
accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params?
+ include PublicBodyDerivedFields
+ class Translation
+ include PublicBodyDerivedFields
+ end
+
# Default fields available for importing from CSV, in the format
# [field_name, 'short description of field (basic html allowed)']
cattr_accessor :csv_import_fields do
@@ -207,10 +212,7 @@ class PublicBody < ActiveRecord::Base
return PublicBody.find(old.first)
end
- # Set the first letter, which is used for faster queries
- def set_first_letter
- PublicBody.set_first_letter(self)
- end
+
# If tagged "not_apply", then FOI/EIR no longer applies to authority at all
def not_apply?
@@ -305,32 +307,6 @@ class PublicBody < ActiveRecord::Base
end
end
- # When name or short name is changed, also change the url name
- def short_name=(short_name)
- globalize.write(Globalize.locale, :short_name, short_name)
- self[:short_name] = short_name
- self.update_url_name
- end
-
- def name=(name)
- globalize.write(Globalize.locale, :name, name)
- self[:name] = name
- self.update_url_name
- end
-
- def update_url_name
- self.url_name = MySociety::Format.simplify_url_part(self.short_or_long_name, 'body')
- end
-
- # Return the short name if present, or else long name
- def short_or_long_name
- if self.short_name.nil? || self.short_name.empty? # 'nil' can happen during construction
- self.name.nil? ? "" : self.name
- else
- self.short_name
- end
- end
-
# Guess home page from the request email, or use explicit override, or nil
# if not known.
def calculated_home_page
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb
index 50a373d9d..789a3d3e3 100644
--- a/spec/controllers/admin_public_body_controller_spec.rb
+++ b/spec/controllers/admin_public_body_controller_spec.rb
@@ -128,7 +128,8 @@ describe AdminPublicBodyController, "when creating a public body" do
:last_edit_comment => 'From test code',
:translations_attributes => {
'es' => { :locale => 'es',
- :name => 'Los Quango' }
+ :name => 'Los Quango',
+ :short_name => 'lq' }
} } }
end
@@ -159,6 +160,8 @@ describe AdminPublicBodyController, "when creating a public body" do
I18n.with_locale(:es) do
expect(body.name).to eq('Los Quango')
+ expect(body.url_name).to eq('lq')
+ expect(body.first_letter).to eq('L')
end
end
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index 7b55efda1..8d584e5e1 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -282,6 +282,31 @@ describe PublicBody, " when saving" do
pb.first_letter.should == 'Å'
end
+ it 'should save the first letter of a translation' do
+ existing = FactoryGirl.create(:public_body, :first_letter => 'T', :name => 'Test body')
+ I18n.with_locale(:es) { existing.update_attributes :name => 'Prueba body' }
+ PublicBody::Translation.
+ where(:public_body_id => existing.id, :locale => :es).
+ pluck('first_letter').first.should == 'P'
+ end
+
+ it 'should save the first letter of a translation, even when it is the same as the
+ first letter in the default locale' do
+ existing = FactoryGirl.create(:public_body, :first_letter => 'T', :name => 'Test body')
+ I18n.with_locale(:es) { existing.update_attributes :name => existing.name }
+ PublicBody::Translation.
+ where(:public_body_id => existing.id, :locale => :es).
+ pluck('first_letter').first.should == 'T'
+ end
+
+ it 'should create a url_name for a translation' do
+ existing = FactoryGirl.create(:public_body, :first_letter => 'T', :short_name => 'Test body')
+ I18n.with_locale(:es) do
+ existing.update_attributes :short_name => 'Prueba', :name => 'Prueba body'
+ existing.url_name.should == 'prueba'
+ end
+ end
+
it "should not save if the url_name is already taken" do
existing = FactoryGirl.create(:public_body)
pb = PublicBody.new(existing.attributes)