aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/public_body.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/public_body.rb')
-rw-r--r--app/models/public_body.rb49
1 files changed, 29 insertions, 20 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index a9cdfeab2..b8163b07d 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -64,6 +64,7 @@ class PublicBody < ActiveRecord::Base
}
translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme
+ accepts_nested_attributes_for :translations
# Default fields available for importing from CSV, in the format
# [field_name, 'short description of field (basic html allowed)']
@@ -151,12 +152,11 @@ class PublicBody < ActiveRecord::Base
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)
@@ -166,6 +166,13 @@ class PublicBody < ActiveRecord::Base
t.save!
end
else # Array => creating
+ warn "[DEPRECATION] PublicBody#translations_attributes= " \
+ "will no longer accept an Array as of release 0.22. " \
+ "Use Hash arguments instead. See " \
+ "spec/models/public_body_spec.rb and " \
+ "app/views/admin_public_body/_form.html.erb for more " \
+ "details."
+
translation_attrs.each do |attrs|
next if empty_translation?(attrs)
new_translation = PublicBody::Translation.new(attrs)
@@ -175,6 +182,12 @@ class PublicBody < ActiveRecord::Base
end
end
+ def translated_versions=(translation_attrs)
+ warn "[DEPRECATION] PublicBody#translated_versions= will be replaced " \
+ "by PublicBody#translations_attributes= as of release 0.22"
+ self.translations_attributes = translation_attrs
+ end
+
def set_default_publication_scheme
# Make sure publication_scheme gets the correct default value.
# (This would work automatically, were publication_scheme not a translated attribute)
@@ -336,33 +349,29 @@ class PublicBody < ActiveRecord::Base
# Use tags to describe what type of thing this is
def type_of_authority(html = false)
- types = []
- first = true
- for tag in self.tags
+ types = tags.each_with_index.map do |tag, index|
if PublicBodyCategory.get().by_tag().include?(tag.name)
desc = PublicBodyCategory.get().singular_by_tag()[tag.name]
- if first
- # terrible that Ruby/Rails doesn't have an equivalent of ucfirst
- # (capitalize shockingly converts later characters to lowercase)
- desc = desc[0,1].capitalize + desc[1,desc.size]
- first = false
+
+ if index.zero?
+ desc = desc.sub(/\S/) { |m| Unicode.upcase(m) }
end
+
if html
# TODO: this should call proper route helpers, but is in model sigh
desc = '<a href="/body/list/' + tag.name + '">' + desc + '</a>'
end
- types.push(desc)
+
+ desc
end
end
- if types.size > 0
- ret = types[0, types.size - 1].join(", ")
- if types.size > 1
- ret = ret + " and "
- end
- ret = ret + types[-1]
- return ret.html_safe
+
+ types.compact!
+
+ if types.any?
+ types.to_sentence(:last_word_connector => ' and ').html_safe
else
- return _("A public authority")
+ _("A public authority")
end
end