aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/censor_rule.rb5
-rw-r--r--app/models/info_request.rb68
-rw-r--r--app/models/info_request_event.rb8
-rw-r--r--app/models/outgoing_message.rb47
-rw-r--r--app/models/post_redirect.rb6
-rw-r--r--app/models/public_body.rb36
-rw-r--r--app/models/user.rb1
-rw-r--r--app/models/widget_vote.rb19
8 files changed, 137 insertions, 53 deletions
diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb
index 3b5c2d805..58170f237 100644
--- a/app/models/censor_rule.rb
+++ b/app/models/censor_rule.rb
@@ -42,6 +42,11 @@ class CensorRule < ActiveRecord::Base
:user_id => nil,
:public_body_id => nil } }
+ def apply_to_text(text_to_censor)
+ return nil if text_to_censor.nil?
+ text_to_censor.gsub(to_replace, replacement)
+ end
+
def apply_to_text!(text_to_censor)
return nil if text_to_censor.nil?
text_to_censor.gsub!(to_replace, replacement)
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index c203f75c3..02faffcfa 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -50,6 +50,7 @@ class InfoRequest < ActiveRecord::Base
has_many :info_request_events, :order => 'created_at'
has_many :user_info_request_sent_alerts
has_many :track_things, :order => 'created_at desc'
+ has_many :widget_votes
has_many :comments, :order => 'created_at'
has_many :censor_rules, :order => 'created_at desc'
has_many :mail_server_logs, :order => 'mail_server_log_done_id'
@@ -783,7 +784,14 @@ public
end
def public_response_events
- self.info_request_events.select{|e| e.response? && e.incoming_message.all_can_view? }
+ condition = <<-SQL
+ info_request_events.event_type = ?
+ AND incoming_messages.prominence = ?
+ SQL
+
+ info_request_events.
+ joins(:incoming_message).
+ where(condition, 'response', 'normal')
end
# The last public response is the default one people might want to reply to
@@ -810,8 +818,9 @@ public
# Text from the the initial request, for use in summary display
def initial_request_text
- return '' if outgoing_messages.empty? # mainly for use with incomplete fixtures
- outgoing_messages.first.get_text_for_indexing
+ return '' if outgoing_messages.empty?
+ body_opts = { :censor_rules => applicable_censor_rules }
+ outgoing_messages.first.try(:get_text_for_indexing, true, body_opts) or ''
end
# Returns index of last event which is described or nil if none described.
@@ -908,21 +917,24 @@ public
# Completely delete this request and all objects depending on it
def fully_destroy
- self.track_things.each do |track_thing|
+ track_things.each do |track_thing|
track_thing.track_things_sent_emails.each { |a| a.destroy }
track_thing.destroy
end
- self.user_info_request_sent_alerts.each { |a| a.destroy }
- self.info_request_events.each do |info_request_event|
+ user_info_request_sent_alerts.each { |a| a.destroy }
+ info_request_events.each do |info_request_event|
info_request_event.track_things_sent_emails.each { |a| a.destroy }
info_request_event.destroy
end
- self.mail_server_logs.each do |mail_server_log|
+ mail_server_logs.each do |mail_server_log|
mail_server_log.destroy
end
- self.outgoing_messages.each { |a| a.destroy }
- self.incoming_messages.each { |a| a.destroy }
- self.destroy
+ outgoing_messages.each { |a| a.destroy }
+ incoming_messages.each { |a| a.destroy }
+ comments.each { |comment| comment.destroy }
+ censor_rules.each{ |censor_rule| censor_rule.destroy }
+
+ destroy
end
# Called by incoming_email - and used to be called to generate separate
@@ -1362,6 +1374,39 @@ public
order('last_event_time')
end
+ def move_to_public_body(destination_public_body, opts = {})
+ old_body = public_body
+ editor = opts.fetch(:editor)
+
+ attrs = { :public_body => destination_public_body }
+
+ if destination_public_body
+ attrs.merge!({
+ :law_used => destination_public_body.law_only_short.downcase
+ })
+ end
+
+ if update_attributes(attrs)
+ log_event('move_request',
+ :editor => editor,
+ :public_body_url_name => public_body.url_name,
+ :old_public_body_url_name => old_body.url_name)
+
+ reindex_request_events
+
+ public_body
+ end
+ end
+
+ # The DateTime of the last InfoRequestEvent belonging to the InfoRequest
+ # Only available if the last_event_time attribute has been set. This is
+ # currentlt only set through .find_in_state
+ #
+ # Returns a DateTime
+ def last_event_time
+ attributes['last_event_time'].try(:to_datetime)
+ end
+
private
def set_defaults
@@ -1373,8 +1418,9 @@ public
# this should only happen on Model.exists?() call. It can be safely ignored.
# See http://www.tatvartha.com/2011/03/activerecordmissingattributeerror-missing-attribute-a-bug-or-a-features/
end
+
# FOI or EIR?
- if !self.public_body.nil? && self.public_body.eir_only?
+ if new_record? && public_body && public_body.eir_only?
self.law_used = 'eir'
end
end
diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb
index 635ba8f58..0ee82d30c 100644
--- a/app/models/info_request_event.rb
+++ b/app/models/info_request_event.rb
@@ -278,9 +278,15 @@ class InfoRequestEvent < ActiveRecord::Base
end
self.params_yaml = params.to_yaml
end
+
def params
- YAML.load(self.params_yaml)
+ param_hash = YAML.load(params_yaml)
+ param_hash.each do |key, value|
+ param_hash[key] = value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
+ end
+ param_hash
end
+
def params_yaml_as_html
ret = ''
# split out parameters into old/new diffs, and other ones
diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb
index c2c8ef4f2..4f6318b3d 100644
--- a/app/models/outgoing_message.rb
+++ b/app/models/outgoing_message.rb
@@ -140,22 +140,28 @@ class OutgoingMessage < ActiveRecord::Base
end
end
- def body
- ret = read_attribute(:body)
- if ret.nil?
- return ret
+ # Public: The body text of the OutgoingMessage. The text is cleaned and
+ # CensorRules are applied.
+ #
+ # options - Hash of options
+ # :censor_rules - Array of CensorRules to apply. Defaults to the
+ # applicable_censor_rules of the associated
+ # InfoRequest. (optional)
+ #
+ # Returns a String
+ def body(options = {})
+ text = raw_body.dup
+ return text if text.nil?
+
+ text = clean_text(text)
+
+ # Use the given censor_rules; otherwise fetch them from the associated
+ # info_request
+ censor_rules = options.fetch(:censor_rules) do
+ info_request.try(:applicable_censor_rules) or []
end
- ret = ret.dup
- ret.strip!
- ret.gsub!(/(?:\n\s*){2,}/, "\n\n") # remove excess linebreaks that unnecessarily space it out
-
- # Remove things from censor rules
- unless info_request.nil?
- self.info_request.apply_censor_rules_to_text!(ret)
- end
-
- ret
+ censor_rules.reduce(text) { |text, rule| rule.apply_to_text(text) }
end
def raw_body
@@ -227,8 +233,12 @@ class OutgoingMessage < ActiveRecord::Base
end
# Returns text for indexing / text display
- def get_text_for_indexing(strip_salutation = true)
- text = body.strip
+ def get_text_for_indexing(strip_salutation = true, opts = {})
+ if opts.empty?
+ text = body.strip
+ else
+ text = body(opts).strip
+ end
# Remove salutation
text.sub!(/Dear .+,/, "") if strip_salutation
@@ -332,6 +342,11 @@ class OutgoingMessage < ActiveRecord::Base
errors.add(:what_doing_dummy, _('Please choose what sort of reply you are making.'))
end
end
+
+ # remove excess linebreaks that unnecessarily space it out
+ def clean_text(text)
+ text.strip.gsub(/(?:\n\s*){2,}/, "\n\n")
+ end
end
diff --git a/app/models/post_redirect.rb b/app/models/post_redirect.rb
index 8049349d0..59160381c 100644
--- a/app/models/post_redirect.rb
+++ b/app/models/post_redirect.rb
@@ -71,7 +71,11 @@ class PostRedirect < ActiveRecord::Base
end
def reason_params
- YAML.load(reason_params_yaml)
+ param_hash = YAML.load(reason_params_yaml)
+ param_hash.each do |key, value|
+ param_hash[key] = value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
+ end
+ param_hash
end
# Extract just local path part, without domain or #
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 232c0ffa1..dac77d4c5 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -339,19 +339,20 @@ class PublicBody < ActiveRecord::Base
# Are all requests to this body under the Environmental Information Regulations?
def eir_only?
- return self.has_tag?('eir_only')
+ has_tag?('eir_only')
end
+
def law_only_short
- if self.eir_only?
- return "EIR"
- else
- return "FOI"
- end
+ eir_only? ? 'EIR' : 'FOI'
end
# Schools are allowed more time in holidays, so we change some wordings
def is_school?
- return self.has_tag?('school')
+ has_tag?('school')
+ end
+
+ def site_administration?
+ has_tag?('site_administration')
end
# The "internal admin" is a special body for internal use.
@@ -379,10 +380,6 @@ class PublicBody < ActiveRecord::Base
end
end
- def site_administration?
- has_tag?('site_administration')
- end
-
class ImportCSVDryRun < StandardError
end
@@ -577,17 +574,11 @@ class PublicBody < ActiveRecord::Base
return self.request_email_domain
end
- # Returns nil if configuration variable not set
- def override_request_email
- e = AlaveteliConfiguration::override_all_public_body_request_emails
- e if e != ""
- end
-
def request_email
- if override_request_email
- override_request_email
- else
+ if AlaveteliConfiguration::override_all_public_body_request_emails.blank? || read_attribute(:request_email).blank?
read_attribute(:request_email)
+ else
+ AlaveteliConfiguration::override_all_public_body_request_emails
end
end
@@ -774,10 +765,7 @@ class PublicBody < ActiveRecord::Base
end
def empty_translation_in_params?(attributes)
- attrs_with_values = attributes.select do |key, value|
- value != '' and key.to_s != 'locale'
- end
- attrs_with_values.empty?
+ attributes.select { |k, v| !v.blank? && k.to_s != 'locale' }.empty?
end
def request_email_if_requestable
diff --git a/app/models/user.rb b/app/models/user.rb
index 920c0da46..1fb5d9139 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -38,6 +38,7 @@ class User < ActiveRecord::Base
has_one :profile_photo
has_many :censor_rules, :order => 'created_at desc'
has_many :info_request_batches, :order => 'created_at desc'
+ has_many :request_classifications
validates_presence_of :email, :message => _("Please enter your email address")
validates_presence_of :name, :message => _("Please enter your name")
diff --git a/app/models/widget_vote.rb b/app/models/widget_vote.rb
new file mode 100644
index 000000000..021c38b30
--- /dev/null
+++ b/app/models/widget_vote.rb
@@ -0,0 +1,19 @@
+# == Schema Information
+#
+# Table name: widget_votes
+#
+# id :integer not null, primary key
+# cookie :string(255)
+# info_request_id :integer not null
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+class WidgetVote < ActiveRecord::Base
+ belongs_to :info_request
+ validates :info_request, :presence => true
+
+ attr_accessible :cookie
+ validates :cookie, :length => { :is => 20 }
+ validates_uniqueness_of :cookie, :scope => :info_request_id
+end