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.rb98
1 files changed, 60 insertions, 38 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 90e9395ae..267b5d60c 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -1,5 +1,5 @@
# == Schema Information
-# Schema version: 95
+# Schema version: 114
#
# Table name: public_bodies
#
@@ -58,11 +58,11 @@ class PublicBody < ActiveRecord::Base
short_long_name = t.short_name if t.short_name and !t.short_name.empty?
t.url_name = MySociety::Format.simplify_url_part(short_long_name, 'body')
end
-
+
def translated_versions
translations
end
-
+
def translated_versions=(translation_attrs)
def skip?(attrs)
valueless = attrs.inject({}) { |h, (k, v)| h[k] = v if v != '' and k != 'locale'; h } # because we want to fall back to alternative translations where there are empty values
@@ -86,7 +86,7 @@ class PublicBody < ActiveRecord::Base
end
end
end
-
+
# Make sure publication_scheme gets the correct default value.
# (This would work automatically, were publication_scheme not a translated attribute)
def after_initialize
@@ -96,7 +96,7 @@ class PublicBody < ActiveRecord::Base
# like find_by_url_name but also search historic url_name if none found
def self.find_by_url_name_with_historic(name)
locale = self.locale || I18n.locale
- PublicBody.with_locale(locale) do
+ PublicBody.with_locale(locale) do
found = PublicBody.find(:all,
:conditions => ["public_body_translations.url_name='#{name}'"],
:joins => :translations,
@@ -189,18 +189,37 @@ class PublicBody < ActiveRecord::Base
text = text.gsub(/\n/, '<br>')
return text
end
+
+ def compare(previous = nil)
+ if previous.nil?
+ yield([])
+ else
+ v = self
+ changes = self.class.content_columns.inject([]) {|memo, c|
+ unless %w(version last_edit_editor last_edit_comment updated_at).include?(c.name)
+ from = previous.send(c.name)
+ to = self.send(c.name)
+ memo << { :name => c.human_name, :from => from, :to => to } if from != to
+ end
+ memo
+ }
+ changes.each do |change|
+ yield(change)
+ end
+ end
+ end
end
acts_as_xapian :texts => [ :name, :short_name, :notes ],
- :values => [
+ :values => [
[ :created_at_numeric, 1, "created_at", :number ] # for sorting
],
:terms => [ [ :variety, 'V', "variety" ],
- [ :tag_array_for_search, 'U', "tag" ]
+ [ :tag_array_for_search, 'U', "tag" ]
]
def created_at_numeric
# format it here as no datetime support in Xapian's value ranges
- return self.created_at.strftime("%Y%m%d%H%M%S")
+ return self.created_at.strftime("%Y%m%d%H%M%S")
end
def variety
return "authority"
@@ -236,7 +255,7 @@ class PublicBody < ActiveRecord::Base
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
@@ -253,7 +272,7 @@ class PublicBody < ActiveRecord::Base
first = true
for tag in self.tags
if PublicBodyCategories::get().by_tag().include?(tag.name)
- desc = PublicBodyCategories::get().singular_by_tag()[tag.name]
+ desc = PublicBodyCategories::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)
@@ -270,7 +289,7 @@ class PublicBody < ActiveRecord::Base
if types.size > 0
ret = types[0, types.size - 1].join(", ")
if types.size > 1
- ret = ret + " and "
+ ret = ret + " and "
end
ret = ret + types[-1]
return ret
@@ -282,19 +301,11 @@ class PublicBody < ActiveRecord::Base
# Guess home page from the request email, or use explicit override, or nil
# if not known.
def calculated_home_page
- # manual override for ones we calculate wrongly
- if self.home_page != ''
- return self.home_page
- end
-
- # extract the domain name from the FOI request email
- url = self.request_email_domain
- if url.nil?
- return nil
+ if home_page && !home_page.empty?
+ home_page[URI::regexp(%w(http https))] ? home_page : "http://#{home_page}"
+ elsif request_email_domain
+ "http://www.#{request_email_domain}"
end
-
- # add standard URL prefix
- return "http://www." + url
end
# Are all requests to this body under the Environmental Information Regulations?
@@ -359,12 +370,12 @@ class PublicBody < ActiveRecord::Base
for existing_body in bodies
# Hide InternalAdminBody from import notes
next if existing_body.id == PublicBody.internal_admin_body.id
-
+
bodies_by_name[existing_body.name] = existing_body
set_of_existing.add(existing_body.name)
end
end
-
+
set_of_importing = Set.new()
field_names = { 'name'=>1, 'request_email'=>2 } # Default values in case no field list is given
line = 0
@@ -380,7 +391,7 @@ class PublicBody < ActiveRecord::Base
fields = {}
field_names.each{|name, i| fields[name] = row[i]}
-
+
name = row[field_names['name']]
email = row[field_names['request_email']]
next if name.nil?
@@ -392,7 +403,7 @@ class PublicBody < ActiveRecord::Base
errors.push "error: line #{line.to_s}: invalid email '#{email}' for authority '#{name}'"
next
end
-
+
field_list = ['name', 'short_name', 'request_email', 'notes', 'publication_scheme', 'home_page', 'tag_string']
if public_body = bodies_by_name[name] # Existing public body
@@ -402,7 +413,7 @@ class PublicBody < ActiveRecord::Base
field_list.each do |field_name|
localized_field_name = (locale.to_s == I18n.default_locale.to_s) ? field_name : "#{field_name}.#{locale}"
localized_value = field_names[localized_field_name] && row[field_names[localized_field_name]]
-
+
# Tags are a special case, as we support adding to the field, not just setting a new value
if localized_field_name == 'tag_string'
if localized_value.nil?
@@ -410,11 +421,11 @@ class PublicBody < ActiveRecord::Base
else
if tag_behaviour == 'add'
localized_value = "#{localized_value} #{tag}" unless tag.empty?
- localized_value = "#{localized_value} #{public_body.tag_string}"
+ localized_value = "#{localized_value} #{public_body.tag_string}"
end
end
end
-
+
if !localized_value.nil? and public_body.send(field_name) != localized_value
changed[field_name] = "#{public_body.send(field_name)}: #{localized_value}"
public_body.send("#{field_name}=", localized_value)
@@ -424,14 +435,14 @@ class PublicBody < ActiveRecord::Base
unless changed.empty?
notes.push "line #{line.to_s}: updating authority '#{name}' (locale: #{locale}):\n\t#{changed.to_json}"
public_body.last_edit_editor = editor
- public_body.last_edit_comment = 'Updated from spreadsheet'
+ public_body.last_edit_comment = 'Updated from spreadsheet'
public_body.save!
end
end
end
else # New public body
public_body = PublicBody.new(:name=>"", :short_name=>"", :request_email=>"")
- available_locales.each do |locale|
+ available_locales.each do |locale|
PublicBody.with_locale(locale) do
changed = ActiveSupport::OrderedHash.new
field_list.each do |field_name|
@@ -441,7 +452,7 @@ class PublicBody < ActiveRecord::Base
if localized_field_name == 'tag_string' and tag_behaviour == 'add'
localized_value = "#{localized_value} #{tag}" unless tag.empty?
end
-
+
if !localized_value.nil? and public_body.send(field_name) != localized_value
changed[field_name] = localized_value
public_body.send("#{field_name}=", localized_value)
@@ -452,7 +463,7 @@ class PublicBody < ActiveRecord::Base
notes.push "line #{line.to_s}: creating new authority '#{name}' (locale: #{locale}):\n\t#{changed.to_json}"
public_body.publication_scheme = public_body.publication_scheme || ""
public_body.last_edit_editor = editor
- public_body.last_edit_comment = 'Created from spreadsheet'
+ public_body.last_edit_comment = 'Created from spreadsheet'
public_body.save!
end
end
@@ -462,7 +473,7 @@ class PublicBody < ActiveRecord::Base
set_of_importing.add(name)
end
- # Give an error listing ones that are to be deleted
+ # Give an error listing ones that are to be deleted
deleted_ones = set_of_existing - set_of_importing
if deleted_ones.size > 0
notes.push "Notes: Some " + tag + " bodies are in database, but not in CSV file:\n " + Array(deleted_ones).sort.join("\n ") + "\nYou may want to delete them manually.\n"
@@ -527,7 +538,7 @@ class PublicBody < ActiveRecord::Base
end
def has_notes?
- return self.notes != ""
+ return !self.notes.nil? && self.notes != ""
end
def notes_as_html
self.notes
@@ -538,7 +549,7 @@ class PublicBody < ActiveRecord::Base
end
def json_for_api
- return {
+ return {
:id => self.id,
:url_name => self.url_name,
:name => self.name,
@@ -546,7 +557,7 @@ class PublicBody < ActiveRecord::Base
# :request_email # we hide this behind a captcha, to stop people doing bulk requests easily
:created_at => self.created_at,
:updated_at => self.updated_at,
- # don't add the history as some edit comments contain sensitive information
+ # don't add the history as some edit comments contain sensitive information
# :version, :last_edit_editor, :last_edit_comment
:home_page => self.calculated_home_page,
:notes => self.notes,
@@ -555,6 +566,17 @@ class PublicBody < ActiveRecord::Base
}
end
+ after_save(:purge_in_cache)
+ def purge_in_cache
+ self.info_requests.each {|x| x.purge_in_cache}
+ end
+
+ def for_admin_column
+ self.class.content_columns.map{|c| c unless %w(name last_edit_comment).include?(c.name)}.compact.each do |column|
+ yield(column.human_name, self.send(column.name), column.type.to_s, column.name)
+ end
+ end
+
end