Fix #422 by validating that project names do not contain commas. Thanks for the bug report, Stephen!

Added this validation to Contexts as well.
Introduced a new plugin to hold extra validations. The first one is validates_does_not_contain, which
DRYs up and cleans up the validations used to prohibit slashes and commas in Project and Context names.



git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@394 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2007-01-10 04:55:39 +00:00
parent 12aa6cee41
commit f73401a850
6 changed files with 52 additions and 7 deletions

View file

@ -10,12 +10,11 @@ class Context < ActiveRecord::Base
attr_protected :user
# Context name must not be empty
# and must be less than 256 characters
validates_presence_of :name, :message => "context must have a name"
validates_length_of :name, :maximum => 255, :message => "context name must be less than 256 characters"
validates_uniqueness_of :name, :message => "already exists", :scope => "user_id"
validates_format_of :name, :with => /^[^\/]*$/i, :message => "cannot contain the slash ('/') character"
validates_does_not_contain :name, :string => '/', :message => "cannot contain the slash ('/') character"
validates_does_not_contain :name, :string => ',', :message => "cannot contain the comma (',') character"
def hidden?
self.hide == true

View file

@ -3,12 +3,11 @@ class Project < ActiveRecord::Base
has_many :notes, :dependent => :delete_all, :order => "created_at DESC"
belongs_to :user
# Project name must not be empty
# and must be less than 255 bytes
validates_presence_of :name, :message => "project must have a name"
validates_length_of :name, :maximum => 255, :message => "project name must be less than 256 characters"
validates_uniqueness_of :name, :message => "already exists", :scope =>"user_id"
validates_format_of :name, :with => /^[^\/]*$/i, :message => "cannot contain the slash ('/') character"
validates_does_not_contain :name, :string => '/', :message => "cannot contain the slash ('/') character"
validates_does_not_contain :name, :string => ',', :message => "cannot contain the comma (',') character"
acts_as_list :scope => :user
acts_as_state_machine :initial => :active, :column => 'state'

View file

@ -38,6 +38,14 @@ class ContextTest < Test::Unit::TestCase
assert_equal 1, newcontext.errors.count
assert_equal "cannot contain the slash ('/') character", newcontext.errors.on(:name)
end
def test_validate_name_does_not_contain_comma
newcontext = Context.new
newcontext.name = "phone,telegraph"
assert !newcontext.save
assert_equal 1, newcontext.errors.count
assert_equal "cannot contain the comma (',') character", newcontext.errors.on(:name)
end
def test_find_by_namepart_with_exact_match
c = Context.find_by_namepart('agenda')

View file

@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/../test_helper'
class ProjectTest < Test::Unit::TestCase
fixtures :projects, :todos, :users, :preferences
fixtures :projects, :contexts, :todos, :users, :preferences
def setup
@timemachine = projects(:timemachine)
@ -39,6 +39,14 @@ class ProjectTest < Test::Unit::TestCase
assert_equal "cannot contain the slash ('/') character", newproj.errors.on(:name)
end
def test_validate_name_does_not_contain_comma
newproj = Project.new
newproj.name = "Buy iPhones for Luke,bsag,David Allen"
assert !newproj.save
assert_equal 1, newproj.errors.count
assert_equal "cannot contain the comma (',') character", newproj.errors.on(:name)
end
def test_project_initial_state_is_active
assert_equal :active, @timemachine.current_state
assert @timemachine.active?

View file

@ -0,0 +1,2 @@
require 'extra_validations'
ActiveRecord::Base.extend ExtraValidations

View file

@ -0,0 +1,29 @@
module ExtraValidations
# Validates the value of the specified attribute by checking for a forbidden string
#
# class Person < ActiveRecord::Base
# validates_does_not_contain :first_name, :string => ','
# end
#
# A string must be provided or else an exception will be raised.
#
# Configuration options:
# * <tt>message</tt> - A custom error message (default is: "is invalid")
# * <tt>string</tt> - The string to verify is not included (note: must be supplied!)
# * <tt>on</tt> Specifies when this validation is active (default is :save, other options :create, :update)
# * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
# method, proc or string should return or evaluate to a true or false value.
def validates_does_not_contain(*attr_names)
configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save, :string => nil }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
raise(ArgumentError, "A string must be supplied as the :string option of the configuration hash") unless configuration[:string].is_a?(String)
validates_each(attr_names, configuration) do |record, attr_name, value|
record.errors.add(attr_name, configuration[:message]) if value.to_s =~ Regexp.new(Regexp.escape(configuration[:string]))
end
end
end