Skip to content

Commit

Permalink
2-space models
Browse files Browse the repository at this point in the history
  • Loading branch information
garethrees committed Jul 15, 2015
1 parent 8f94cb2 commit f2c538c
Show file tree
Hide file tree
Showing 32 changed files with 5,206 additions and 5,210 deletions.
14 changes: 7 additions & 7 deletions app/models/about_me_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
# Email: [email protected]; WWW: http://www.mysociety.org/

class AboutMeValidator
include ActiveModel::Validations
include ActiveModel::Validations

attr_accessor :about_me
attr_accessor :about_me

validates_length_of :about_me, :maximum => 500, :message => _("Please keep it shorter than 500 characters")
validates_length_of :about_me, :maximum => 500, :message => _("Please keep it shorter than 500 characters")

def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
end
138 changes: 69 additions & 69 deletions app/models/censor_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,82 +23,82 @@
# Email: [email protected]; WWW: http://www.mysociety.org/

class CensorRule < ActiveRecord::Base
include AdminColumn
belongs_to :info_request
belongs_to :user
belongs_to :public_body

# a flag to allow the require_user_request_or_public_body
# validation to be skipped
attr_accessor :allow_global

validate :require_user_request_or_public_body, :unless => proc { |rule| rule.allow_global == true }
validate :require_valid_regexp, :if => proc { |rule| rule.regexp? == true }

validates_presence_of :text,
:replacement,
:last_edit_comment,
:last_edit_editor

