diff --git a/tracks/app/models/context.rb b/tracks/app/models/context.rb index d1be5a5e..5ca18ed8 100644 --- a/tracks/app/models/context.rb +++ b/tracks/app/models/context.rb @@ -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 diff --git a/tracks/app/models/project.rb b/tracks/app/models/project.rb index 61e6daa4..ab6f5a63 100644 --- a/tracks/app/models/project.rb +++ b/tracks/app/models/project.rb @@ -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' diff --git a/tracks/test/unit/context_test.rb b/tracks/test/unit/context_test.rb index 2dc0a163..5f272565 100644 --- a/tracks/test/unit/context_test.rb +++ b/tracks/test/unit/context_test.rb @@ -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') diff --git a/tracks/test/unit/project_test.rb b/tracks/test/unit/project_test.rb index 02daf054..046e8614 100644 --- a/tracks/test/unit/project_test.rb +++ b/tracks/test/unit/project_test.rb @@ -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? diff --git a/tracks/vendor/plugins/extra_validations/init.rb b/tracks/vendor/plugins/extra_validations/init.rb new file mode 100644 index 00000000..17709ad4 --- /dev/null +++ b/tracks/vendor/plugins/extra_validations/init.rb @@ -0,0 +1,2 @@ +require 'extra_validations' +ActiveRecord::Base.extend ExtraValidations \ No newline at end of file diff --git a/tracks/vendor/plugins/extra_validations/lib/extra_validations.rb b/tracks/vendor/plugins/extra_validations/lib/extra_validations.rb new file mode 100644 index 00000000..f5486e96 --- /dev/null +++ b/tracks/vendor/plugins/extra_validations/lib/extra_validations.rb @@ -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: + # * message - A custom error message (default is: "is invalid") + # * string - The string to verify is not included (note: must be supplied!) + # * on Specifies when this validation is active (default is :save, other options :create, :update) + # * if - 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