ccls-common_lib 1.4.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.
- checksums.yaml +7 -0
- data/README.rdoc +116 -0
- data/lib/ccls-common_lib.rb +1 -0
- data/lib/common_lib.rb +37 -0
- data/lib/common_lib/action_controller_extension.rb +8 -0
- data/lib/common_lib/action_controller_extension/accessible_via_protocol.rb +405 -0
- data/lib/common_lib/action_controller_extension/accessible_via_user.rb +605 -0
- data/lib/common_lib/action_controller_extension/routing.rb +20 -0
- data/lib/common_lib/action_controller_extension/test_case.rb +22 -0
- data/lib/common_lib/action_view_extension.rb +3 -0
- data/lib/common_lib/action_view_extension/base.rb +310 -0
- data/lib/common_lib/action_view_extension/form_builder.rb +197 -0
- data/lib/common_lib/active_model.rb +4 -0
- data/lib/common_lib/active_model/errors.rb +16 -0
- data/lib/common_lib/active_model/validations/absence.rb +78 -0
- data/lib/common_lib/active_model/validations/complete_date.rb +138 -0
- data/lib/common_lib/active_model/validations/past_date.rb +121 -0
- data/lib/common_lib/active_record.rb +1 -0
- data/lib/common_lib/active_record/base.rb +129 -0
- data/lib/common_lib/active_support_extension.rb +12 -0
- data/lib/common_lib/active_support_extension/assertions.rb +108 -0
- data/lib/common_lib/active_support_extension/associations.rb +154 -0
- data/lib/common_lib/active_support_extension/attributes.rb +296 -0
- data/lib/common_lib/active_support_extension/pending.rb +115 -0
- data/lib/common_lib/active_support_extension/test_case.rb +203 -0
- data/lib/common_lib/railtie.rb +48 -0
- data/lib/common_lib/ruby.rb +8 -0
- data/lib/common_lib/ruby/array.rb +128 -0
- data/lib/common_lib/ruby/fixnum.rb +5 -0
- data/lib/common_lib/ruby/hash.rb +51 -0
- data/lib/common_lib/ruby/integer.rb +11 -0
- data/lib/common_lib/ruby/nil_class.rb +13 -0
- data/lib/common_lib/ruby/numeric.rb +0 -0
- data/lib/common_lib/ruby/object.rb +53 -0
- data/lib/common_lib/ruby/string.rb +20 -0
- data/lib/common_lib/translation_table.rb +129 -0
- data/lib/tasks/common_lib.rake +10 -0
- data/lib/tasks/csv.rake +0 -0
- data/lib/tasks/database.rake +229 -0
- data/lib/tasks/rcov.rake +52 -0
- data/vendor/assets/javascripts/common_lib.js +77 -0
- data/vendor/assets/stylesheets/common_lib.css +71 -0
- metadata +84 -0
@@ -0,0 +1,12 @@
|
|
1
|
+
module CommonLib::ActiveSupportExtension; end
|
2
|
+
if defined?(Rails) and Rails.env == 'test'
|
3
|
+
require 'common_lib/active_support_extension/test_case'
|
4
|
+
require 'common_lib/active_support_extension/associations'
|
5
|
+
require 'common_lib/active_support_extension/assertions'
|
6
|
+
require 'common_lib/active_support_extension/attributes'
|
7
|
+
|
8
|
+
# due to the complications of rake and autotest using
|
9
|
+
# differing versions of Test::Unit and MiniTest
|
10
|
+
# I have modified and am reincluding pending
|
11
|
+
require 'common_lib/active_support_extension/pending'
|
12
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
module CommonLib::ActiveSupportExtension::Assertions
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
def assert_blank obj, msg = nil
|
8
|
+
msg ||= "Expected '#{obj}' to be blank"
|
9
|
+
assert obj.blank?, msg
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
def assert_should_create_default_object(*args)
|
15
|
+
options = {}
|
16
|
+
options.update(args.extract_options!)
|
17
|
+
model = options[:model] || model_name_without_test
|
18
|
+
|
19
|
+
test "should create default #{model.underscore}" do
|
20
|
+
assert_difference( "#{model}.count", 1 ) do
|
21
|
+
object = create_object
|
22
|
+
assert !object.new_record?,
|
23
|
+
"#{object.errors.full_messages.to_sentence}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def assert_should_behave_like_a_hash(*args)
|
29
|
+
options = {
|
30
|
+
:key => :key,
|
31
|
+
:value => :description
|
32
|
+
}
|
33
|
+
options.update(args.extract_options!)
|
34
|
+
model = options[:model] || model_name_without_test
|
35
|
+
|
36
|
+
assert_should_require_attribute( options[:key], options[:value] )
|
37
|
+
assert_should_require_unique_attribute( options[:key], options[:value] )
|
38
|
+
assert_should_require_attribute_length( options[:key], options[:value],
|
39
|
+
:maximum => 250 )
|
40
|
+
|
41
|
+
test "should find by key with ['string']" do
|
42
|
+
object = create_object
|
43
|
+
assert object.is_a?(model.constantize)
|
44
|
+
found = (model.constantize)[object.key.to_s]
|
45
|
+
assert found.is_a?(model.constantize)
|
46
|
+
assert_equal object, found
|
47
|
+
end
|
48
|
+
|
49
|
+
test "should find by key with [:symbol]" do
|
50
|
+
object = create_object
|
51
|
+
assert object.is_a?(model.constantize)
|
52
|
+
found = (model.constantize)[object.key.to_sym]
|
53
|
+
assert found.is_a?(model.constantize)
|
54
|
+
assert_equal object, found
|
55
|
+
end
|
56
|
+
|
57
|
+
end # def assert_should_behave_like_a_hash(*args)
|
58
|
+
|
59
|
+
def assert_should_accept_only_good_values(*attributes)
|
60
|
+
options = {
|
61
|
+
:good_values => [],
|
62
|
+
:bad_values => []
|
63
|
+
}
|
64
|
+
options.update(attributes.extract_options!)
|
65
|
+
model = options[:model] || model_name_without_test
|
66
|
+
|
67
|
+
attributes.flatten.each do |field|
|
68
|
+
|
69
|
+
[options[:bad_values]].flatten.each do |value|
|
70
|
+
|
71
|
+
# could be a naming problem if both nil and blank are passed
|
72
|
+
|
73
|
+
test "should NOT allow #{value||'nil'} for #{field}" do
|
74
|
+
# object = model.constantize.new(field => value)
|
75
|
+
# what if field is protected?
|
76
|
+
object = model.constantize.new
|
77
|
+
object.send("#{field}=", value)
|
78
|
+
assert_equal object.send(field), value
|
79
|
+
object.valid?
|
80
|
+
assert object.errors.matching?(field,'is not included in the list')
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
[options[:good_values]].flatten.each do |value|
|
86
|
+
|
87
|
+
# could be a naming problem if both nil and blank are passed
|
88
|
+
|
89
|
+
test "should allow #{value||'nil'} for #{field}" do
|
90
|
+
# object = model.constantize.new(field => value)
|
91
|
+
# what if field is protected?
|
92
|
+
object = model.constantize.new
|
93
|
+
object.send("#{field}=", value)
|
94
|
+
assert_equal object.send(field), value
|
95
|
+
object.valid?
|
96
|
+
assert !object.errors.include?(field)
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end # attributes.flatten.each do |field|
|
102
|
+
|
103
|
+
end # def assert_should_accept_only_good_values(*attributes)
|
104
|
+
|
105
|
+
end # module ClassMethods
|
106
|
+
|
107
|
+
end # module ActiveSupportExtension::Assertions
|
108
|
+
ActiveSupport::TestCase.send(:include,CommonLib::ActiveSupportExtension::Assertions)
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module CommonLib::ActiveSupportExtension::Associations
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.class_eval do
|
6
|
+
class << self
|
7
|
+
alias_methods = {
|
8
|
+
:should_have_many => :should_have_many_,
|
9
|
+
:should_have_many_associations => :should_have_many_
|
10
|
+
}
|
11
|
+
alias_methods.each do |alias_name,method_name|
|
12
|
+
alias_method( "assert_#{alias_name}",
|
13
|
+
"assert_#{method_name}" ) unless
|
14
|
+
self.method_defined?("assert_#{alias_name}")
|
15
|
+
end # alias_methods.each
|
16
|
+
end # class << self
|
17
|
+
end # base.class_eval
|
18
|
+
end # def self.included
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
|
22
|
+
def assert_should_initially_belong_to(*associations)
|
23
|
+
options = associations.extract_options!
|
24
|
+
model = options[:model] || model_name_without_test
|
25
|
+
associations.each do |assoc|
|
26
|
+
class_name = ( assoc = assoc.to_s ).camelize
|
27
|
+
title = "#{brand}should initially belong to #{assoc}"
|
28
|
+
if !options[:class_name].blank?
|
29
|
+
title << " ( #{options[:class_name]} )"
|
30
|
+
class_name = options[:class_name].to_s
|
31
|
+
end
|
32
|
+
test title do
|
33
|
+
object = create_object
|
34
|
+
assert_not_nil object.send(assoc)
|
35
|
+
if object.send(assoc).respond_to?(
|
36
|
+
"#{model.underscore.pluralize}_count")
|
37
|
+
assert_equal 1, object.reload.send(assoc).send(
|
38
|
+
"#{model.underscore.pluralize}_count")
|
39
|
+
end
|
40
|
+
if !options[:class_name].blank?
|
41
|
+
assert object.send(assoc).is_a?(class_name.constantize)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def assert_should_belong_to(*associations)
|
48
|
+
options = associations.extract_options!
|
49
|
+
model = options[:model] || model_name_without_test
|
50
|
+
associations.each do |assoc|
|
51
|
+
class_name = ( assoc = assoc.to_s ).camelize
|
52
|
+
title = "#{brand}should belong to #{assoc}"
|
53
|
+
if !options[:class_name].blank?
|
54
|
+
title << " ( #{options[:class_name]} )"
|
55
|
+
class_name = options[:class_name].to_s
|
56
|
+
end
|
57
|
+
test title do
|
58
|
+
object = create_object
|
59
|
+
assert_nil object.send(assoc)
|
60
|
+
object.send("#{assoc}=",send("create_#{class_name.underscore}"))
|
61
|
+
assert_not_nil object.send(assoc)
|
62
|
+
assert object.send(assoc).is_a?(class_name.constantize
|
63
|
+
) unless options[:polymorphic]
|
64
|
+
# Paperclip attachments don't get deleted on rollback.
|
65
|
+
# So we much destroy the object, and therefore the attachment, by hand.
|
66
|
+
# Only seems to matter with ...
|
67
|
+
# BirthDatumTest#assert_should_belong_to( :birth_datum_update )
|
68
|
+
# but does't seem to cause a problem elsewhere.
|
69
|
+
object.send(assoc).destroy
|
70
|
+
object.destroy
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def assert_should_have_one(*associations)
|
76
|
+
options = associations.extract_options!
|
77
|
+
model = options[:model] || model_name_without_test
|
78
|
+
associations.each do |assoc|
|
79
|
+
assoc = assoc.to_s
|
80
|
+
test "#{brand}should have one #{assoc}" do
|
81
|
+
object = create_object
|
82
|
+
assert_nil object.reload.send(assoc)
|
83
|
+
send("create_#{assoc}", model.underscore => object )
|
84
|
+
assert_not_nil object.reload.send(assoc)
|
85
|
+
object.send(assoc).destroy
|
86
|
+
assert_nil object.reload.send(assoc)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def assert_should_have_many_(*associations)
|
92
|
+
options = associations.extract_options!
|
93
|
+
model = options[:model] || model_name_without_test
|
94
|
+
associations.each do |assoc|
|
95
|
+
class_name = ( assoc = assoc.to_s ).camelize
|
96
|
+
title = "#{brand}should have many #{assoc}"
|
97
|
+
if !options[:class_name].blank?
|
98
|
+
title << " ( #{options[:class_name]} )"
|
99
|
+
class_name = options[:class_name].to_s
|
100
|
+
end
|
101
|
+
if !options[:as].blank?
|
102
|
+
title << " ( as #{options[:as]} )"
|
103
|
+
end
|
104
|
+
test title do
|
105
|
+
object = create_object
|
106
|
+
assert_equal 0, object.send(assoc).length
|
107
|
+
command = ["create_#{class_name.singularize.underscore}"]
|
108
|
+
if !options[:foreign_key].blank?
|
109
|
+
command.push( options[:foreign_key].to_sym => object.id )
|
110
|
+
elsif !options[:as].blank?
|
111
|
+
command.push( options[:as].to_sym => object )
|
112
|
+
else
|
113
|
+
command.push( model.underscore => object )
|
114
|
+
end
|
115
|
+
send *command
|
116
|
+
assert_equal 1, object.reload.send(assoc).length
|
117
|
+
if object.respond_to?("#{assoc}_count")
|
118
|
+
assert_equal 1, object.reload.send("#{assoc}_count")
|
119
|
+
end
|
120
|
+
send *command
|
121
|
+
assert_equal 2, object.reload.send(assoc).length
|
122
|
+
if object.respond_to?("#{assoc}_count")
|
123
|
+
assert_equal 2, object.reload.send("#{assoc}_count")
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def assert_should_habtm(*associations)
|
130
|
+
options = associations.extract_options!
|
131
|
+
model = options[:model] || model_name_without_test
|
132
|
+
associations.each do |assoc|
|
133
|
+
assoc = assoc.to_s
|
134
|
+
test "#{brand}should habtm #{assoc}" do
|
135
|
+
object = create_object
|
136
|
+
assert_equal 0, object.send(assoc).length
|
137
|
+
object.send(assoc) << send("create_#{assoc.singularize}")
|
138
|
+
assert_equal 1, object.reload.send(assoc).length
|
139
|
+
if object.respond_to?("#{assoc}_count")
|
140
|
+
assert_equal 1, object.reload.send("#{assoc}_count")
|
141
|
+
end
|
142
|
+
object.send(assoc) << send("create_#{assoc.singularize}")
|
143
|
+
assert_equal 2, object.reload.send(assoc).length
|
144
|
+
if object.respond_to?("#{assoc}_count")
|
145
|
+
assert_equal 2, object.reload.send("#{assoc}_count")
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
end # ClassMethods
|
152
|
+
|
153
|
+
end # module ActiveSupportExtension::Associations
|
154
|
+
ActiveSupport::TestCase.send(:include, CommonLib::ActiveSupportExtension::Associations)
|
@@ -0,0 +1,296 @@
|
|
1
|
+
module CommonLib::ActiveSupportExtension::Attributes
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.class_eval do
|
6
|
+
class << self
|
7
|
+
alias_methods = {
|
8
|
+
:not_require_unique_attributes => :not_require_unique_attribute,
|
9
|
+
:not_require_unique => :not_require_unique_attribute,
|
10
|
+
:require_unique_attributes => :require_unique_attribute,
|
11
|
+
:require_unique => :require_unique_attribute,
|
12
|
+
:require_attributes_not_nil => :require_attribute_not_nil,
|
13
|
+
:require_not_nil => :require_attribute_not_nil,
|
14
|
+
:require_attributes => :require_attribute,
|
15
|
+
:require => :require_attribute,
|
16
|
+
:not_require_attributes => :not_require_attribute,
|
17
|
+
:not_require => :not_require_attribute,
|
18
|
+
:require_attributes_length => :require_attribute_length,
|
19
|
+
:require_length => :require_attribute_length,
|
20
|
+
:protect_attributes => :protect_attribute,
|
21
|
+
:protect => :protect_attribute,
|
22
|
+
:not_protect_attributes => :not_protect_attribute,
|
23
|
+
:not_protect => :not_protect_attribute
|
24
|
+
}
|
25
|
+
alias_methods.each do |alias_name,method_name|
|
26
|
+
alias_method( "assert_should_#{alias_name}",
|
27
|
+
"assert_should_#{method_name}" ) unless
|
28
|
+
self.method_defined?("assert_should_#{alias_name}")
|
29
|
+
end # alias_methods.each
|
30
|
+
end # class << self
|
31
|
+
end # base.class_eval
|
32
|
+
end # def self.included
|
33
|
+
|
34
|
+
module ClassMethods
|
35
|
+
|
36
|
+
def assert_should_not_require_unique_attribute(*attributes)
|
37
|
+
options = attributes.extract_options!
|
38
|
+
|
39
|
+
attributes.flatten.each do |attr|
|
40
|
+
attr = attr.to_s
|
41
|
+
title = "#{brand}should not require unique #{attr}"
|
42
|
+
scope = options[:scope]
|
43
|
+
unless scope.blank?
|
44
|
+
title << " scope "
|
45
|
+
title << (( scope.is_a?(Array) ) ? scope.join(',') : scope.to_s )
|
46
|
+
end
|
47
|
+
test title do
|
48
|
+
o = create_object
|
49
|
+
attrs = { attr.to_sym => o.send(attr) }
|
50
|
+
if( scope.is_a?(String) || scope.is_a?(Symbol) )
|
51
|
+
attrs[scope.to_sym] = o.send(scope.to_sym)
|
52
|
+
elsif scope.is_a?(Array)
|
53
|
+
scope.each do |s|
|
54
|
+
attrs[s.to_sym] = o.send(s.to_sym)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
object = create_object(attrs)
|
58
|
+
assert !object.errors.matching?(attr,'has already been taken'),
|
59
|
+
object.errors.full_messages.to_sentence
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def assert_should_require_unique_attribute(*attributes)
|
65
|
+
options = attributes.extract_options!
|
66
|
+
model = options[:model] || model_name_without_test
|
67
|
+
|
68
|
+
attributes.flatten.each do |attr|
|
69
|
+
attr = attr.to_s
|
70
|
+
title = "#{brand}should require unique #{attr}"
|
71
|
+
scope = options[:scope]
|
72
|
+
unless scope.blank?
|
73
|
+
title << " scope "
|
74
|
+
title << (( scope.is_a?(Array) ) ? scope.join(',') : scope.to_s)
|
75
|
+
end
|
76
|
+
test title do
|
77
|
+
o = create_object
|
78
|
+
assert_no_difference "#{model}.count" do
|
79
|
+
attrs = { attr.to_sym => o.send(attr) }
|
80
|
+
if( scope.is_a?(String) || scope.is_a?(Symbol) )
|
81
|
+
attrs[scope.to_sym] = o.send(scope.to_sym)
|
82
|
+
elsif scope.is_a?(Array)
|
83
|
+
scope.each do |s|
|
84
|
+
attrs[s.to_sym] = o.send(s.to_sym)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
object = create_object(attrs)
|
88
|
+
assert object.errors.matching?(attr,'has already been taken'),
|
89
|
+
object.errors.full_messages.to_sentence
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def assert_should_require_attribute_not_nil(*attributes)
|
96
|
+
options = attributes.extract_options!
|
97
|
+
model = options[:model] || model_name_without_test
|
98
|
+
|
99
|
+
attributes.flatten.each do |attr|
|
100
|
+
attr = attr.to_s
|
101
|
+
test "#{brand}should require #{attr} not nil" do
|
102
|
+
object = model.constantize.new
|
103
|
+
object.send("#{attr}=", nil)
|
104
|
+
assert !object.valid?
|
105
|
+
# message could be a number of things ...
|
106
|
+
# "is not included in the list","can't be blank"
|
107
|
+
assert object.errors.include?(attr.to_sym),
|
108
|
+
object.errors.full_messages.to_sentence
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def assert_should_require_attribute(*attributes)
|
114
|
+
options = attributes.extract_options!
|
115
|
+
model = options[:model] || model_name_without_test
|
116
|
+
|
117
|
+
attributes.flatten.each do |attr|
|
118
|
+
attr = attr.to_s
|
119
|
+
test "#{brand}should require #{attr}" do
|
120
|
+
object = model.constantize.new
|
121
|
+
object.send("#{attr}=", nil)
|
122
|
+
assert !object.valid?
|
123
|
+
assert object.errors.include?(attr.to_sym),
|
124
|
+
object.errors.full_messages.to_sentence
|
125
|
+
assert object.errors.matching?(attr,"can't be blank") ||
|
126
|
+
object.errors.matching?(attr,'is too short'),
|
127
|
+
object.errors.full_messages.to_sentence
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def assert_should_not_require_attribute(*attributes)
|
133
|
+
options = attributes.extract_options!
|
134
|
+
model = options[:model] || model_name_without_test
|
135
|
+
|
136
|
+
attributes.flatten.each do |attr|
|
137
|
+
attr = attr.to_s
|
138
|
+
test "#{brand}should not require #{attr}" do
|
139
|
+
object = model.constantize.new
|
140
|
+
object.send("#{attr}=", nil)
|
141
|
+
# don't know if it will be true or false, but must be called
|
142
|
+
object.valid?
|
143
|
+
assert !object.errors.include?(attr.to_sym),
|
144
|
+
object.errors.full_messages.to_sentence
|
145
|
+
if attr =~ /^(.*)_id$/
|
146
|
+
assert !object.errors.include?($1.to_sym),
|
147
|
+
object.errors.full_messages.to_sentence
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def assert_should_require_attribute_length(*attributes)
|
154
|
+
options = attributes.extract_options!
|
155
|
+
model = options[:model] || model_name_without_test
|
156
|
+
|
157
|
+
if( ( options.keys & [:in,:within] ).length >= 1 )
|
158
|
+
range = options[:in]||options[:within]
|
159
|
+
options[:minimum] = range.min
|
160
|
+
options[:maximum] = range.max
|
161
|
+
end
|
162
|
+
|
163
|
+
attributes.flatten.each do |attr|
|
164
|
+
attr = attr.to_s
|
165
|
+
if options.keys.include?(:is)
|
166
|
+
length = options[:is]
|
167
|
+
test "#{brand}should require exact length of #{length} for #{attr}" do
|
168
|
+
value = 'x'*(length-1)
|
169
|
+
object = model.constantize.new
|
170
|
+
object.send("#{attr}=", value)
|
171
|
+
assert !object.valid?
|
172
|
+
assert_equal length-1, object.send(attr.to_sym).length
|
173
|
+
assert_equal object.send(attr.to_sym), value
|
174
|
+
assert object.errors.include?(attr.to_sym),
|
175
|
+
object.errors.full_messages.to_sentence
|
176
|
+
assert object.errors.matching?(attr,'is the wrong length'),
|
177
|
+
object.errors.full_messages.to_sentence
|
178
|
+
|
179
|
+
value = 'x'*(length+1)
|
180
|
+
object = model.constantize.new
|
181
|
+
object.send("#{attr}=", value)
|
182
|
+
assert !object.valid?
|
183
|
+
assert_equal length+1, object.send(attr.to_sym).length
|
184
|
+
assert_equal object.send(attr.to_sym), value
|
185
|
+
assert object.errors.include?(attr.to_sym),
|
186
|
+
object.errors.full_messages.to_sentence
|
187
|
+
assert object.errors.matching?(attr,'is the wrong length'),
|
188
|
+
object.errors.full_messages.to_sentence
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
if options.keys.include?(:minimum)
|
193
|
+
min = options[:minimum]
|
194
|
+
test "#{brand}should require min length of #{min} for #{attr}" do
|
195
|
+
value = 'x'*(min)
|
196
|
+
object = model.constantize.new
|
197
|
+
object.send("#{attr}=", value)
|
198
|
+
# don't know if really valid
|
199
|
+
object.valid?
|
200
|
+
assert_equal min, object.send(attr.to_sym).length
|
201
|
+
assert_equal object.send(attr.to_sym), value
|
202
|
+
assert !object.errors.matching?(attr,'is too short'),
|
203
|
+
object.errors.full_messages.to_sentence
|
204
|
+
|
205
|
+
value = 'x'*(min-1)
|
206
|
+
object = model.constantize.new
|
207
|
+
object.send("#{attr}=", value)
|
208
|
+
assert !object.valid?
|
209
|
+
assert_equal min-1, object.send(attr.to_sym).length
|
210
|
+
assert_equal object.send(attr.to_sym), value
|
211
|
+
assert object.errors.include?(attr.to_sym),
|
212
|
+
object.errors.full_messages.to_sentence
|
213
|
+
assert object.errors.matching?(attr,'is too short'),
|
214
|
+
object.errors.full_messages.to_sentence
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
if options.keys.include?(:maximum)
|
219
|
+
max = options[:maximum]
|
220
|
+
test "#{brand}should require max length of #{max} for #{attr}" do
|
221
|
+
value = 'x'*(max)
|
222
|
+
object = model.constantize.new
|
223
|
+
object.send("#{attr}=", value)
|
224
|
+
# don't know if really valid
|
225
|
+
object.valid?
|
226
|
+
assert_equal max, object.send(attr.to_sym).length
|
227
|
+
assert_equal object.send(attr.to_sym), value
|
228
|
+
assert !object.errors.matching?(attr,'is too long'),
|
229
|
+
object.errors.full_messages.to_sentence
|
230
|
+
|
231
|
+
value = 'x'*(max+1)
|
232
|
+
object = model.constantize.new
|
233
|
+
object.send("#{attr}=", value)
|
234
|
+
assert !object.valid?
|
235
|
+
assert_equal max+1, object.send(attr.to_sym).length
|
236
|
+
assert_equal object.send(attr.to_sym), value
|
237
|
+
assert object.errors.include?(attr.to_sym),
|
238
|
+
object.errors.full_messages.to_sentence
|
239
|
+
assert object.errors.matching?(attr,'is too long'),
|
240
|
+
object.errors.full_messages.to_sentence
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
def assert_should_protect_attribute(*attributes)
|
248
|
+
options = attributes.extract_options!
|
249
|
+
model = options[:model] || model_name_without_test
|
250
|
+
|
251
|
+
attributes.flatten.each do |attr|
|
252
|
+
attr = attr.to_s
|
253
|
+
test "#{brand}should protect attribute #{attr}" do
|
254
|
+
assert model.constantize.accessible_attributes||model.constantize.protected_attributes,
|
255
|
+
"Both accessible and protected attributes are empty"
|
256
|
+
assert !(model.constantize.accessible_attributes||[]).include?(attr),
|
257
|
+
"#{attr} is included in accessible attributes"
|
258
|
+
if !model.constantize.protected_attributes.nil?
|
259
|
+
assert model.constantize.protected_attributes.include?(attr),
|
260
|
+
"#{attr} is not included in protected attributes"
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
# >> Abstract.accessible_attributes
|
267
|
+
# => #<ActiveModel::MassAssignmentSecurity::WhiteList: {}>
|
268
|
+
|
269
|
+
# >> Abstract.protected_attributes
|
270
|
+
# => #<ActiveModel::MassAssignmentSecurity::BlackList: {"study_subject", "entry_2_by_uid", "entry_1_by_uid", "id", "type", "study_subject_id", "merged_by_uid"}>
|
271
|
+
|
272
|
+
def assert_should_not_protect_attribute(*attributes)
|
273
|
+
options = attributes.extract_options!
|
274
|
+
model = options[:model] || model_name_without_test
|
275
|
+
|
276
|
+
attributes.flatten.each do |attr|
|
277
|
+
attr = attr.to_s
|
278
|
+
test "#{brand}should not protect attribute #{attr}" do
|
279
|
+
assert !model.constantize.protected_attributes.include?(attr),
|
280
|
+
"#{attr} is included in protected attributes"
|
281
|
+
|
282
|
+
# Rails 3 change
|
283
|
+
# apparently no longer always true
|
284
|
+
# if !model.accessible_attributes.nil?
|
285
|
+
# assert model.accessible_attributes.include?(attr),
|
286
|
+
# "#{attr} is not included in accessible attributes"
|
287
|
+
# end
|
288
|
+
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
end # module ActiveSupportExtension::Attributes
|
296
|
+
ActiveSupport::TestCase.send(:include, CommonLib::ActiveSupportExtension::Attributes)
|