aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/about_me_validator.rb8
-rw-r--r--app/models/change_email_validator.rb5
-rw-r--r--app/models/comment.rb39
-rw-r--r--app/models/contact_mailer.rb2
-rw-r--r--app/models/incoming_message.rb2
-rw-r--r--app/models/outgoing_message.rb54
-rw-r--r--app/models/profile_photo.rb6
-rw-r--r--app/models/public_body.rb19
-rw-r--r--app/models/user.rb20
9 files changed, 87 insertions, 68 deletions
diff --git a/app/models/about_me_validator.rb b/app/models/about_me_validator.rb
index 5e04c2761..8ee505ac8 100644
--- a/app/models/about_me_validator.rb
+++ b/app/models/about_me_validator.rb
@@ -17,10 +17,14 @@ class AboutMeValidator < ActiveRecord::BaseWithoutTable
column :about_me, :text, "I...", false
- def validate
+ # TODO: Switch to built in validations
+ validate :length_of_about_me
+
+ private
+
+ def length_of_about_me
if !self.about_me.blank? && self.about_me.size > 500
errors.add(:about_me, _("Please keep it shorter than 500 characters"))
end
end
-
end
diff --git a/app/models/change_email_validator.rb b/app/models/change_email_validator.rb
index 9ef25217d..2ddebb177 100644
--- a/app/models/change_email_validator.rb
+++ b/app/models/change_email_validator.rb
@@ -28,12 +28,15 @@ class ChangeEmailValidator < ActiveRecord::BaseWithoutTable
validates_presence_of :old_email, :message => N_("Please enter your old email address")
validates_presence_of :new_email, :message => N_("Please enter your new email address")
validates_presence_of :password, :message => N_("Please enter your password"), :unless => :changing_email
+ validate :password_and_format_of_email
def changing_email()
self.user_circumstance == 'change_email'
end
- def validate
+ private
+
+ def password_and_format_of_email
if !self.old_email.blank? && !MySociety::Validate.is_valid_email(self.old_email)
errors.add(:old_email, _("Old email doesn't look like a valid address"))
end
diff --git a/app/models/comment.rb b/app/models/comment.rb
index bcd1efca8..70f3ba00d 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -24,13 +24,13 @@ class Comment < ActiveRecord::Base
strip_attributes!
belongs_to :user
- #validates_presence_of :user # breaks during construction of new ones :(
-
- validates_inclusion_of :comment_type, :in => [ 'request' ]
belongs_to :info_request
-
has_many :info_request_events # in practice only ever has one
+ #validates_presence_of :user # breaks during construction of new ones :(
+ validates_inclusion_of :comment_type, :in => [ 'request' ]
+ validate :body_of_comment
+
def body
ret = read_attribute(:body)
if ret.nil?
@@ -40,6 +40,7 @@ class Comment < ActiveRecord::Base
ret = ret.gsub(/(?:\n\s*){2,}/, "\n\n") # remove excess linebreaks that unnecessarily space it out
ret
end
+
def raw_body
read_attribute(:body)
end
@@ -52,16 +53,6 @@ class Comment < ActiveRecord::Base
end
end
- # Check have edited comment
- def validate
- if self.body.empty? || self.body =~ /^\s+$/
- errors.add(:body, _("Please enter your annotation"))
- end
- if !MySociety::Validate.uses_mixed_capitals(self.body)
- errors.add(:body, _('Please write your annotation using a mixture of capital and lower case letters. This makes it easier for others to read.'))
- end
- end
-
# Return body for display as HTML
def get_body_for_html_display
text = self.body.strip
@@ -82,9 +73,21 @@ class Comment < ActiveRecord::Base
return Comment.find(:first, :conditions => [ "info_request_id = ? and body = ?", info_request_id, body ])
end
end
- def for_admin_column
- self.class.content_columns.each do |column|
- yield(column.human_name, self.send(column.name), column.type.to_s, column.name)
+
+ def for_admin_column
+ self.class.content_columns.each do |column|
+ yield(column.human_name, self.send(column.name), column.type.to_s, column.name)
+ end
+ end
+
+ private
+
+ def body_of_comment
+ if self.body.empty? || self.body =~ /^\s+$/
+ errors.add(:body, _("Please enter your annotation"))
+ end
+ if !MySociety::Validate.uses_mixed_capitals(self.body)
+ errors.add(:body, _('Please write your annotation using a mixture of capital and lower case letters. This makes it easier for others to read.'))
+ end
end
- end
end
diff --git a/app/models/contact_mailer.rb b/app/models/contact_mailer.rb
index 16aae2f15..abde64928 100644
--- a/app/models/contact_mailer.rb
+++ b/app/models/contact_mailer.rb
@@ -7,7 +7,7 @@
class ContactMailer < ApplicationMailer
# Send message to administrator
- def message(name, email, subject, message, logged_in_user, last_request, last_body)
+ def to_admin_message(name, email, subject, message, logged_in_user, last_request, last_body)
@from = name + " <" + email + ">"
@recipients = contact_from_name_and_email
@subject = subject
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index 3f551f420..5c845ead3 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -535,7 +535,7 @@ class IncomingMessage < ActiveRecord::Base
text = Iconv.conv('utf-8//IGNORE', source_charset, text) +
_("\n\n[ {{site_name}} note: The above text was badly encoded, and has had strange characters removed. ]",
:site_name => Configuration::site_name)
- rescue Iconv::InvalidEncoding, Iconv::IllegalSequence
+ rescue Iconv::InvalidEncoding, Iconv::IllegalSequence, Iconv::InvalidCharacter
if source_charset != "utf-8"
source_charset = "utf-8"
retry
diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb
index 441813e5f..c75894e6a 100644
--- a/app/models/outgoing_message.rb
+++ b/app/models/outgoing_message.rb
@@ -30,6 +30,7 @@ class OutgoingMessage < ActiveRecord::Base
validates_inclusion_of :status, :in => ['ready', 'sent', 'failed']
validates_inclusion_of :message_type, :in => ['initial_request', 'followup' ] #, 'complaint']
+ validate :format_of_body
belongs_to :incoming_message_followup, :foreign_key => 'incoming_message_followup_id', :class_name => 'IncomingMessage'
@@ -136,32 +137,6 @@ class OutgoingMessage < ActiveRecord::Base
end
end
- # Check have edited letter
- def validate
- if self.body.empty? || self.body =~ /\A#{get_salutation}\s+#{get_signoff}/ || self.body =~ /#{get_internal_review_insert_here_note}/
- if self.message_type == 'followup'
- if self.what_doing == 'internal_review'
- errors.add(:body, _("Please give details explaining why you want a review"))
- else
- errors.add(:body, _("Please enter your follow up message"))
- end
- elsif
- errors.add(:body, _("Please enter your letter requesting information"))
- else
- raise "Message id #{self.id} has type '#{self.message_type}' which validate can't handle"
- end
- end
- if self.body =~ /#{get_signoff}\s*\Z/m
- errors.add(:body, _("Please sign at the bottom with your name, or alter the \"%{signoff}\" signature" % { :signoff => get_signoff }))
- end
- if !MySociety::Validate.uses_mixed_capitals(self.body)
- errors.add(:body, _('Please write your message using a mixture of capital and lower case letters. This makes it easier for others to read.'))
- end
- if self.what_doing.nil? || !['new_information', 'internal_review', 'normal_sort'].include?(self.what_doing)
- errors.add(:what_doing_dummy, _('Please choose what sort of reply you are making.'))
- end
- end
-
# Deliver outgoing message
# Note: You can test this from script/console with, say:
# InfoRequest.find(1).outgoing_messages[0].send_message
@@ -275,6 +250,33 @@ class OutgoingMessage < ActiveRecord::Base
yield(column.human_name, self.send(column.name), column.type.to_s, column.name)
end
end
+
+ private
+
+ def format_of_body
+ if self.body.empty? || self.body =~ /\A#{get_salutation}\s+#{get_signoff}/ || self.body =~ /#{get_internal_review_insert_here_note}/
+ if self.message_type == 'followup'
+ if self.what_doing == 'internal_review'
+ errors.add(:body, _("Please give details explaining why you want a review"))
+ else
+ errors.add(:body, _("Please enter your follow up message"))
+ end
+ elsif
+ errors.add(:body, _("Please enter your letter requesting information"))
+ else
+ raise "Message id #{self.id} has type '#{self.message_type}' which validate can't handle"
+ end
+ end
+ if self.body =~ /#{get_signoff}\s*\Z/m
+ errors.add(:body, _("Please sign at the bottom with your name, or alter the \"%{signoff}\" signature" % { :signoff => get_signoff }))
+ end
+ if !MySociety::Validate.uses_mixed_capitals(self.body)
+ errors.add(:body, _('Please write your message using a mixture of capital and lower case letters. This makes it easier for others to read.'))
+ end
+ if self.what_doing.nil? || !['new_information', 'internal_review', 'normal_sort'].include?(self.what_doing)
+ errors.add(:what_doing_dummy, _('Please choose what sort of reply you are making.'))
+ end
+ end
end
diff --git a/app/models/profile_photo.rb b/app/models/profile_photo.rb
index 6e605651d..73d7ca12b 100644
--- a/app/models/profile_photo.rb
+++ b/app/models/profile_photo.rb
@@ -23,6 +23,8 @@ class ProfilePhoto < ActiveRecord::Base
belongs_to :user
+ validate :data_and_draft_checks
+
# deliberately don't strip_attributes, so keeps raw photo properly
attr_accessor :x, :y, :w, :h
@@ -81,7 +83,9 @@ class ProfilePhoto < ActiveRecord::Base
end
end
- def validate
+ private
+
+ def data_and_draft_checks
if self.data.nil?
errors.add(:data, N_("Please choose a file containing your photo."))
return
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index f71520ee6..168b9f4c7 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -35,6 +35,8 @@ class PublicBody < ActiveRecord::Base
validates_uniqueness_of :short_name, :message => N_("Short name is already taken"), :if => Proc.new { |pb| pb.short_name != "" }
validates_uniqueness_of :name, :message => N_("Name is already taken")
+ validate :request_email_if_requestable
+
has_many :info_requests, :order => 'created_at desc'
has_many :track_things, :order => 'created_at desc'
has_many :censor_rules, :order => 'created_at desc'
@@ -135,15 +137,6 @@ class PublicBody < ActiveRecord::Base
self.first_letter = self.name.scan(/./mu)[0].upcase
end
- def validate
- # Request_email can be blank, meaning we don't have details
- if self.is_requestable?
- unless MySociety::Validate.is_valid_email(self.request_email)
- errors.add(:request_email, "Request email doesn't look like a valid email address")
- end
- end
- end
-
# If tagged "not_apply", then FOI/EIR no longer applies to authority at all
def not_apply?
return self.has_tag?('not_apply')
@@ -642,4 +635,12 @@ class PublicBody < ActiveRecord::Base
end
end
+ def request_email_if_requestable
+ # Request_email can be blank, meaning we don't have details
+ if self.is_requestable?
+ unless MySociety::Validate.is_valid_email(self.request_email)
+ errors.add(:request_email, "Request email doesn't look like a valid email address")
+ end
+ end
+ end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 6e1e21481..617b51c60 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -50,6 +50,8 @@ class User < ActiveRecord::Base
'super',
], :message => N_('Admin level is not included in list')
+ validate :email_and_name_are_valid
+
acts_as_xapian :texts => [ :name, :about_me ],
:values => [
[ :created_at_numeric, 1, "created_at", :number ] # for sorting
@@ -108,15 +110,6 @@ class User < ActiveRecord::Base
self.comments.find(:all, :conditions => 'visible')
end
- def validate
- if self.email != "" && !MySociety::Validate.is_valid_email(self.email)
- errors.add(:email, _("Please enter a valid email address"))
- end
- if MySociety::Validate.is_valid_email(self.name)
- errors.add(:name, _("Please enter your name, not your email address, in the name field."))
- end
- end
-
# Don't display any leading/trailing spaces
# XXX we have strip_attributes! now, so perhaps this can be removed (might
# be still needed for existing cases)
@@ -413,6 +406,15 @@ class User < ActiveRecord::Base
self.salt = self.object_id.to_s + rand.to_s
end
+ def email_and_name_are_valid
+ if self.email != "" && !MySociety::Validate.is_valid_email(self.email)
+ errors.add(:email, _("Please enter a valid email address"))
+ end
+ if MySociety::Validate.is_valid_email(self.name)
+ errors.add(:name, _("Please enter your name, not your email address, in the name field."))
+ end
+ end
+
## Class methods
def User.encrypted_password(password, salt)
string_to_hash = password + salt # XXX need to add a secret here too?