acts_as_human 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ = ActsAsHuman
2
+
3
+ * Adds:
4
+ full_name
5
+ * and
6
+ full_name=
7
+ instance methods to a model
8
+
9
+ Adds validations to ensure that the names are reasonable.
10
+
11
+ * Requires:
12
+ first_name, last_name and middle_names columns on the model
13
+
14
+ @todo Need to add rake task to add fields to model.
15
+
16
+ == Example
17
+
18
+ class User < ActiveRecord::Base
19
+ acts_as_human
20
+ end
21
+
22
+ * user = User.new(:first_name => 'Brent', :last_name => 'Greeff')
23
+ user.full_name
24
+ => "Brent Greeff"
25
+
26
+ * user = User.new(:full_name => "Brent Wicked Middle Names Greeff")
27
+ user.first_name
28
+ => "Brent"
29
+
30
+ user.last_name
31
+ => "Greeff"
32
+
33
+ user.middle_names
34
+ => "Wicked Middle Names"
35
+
36
+ Copyright (c) 2009 [Brent Greeff], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the acts_as_human plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the acts_as_human plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ActsAsHuman'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
@@ -0,0 +1,22 @@
1
+ module ActiveRecord
2
+ module Validations
3
+ module ClassMethods
4
+
5
+ def validates_as_person_name(*attr_names)
6
+ config = {
7
+ :with => ActsAs::Human.acceptable_name,
8
+ :message => ActsAs::Human.bad_name_message
9
+ }
10
+ check_format(attr_names, config)
11
+ end
12
+
13
+ private
14
+
15
+ def check_format(attr_names, config)
16
+ config.update(attr_names.pop) if attr_names.last.is_a?(Hash)
17
+ validates_format_of attr_names, config
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,83 @@
1
+ module ActsAs
2
+ module Human
3
+ mattr_accessor :acceptable_name, :bad_name_message
4
+
5
+ self.acceptable_name = /\A[^[:cntrl:]\\<>\/&]*\z/
6
+ self.bad_name_message = "some characters in your name are not allowed".freeze
7
+
8
+ def self.included(base)
9
+ base.send :extend, ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ def acts_as_human(options={})
15
+ cattr_accessor :require_last_name
16
+
17
+ if options.has_key? :require_last_name
18
+ @require_last_name = options[:require_last_name]
19
+ else
20
+ @require_last_name = true
21
+ end
22
+ send :include, InstanceMethods
23
+
24
+ class_eval do
25
+ validates_presence_of :first_name, :message => 'first name is required'
26
+ validates_length_of :first_name, :maximum => 40,
27
+ :message => 'first name is too long (max 40 characters)'
28
+ validates_as_person_name :first_name
29
+
30
+ validates_length_of :middle_names, :maximum => 40,
31
+ :allow_blank => true,
32
+ :message => 'middle names are too long (max 40 characters)'
33
+ validates_as_person_name :middle_names, :allow_nil => true
34
+
35
+ validates_presence_of :last_name, :if => Proc.new { @require_last_name },
36
+ :message => 'last name is required'
37
+ validates_length_of :last_name, :maximum => 40,
38
+ :allow_blank => true,
39
+ :message => 'last name is too long (max 40 characters)'
40
+ validates_as_person_name :last_name
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ module InstanceMethods
47
+ def full_name
48
+ return '' if first_name.blank? and last_name.blank?
49
+
50
+ return "#{first_name} #{last_name}" if middle_names.blank?
51
+ return "#{first_name} #{middle_names} #{last_name}"
52
+ end
53
+
54
+ def full_name=(names)
55
+ names_array = names.titlecase.split
56
+
57
+ self.first_name = names_array.first
58
+ return if names_array.size < 2
59
+
60
+ self.last_name = names_array.last
61
+
62
+ assign_middle_names(names_array)
63
+ end
64
+
65
+ private
66
+
67
+ def assign_middle_names(names_array)
68
+ if names_array.size > 2
69
+ self.middle_names = get_middle_names(names_array)
70
+ else
71
+ self.middle_names = nil
72
+ end
73
+ end
74
+
75
+ def get_middle_names(names_array)
76
+ names_array[1..(names_array.size-2)].join(' ')
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ ActiveRecord::Base.send(:include, ActsAs::Human)
83
+
@@ -0,0 +1,126 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class ActsAsHumanTest < ActiveSupport::TestCase
4
+
5
+ class User < ActiveRecord::Base
6
+ acts_as_human
7
+ end
8
+
9
+ class Customer < ActiveRecord::Base
10
+ acts_as_human :require_last_name => false
11
+ end
12
+
13
+ def test_should_require_a_first_name
14
+ user = create_user
15
+ user.first_name = ''
16
+
17
+ deny user.valid?, 'Should not be valid without a first name'
18
+ assert_equal 'first name is required', user.errors[:first_name]
19
+ end
20
+
21
+ def test_should_have_first_name_that_is_less_than_40_characters
22
+ user = User.new(:first_name => 'aaaaabbbbbcccccdddddeeeeefffffggggghhhhhi')
23
+
24
+ deny user.valid?
25
+ assert_equal 'first name is too long (max 40 characters)', user.errors[:first_name]
26
+ end
27
+
28
+ def test_should_require_a_last_name
29
+ user = User.new(:first_name => 'Brent')
30
+
31
+ deny user.valid?
32
+ assert_equal 'last name is required', user.errors[:last_name]
33
+ end
34
+
35
+ def test_should_not_require_a_last_name_if_not_required
36
+ someone_else = Customer.new(:first_name => 'Brent')
37
+ assert someone_else.valid?
38
+ end
39
+
40
+ def test_should_have_last_name_that_is_less_than_40_characters
41
+ user = create_user
42
+ user.last_name = 'aaaaabbbbbcccccdddddeeeeefffffggggghhhhhi'
43
+
44
+ deny user.valid?
45
+ assert_equal 'last name is too long (max 40 characters)', user.errors[:last_name]
46
+ end
47
+
48
+ def test_should_have_a_last_name_when_assigned_through_full_name
49
+ user = User.new(:full_name => 'Brent')
50
+
51
+ deny user.valid?
52
+ assert_equal 'last name is required', user.errors[:last_name]
53
+ end
54
+
55
+ def test_should_have_full_name_composed_of_first_and_last_name
56
+ user = User.new(:first_name => 'Brent', :last_name => 'Greeff')
57
+ assert_equal 'Brent Greeff', user.full_name
58
+ end
59
+
60
+ def test_should_have_full_name_composed_of_first_middle_and_last_name
61
+ user = User.new(:first_name => 'Brent', :middle_names => 'Middle Names', :last_name => 'Greeff')
62
+ assert_equal 'Brent Middle Names Greeff', user.full_name
63
+ end
64
+
65
+ def test_should_assign_first_and_last_name_when_assigning_to_full_name
66
+ user = User.new(:full_name => 'Brent Greeff')
67
+
68
+ assert_equal 'Brent', user.first_name
69
+ assert_equal 'Greeff', user.last_name
70
+ end
71
+
72
+ def test_should_assign_first_last_and_middle_names_when_assigning_to_full_name
73
+ user = User.new(:full_name => 'Brent Middle Names Greeff')
74
+
75
+ assert_equal 'Brent', user.first_name
76
+ assert_equal 'Middle Names', user.middle_names
77
+ assert_equal 'Greeff', user.last_name
78
+ end
79
+
80
+ def test_should_have_middle_names_that_are_less_than_40_characters
81
+ user = create_user
82
+ user.middle_names = 'aaaaabbbbbcccccdddddeeeeefffffggggghhhhhi'
83
+
84
+ deny user.valid?
85
+ assert_equal 'middle names are too long (max 40 characters)', user.errors[:middle_names]
86
+ end
87
+
88
+ def test_should_have_a_empty_full_name_when_user_is_new
89
+ user = User.new
90
+ assert_equal '', user.full_name
91
+ end
92
+
93
+ def test_should_not_allow_non_name_like_characters
94
+ user = User.new(:full_name => "<Brent> Middle >< Names Gre&eff")
95
+
96
+ deny user.valid?
97
+ assert_equal 'some characters in your name are not allowed', user.errors[:first_name]
98
+ assert_equal 'some characters in your name are not allowed', user.errors[:last_name]
99
+ assert_equal 'some characters in your name are not allowed', user.errors[:middle_names]
100
+ end
101
+
102
+ private
103
+
104
+ def create_user(options={})
105
+ default_options = {
106
+ :first_name => "Brent",
107
+ :last_name => "Greeff"
108
+ }
109
+
110
+ return User.create!(default_options.merge(options))
111
+ end
112
+
113
+ def create_customer(options={})
114
+ default_options = {
115
+ :first_name => "Brent",
116
+ :last_name => "Greeff"
117
+ }
118
+
119
+ return Customer.create!(default_options.merge(options))
120
+ end
121
+
122
+ def deny(expected_to_be_false, message = '')
123
+ assert ! expected_to_be_false, message
124
+ end
125
+
126
+ end
data/test/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
data/test/schema.rb ADDED
@@ -0,0 +1,15 @@
1
+ ActiveRecord::Schema.define(:version => 20090628014113) do
2
+
3
+ create_table "users", :force => true do |t|
4
+ t.string "first_name"
5
+ t.string "middle_names"
6
+ t.string "last_name"
7
+ end
8
+
9
+ create_table "customers", :force => true do |t|
10
+ t.string "first_name"
11
+ t.string "middle_names"
12
+ t.string "last_name"
13
+ end
14
+
15
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
4
+ require 'test/unit'
5
+
6
+ ENV['RAILS_ENV'] = 'test'
7
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
8
+
9
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
10
+
11
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
12
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
13
+ ActiveRecord::Base.establish_connection(config['test'])
14
+
15
+ load(File.dirname(__FILE__) + "/schema.rb")
16
+
17
+ require File.dirname(__FILE__) + '/../rails/init.rb'
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_human
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 2
8
+ - 0
9
+ - 0
10
+ version: 2.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Brent Greeff
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-27 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Rails plugin to handle first_name, middle_names and last_name combinations.
23
+ email: email@brentgreeff.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - MIT-LICENSE
30
+ - README.rdoc
31
+ files:
32
+ - MIT-LICENSE
33
+ - README.rdoc
34
+ - Rakefile
35
+ - VERSION
36
+ - lib/acts_as_human.rb
37
+ - lib/acts_as_human/validations.rb
38
+ - test/acts_as_human_test.rb
39
+ - test/database.yml
40
+ - test/schema.rb
41
+ - test/test_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/brentgreeff/acts_as_human
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.7
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Rails plugin to handle first_name, middle_names and last_name combinations.
76
+ test_files: []
77
+