-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid_attributes.rb
executable file
·65 lines (58 loc) · 1.93 KB
/
valid_attributes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module ValidAttributes
# create a valid new record
# attributes are set separetly(no mass-assignment)
# valid User or valid 'user' or valid User, :name=>'Peter'
def valid(type,attributes={})
klas = type.to_s.camelcase.constantize
record = klas.new
valid_attributes(klas.to_s.underscore).merge(attributes).each do |field,value|
record.send("#{field}=",value)
end
record
end
def create_valid(type,attributes={})
record = valid(type,attributes)
record.save!
record
end
def valid_attributes(record_name,attributes={})
ValidAttributesHelper.valid_attributes(record_name).merge(attributes.to_options)
end
#example: User, :login=>['',nil,'admin'], :email=>['',nil,'aa','@','a@','@a']
def assert_invalid_attributes(model_class, attributes)
attributes.each_pair do |attribute, value|
assert_invalid_value model_class, attribute, value
end
end
#example: User, :login, ['',nil,'admin']
def assert_invalid_value(klas, attribute, values)
[*values].each do |value|
record = valid klas
record.send(attribute.to_s+'=',value)
assert_block "<#{klas}.#{attribute}> expected to be invalid when set to <#{value}>" do
record.valid? # Must be called to generate the errors
record.errors.invalid? attribute
end
end
end
end
class ValidAttributesHelper
cattr_accessor :stored_valid_attributes
def self.valid_attributes(record_name)
name = record_name.to_s.underscore
self.stored_valid_attributes ||= YAML::load_file(path_to_valid_yml)
begin
attrs = stored_valid_attributes[name].to_options!
rescue
raise "attributes for #{name} not found! --> #{path_to_valid_yml}"
end
attrs
end
def self.path_to_valid_yml
%w[test spec].each do |folder|
file = File.join(Rails.root, folder, 'valid_attributes.yml')
return file if File.exist?(file)
end
raise "create valid_attributes.yml in test/ or spec/"
end
end