scope :global, { :conditions => { :info_request_id => nil,
: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('UTF-8'), replacement)
include AdminColumn
belongs_to :info_request
belongs_to :user
belongs_to :public_body

# a flag to allow the require_user_request_or_public_body
# validation to be skipped
attr_accessor :allow_global

validate :require_user_request_or_public_body, :unless => proc { |rule| rule.allow_global == true }
validate :require_valid_regexp, :if => proc { |rule| rule.regexp? == true }

validates_presence_of :text,
:replacement,
:last_edit_comment,
:last_edit_editor

scope :global, { :conditions => { :info_request_id => nil,
: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('UTF-8'), replacement)
end

def apply_to_text!(text_to_censor)
return nil if text_to_censor.nil?
text_to_censor.gsub!(to_replace('UTF-8'), replacement)
end

def apply_to_binary!(binary_to_censor)
return nil if binary_to_censor.nil?
binary_to_censor.gsub!(to_replace('ASCII-8BIT')) { |match| match.gsub(single_char_regexp, 'x') }
end

def is_global?
info_request_id.nil? && user_id.nil? && public_body_id.nil?
end

private

def single_char_regexp
if String.method_defined?(:encode)
Regexp.new('.'.force_encoding('ASCII-8BIT'))
else
Regexp.new('.', nil, 'N')
end
end

def apply_to_text!(text_to_censor)
return nil if text_to_censor.nil?
text_to_censor.gsub!(to_replace('UTF-8'), replacement)
def require_user_request_or_public_body
if info_request.nil? && user.nil? && public_body.nil?
[:info_request, :user, :public_body].each do |a|
errors.add(a, "Rule must apply to an info request, a user or a body")
end
end
end

def apply_to_binary!(binary_to_censor)
return nil if binary_to_censor.nil?
binary_to_censor.gsub!(to_replace('ASCII-8BIT')) { |match| match.gsub(single_char_regexp, 'x') }
def require_valid_regexp
begin
make_regexp('UTF-8')
rescue RegexpError => e
errors.add(:text, e.message)
end
end

def is_global?
info_request_id.nil? && user_id.nil? && public_body_id.nil?
end

private

def single_char_regexp
if String.method_defined?(:encode)
Regexp.new('.'.force_encoding('ASCII-8BIT'))
else
Regexp.new('.', nil, 'N')
end
end

def require_user_request_or_public_body
if info_request.nil? && user.nil? && public_body.nil?
[:info_request, :user, :public_body].each do |a|
errors.add(a, "Rule must apply to an info request, a user or a body")
end
end
end
def to_replace(encoding)
regexp? ? make_regexp(encoding) : encoded_text(encoding)
end

def require_valid_regexp
begin
make_regexp('UTF-8')
rescue RegexpError => e
errors.add(:text, e.message)
end
end

def to_replace(encoding)
regexp? ? make_regexp(encoding) : encoded_text(encoding)
end
def encoded_text(encoding)
String.method_defined?(:encode) ? text.dup.force_encoding(encoding) : text
end

def encoded_text(encoding)
String.method_defined?(:encode) ? text.dup.force_encoding(encoding) : text
end

def make_regexp(encoding)
Regexp.new(encoded_text(encoding), Regexp::MULTILINE)
end
def make_regexp(encoding)
Regexp.new(encoded_text(encoding), Regexp::MULTILINE)
end

end
106 changes: 53 additions & 53 deletions app/models/change_email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,76 @@
# Email: [email protected]; WWW: http://www.mysociety.org/

class ChangeEmailValidator
include ActiveModel::Validations
include ActiveModel::Validations

attr_accessor :old_email,
:new_email,
:password,
:user_circumstance,
:logged_in_user
attr_accessor :old_email,
:new_email,
:password,
:user_circumstance,
:logged_in_user

validates_presence_of :old_email,
:message => N_("Please enter your old email address")
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 :new_email,
:message => N_("Please enter your new email address")

validates_presence_of :password,
:message => N_("Please enter your password"),
:unless => :changing_email
validates_presence_of :password,
:message => N_("Please enter your password"),
:unless => :changing_email

validate :password_and_format_of_email
validate :password_and_format_of_email

def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end

def changing_email
self.user_circumstance == 'change_email'
end
def changing_email
self.user_circumstance == 'change_email'
end

private
private

def password_and_format_of_email
check_email_is_present_and_valid(:old_email)
def password_and_format_of_email
check_email_is_present_and_valid(:old_email)

if errors[:old_email].blank?
if !email_belongs_to_user?(old_email)
errors.add(:old_email, _("Old email address isn't the same as the address of the account you are logged in with"))
elsif !changing_email && !correct_password?
if errors[:password].blank?
errors.add(:password, _("Password is not correct"))
end
end
if errors[:old_email].blank?
if !email_belongs_to_user?(old_email)
errors.add(:old_email, _("Old email address isn't the same as the address of the account you are logged in with"))
elsif !changing_email && !correct_password?
if errors[:password].blank?
errors.add(:password, _("Password is not correct"))
end

check_email_is_present_and_valid(:new_email)
end
end

def check_email_is_present_and_valid(email)
if !send(email).blank? && !MySociety::Validate.is_valid_email(send(email))
msg_string = check_email_is_present_and_valid_msg_string(email)
errors.add(email, msg_string)
end
end
check_email_is_present_and_valid(:new_email)
end

def check_email_is_present_and_valid_msg_string(email)
case email.to_sym
when :old_email then _("Old email doesn't look like a valid address")
when :new_email then _("New email doesn't look like a valid address")
else
raise "Unsupported email type #{ email }"
end
def check_email_is_present_and_valid(email)
if !send(email).blank? && !MySociety::Validate.is_valid_email(send(email))
msg_string = check_email_is_present_and_valid_msg_string(email)
errors.add(email, msg_string)
end

def email_belongs_to_user?(email)
email.downcase == logged_in_user.email.downcase
end

def check_email_is_present_and_valid_msg_string(email)
case email.to_sym
when :old_email then _("Old email doesn't look like a valid address")
when :new_email then _("New email doesn't look like a valid address")
else
raise "Unsupported email type #{ email }"
end
end

def correct_password?
logged_in_user.has_this_password?(password)
end
def email_belongs_to_user?(email)
email.downcase == logged_in_user.email.downcase
end

def correct_password?
logged_in_user.has_this_password?(password)
end

end
Loading

0 comments on commit f2c538c

Please sign in to comment.