aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/info_request.rb8
-rw-r--r--app/models/public_body.rb241
-rw-r--r--app/models/public_body_category.rb5
-rw-r--r--app/models/track_thing.rb3
-rw-r--r--app/models/user.rb12
5 files changed, 129 insertions, 140 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 814057ef4..fd42ccd9c 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -187,11 +187,9 @@ class InfoRequest < ActiveRecord::Base
@@custom_states_loaded = false
begin
- if !Rails.env.test?
- require 'customstates'
- include InfoRequestCustomStates
- @@custom_states_loaded = true
- end
+ require 'customstates'
+ include InfoRequestCustomStates
+ @@custom_states_loaded = true
rescue MissingSourceFile, NameError
end
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index b8163b07d..0e90a3c16 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -235,39 +235,38 @@ class PublicBody < ActiveRecord::Base
return self.has_tag?('defunct')
end
- # Can an FOI (etc.) request be made to this body, and if not why not?
+ # Can an FOI (etc.) request be made to this body?
def is_requestable?
- if self.defunct?
- return false
- end
- if self.not_apply?
- return false
- end
- if self.request_email.nil?
- return false
- end
- return !self.request_email.empty? && self.request_email != 'blank'
+ has_request_email? && !defunct? && !not_apply?
end
+
# Strict superset of is_requestable?
def is_followupable?
- if self.request_email.nil?
- return false
- end
- return !self.request_email.empty? && self.request_email != 'blank'
+ has_request_email?
+ end
+
+ def has_request_email?
+ !request_email.blank? && request_email != 'blank'
end
+
# Also used as not_followable_reason
def not_requestable_reason
if self.defunct?
return 'defunct'
elsif self.not_apply?
return 'not_apply'
- elsif self.request_email.nil? or self.request_email.empty? or self.request_email == 'blank'
+ elsif !has_request_email?
return 'bad_contact'
else
- raise "requestable_failure_reason called with type that has no reason"
+ raise "not_requestable_reason called with type that has no reason"
end
end
+ def special_not_requestable_reason?
+ self.defunct? || self.not_apply?
+ end
+
+
class Version
def last_edit_comment_for_html_display
@@ -346,35 +345,6 @@ class PublicBody < ActiveRecord::Base
end
end
-
- # Use tags to describe what type of thing this is
- def type_of_authority(html = false)
- 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 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
-
- desc
- end
- end
-
- types.compact!
-
- if types.any?
- types.to_sentence(:last_word_connector => ' and ').html_safe
- else
- _("A public authority")
- end
- end
-
# Guess home page from the request email, or use explicit override, or nil
# if not known.
def calculated_home_page
@@ -454,8 +424,6 @@ class PublicBody < ActiveRecord::Base
def self.import_csv_from_file(csv_filename, tag, tag_behaviour, dry_run, editor, available_locales = [])
errors = []
notes = []
- available_locales = [I18n.default_locale] if available_locales.empty?
-
begin
ActiveRecord::Base.transaction do
# Use the default locale when retrieving existing bodies; otherwise
@@ -476,9 +444,18 @@ class PublicBody < ActiveRecord::Base
end
set_of_importing = Set.new()
- field_names = { 'name'=>1, 'request_email'=>2 } # Default values in case no field list is given
+ # Default values in case no field list is given
+ field_names = { 'name' => 1, 'request_email' => 2 }
line = 0
+ import_options = {:field_names => field_names,
+ :available_locales => available_locales,
+ :tag => tag,
+ :tag_behaviour => tag_behaviour,
+ :editor => editor,
+ :notes => notes,
+ :errors => errors }
+
CSV.foreach(csv_filename) do |row|
line = line + 1
@@ -490,7 +467,7 @@ class PublicBody < ActiveRecord::Base
end
fields = {}
- field_names.each{|name, i| fields[name] = row[i]}
+ field_names.each{ |name, i| fields[name] = row[i] }
yield line, fields if block_given?
@@ -506,83 +483,11 @@ class PublicBody < ActiveRecord::Base
next
end
- field_list = []
- self.csv_import_fields.each do |field_name, field_notes|
- field_list.push field_name
- end
-
- if public_body = bodies_by_name[name] # Existing public body
- available_locales.each do |locale|
- I18n.with_locale(locale) do
- changed = ActiveSupport::OrderedHash.new
- 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?
- localized_value = tag unless tag.empty?
- else
- if tag_behaviour == 'add'
- localized_value = "#{localized_value} #{tag}" unless tag.empty?
- 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)
- end
- end
-
- 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.save!
- end
- end
- end
- else # New public body
- public_body = PublicBody.new(:name=>"", :short_name=>"", :request_email=>"")
- available_locales.each do |locale|
- I18n.with_locale(locale) do
- changed = ActiveSupport::OrderedHash.new
- 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]]
-
- 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)
- end
- end
-
- unless changed.empty?
- 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'
-
- begin
- public_body.save!
- rescue ActiveRecord::RecordInvalid
- public_body.errors.full_messages.each do |msg|
- errors.push "error: line #{ line }: #{ msg } for authority '#{ name }'"
- end
- next
- end
- end
- end
- end
- end
+ public_body = bodies_by_name[name] || PublicBody.new(:name => "",
+ :short_name => "",
+ :request_email => "")
+ public_body.import_values_from_csv_row(row, line, name, import_options)
set_of_importing.add(name)
end
@@ -604,6 +509,77 @@ class PublicBody < ActiveRecord::Base
return [errors, notes]
end
+ def self.localized_csv_field_name(locale, field_name)
+ (locale.to_s == I18n.default_locale.to_s) ? field_name : "#{field_name}.#{locale}"
+ end
+
+
+ # import values from a csv row (that may include localized columns)
+ def import_values_from_csv_row(row, line, name, options)
+ is_new = new_record?
+ edit_info = if is_new
+ { :action => "creating new authority",
+ :comment => 'Created from spreadsheet' }
+ else
+ { :action => "updating authority",
+ :comment => 'Updated from spreadsheet' }
+ end
+ locales = options[:available_locales]
+ locales = [I18n.default_locale] if locales.empty?
+ locales.each do |locale|
+ I18n.with_locale(locale) do
+ changed = set_locale_fields_from_csv_row(is_new, locale, row, options)
+ unless changed.empty?
+ options[:notes].push "line #{ line }: #{ edit_info[:action] } '#{ name }' (locale: #{ locale }):\n\t#{ changed.to_json }"
+ self.last_edit_comment = edit_info[:comment]
+ self.publication_scheme = publication_scheme || ""
+ self.last_edit_editor = options[:editor]
+
+ begin
+ save!
+ rescue ActiveRecord::RecordInvalid
+ errors.full_messages.each do |msg|
+ options[:errors].push "error: line #{ line }: #{ msg } for authority '#{ name }'"
+ end
+ next
+ end
+ end
+ end
+ end
+ end
+
+ # Sets attribute values for a locale from a csv row
+ def set_locale_fields_from_csv_row(is_new, locale, row, options)
+ changed = ActiveSupport::OrderedHash.new
+ csv_field_names = options[:field_names]
+ csv_import_fields.each do |field_name, field_notes|
+ localized_field_name = self.class.localized_csv_field_name(locale, field_name)
+ column = csv_field_names[localized_field_name]
+ value = column && row[column]
+ # Tags are a special case, as we support adding to the field, not just setting a new value
+ if field_name == 'tag_string'
+ new_tags = [value, options[:tag]].select{ |new_tag| !new_tag.blank? }
+ if new_tags.empty?
+ value = nil
+ else
+ value = new_tags.join(" ")
+ value = "#{value} #{tag_string}"if options[:tag_behaviour] == 'add'
+ end
+
+ end
+
+ if value and read_attribute_value(field_name, locale) != value
+ if is_new
+ changed[field_name] = value
+ else
+ changed[field_name] = "#{read_attribute_value(field_name, locale)}: #{value}"
+ end
+ assign_attributes({ field_name => value })
+ end
+ end
+ changed
+ end
+
# Does this user have the power of FOI officer for this body?
def is_foi_officer?(user)
user_domain = user.email_domain
@@ -802,6 +778,19 @@ class PublicBody < ActiveRecord::Base
private
+ # Read an attribute value (without using locale fallbacks if the attribute is translated)
+ def read_attribute_value(name, locale)
+ if self.class.translates.include?(name.to_sym)
+ if globalize.stash.contains?(locale, name)
+ globalize.stash.read(locale, name)
+ else
+ translation_for(locale).send(name)
+ end
+ else
+ send(name)
+ end
+ end
+
def request_email_if_requestable
# Request_email can be blank, meaning we don't have details
if self.is_requestable?
diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb
index c313e5734..198e8b737 100644
--- a/app/models/public_body_category.rb
+++ b/app/models/public_body_category.rb
@@ -49,11 +49,6 @@ class PublicBodyCategory < ActiveRecord::Base
PublicBodyCategory.find_by_sql(sql)
end
- # Called from the old-style public_body_categories_[locale].rb data files
- def self.add(locale, data_list)
- CategoryAndHeadingMigrator.add_categories_and_headings_from_list(locale, data_list)
- end
-
# Convenience methods for creating/editing translations via forms
def find_translation_by_locale(locale)
translations.find_by_locale(locale)
diff --git a/app/models/track_thing.rb b/app/models/track_thing.rb
index 5819876ff..cd90c4a9e 100644
--- a/app/models/track_thing.rb
+++ b/app/models/track_thing.rb
@@ -231,8 +231,7 @@ class TrackThing < ActiveRecord::Base
{ # Website
:verb_on_page => _("Follow requests to {{public_body_name}}",
:public_body_name => public_body.name),
- :verb_on_page_already => _("You are already following requests to {{public_body_name}}",
- :public_body_name => public_body.name),
+ :verb_on_page_already => _("Following"),
# Email
:title_in_email => _("{{foi_law}} requests to '{{public_body_name}}'",
:foi_law => public_body.law_only_short,
diff --git a/app/models/user.rb b/app/models/user.rb
index c953e52f2..920c0da46 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -207,7 +207,7 @@ class User < ActiveRecord::Base
if not name.nil?
name.strip!
end
- if public_banned?
+ if banned?
# Use interpolation to return a string rather than a SafeBuffer so that
# gsub can be called on it until we upgrade to Rails 3.2. The name returned
# is not marked as HTML safe so will be escaped automatically in views. We
@@ -294,10 +294,18 @@ class User < ActiveRecord::Base
def admin_page_links?
super?
end
+
# Is it public that they are banned?
+ def banned?
+ !ban_text.empty?
+ end
+
def public_banned?
- !ban_text.empty?
+ warn %q([DEPRECATION] User#public_banned? will be replaced with
+ User#banned? as of 0.22).squish
+ banned?
end
+
# Various ways the user can be banned, and text to describe it if failed
def can_file_requests?
ban_text.empty? && !exceeded_limit?