mleung-koujou 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/koujou.rb +1 -1
- data/lib/koujou/builder.rb +13 -8
- data/lib/koujou/data_generator.rb +26 -14
- data/lib/koujou/validation_reflection.rb +2 -2
- data/test/test_builder.rb +5 -1
- metadata +1 -1
data/lib/koujou.rb
CHANGED
data/lib/koujou/builder.rb
CHANGED
@@ -101,7 +101,6 @@ module Koujou #:nodoc:
|
|
101
101
|
end
|
102
102
|
end
|
103
103
|
end
|
104
|
-
|
105
104
|
|
106
105
|
def set_confirmation_attributes!(instance, attributes)
|
107
106
|
instance.class.confirmation_validations.each do |v|
|
@@ -111,7 +110,6 @@ module Koujou #:nodoc:
|
|
111
110
|
end
|
112
111
|
end
|
113
112
|
|
114
|
-
|
115
113
|
def create_associations(instance, recursed_from_model = nil)
|
116
114
|
# We loop through all the has_one or belongs_to associations on the current instance
|
117
115
|
# using introspection, and build up and assign some models to each, if the user has
|
@@ -181,19 +179,26 @@ module Koujou #:nodoc:
|
|
181
179
|
|
182
180
|
options = instance.class.length_validations.select{|v| v.name == validation.name }.first.options
|
183
181
|
|
182
|
+
retval = nil
|
184
183
|
# If the validation is validates_length_of :name, :within => 1..20 (or in, which is an alias),
|
185
184
|
# let's just return the minimum value of the range.
|
186
|
-
%w(within in).each
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
nil
|
185
|
+
%w(within in).each do |o|
|
186
|
+
retval = options[o.to_sym].entries.first if options.has_key?(o.to_sym)
|
187
|
+
break
|
188
|
+
end
|
189
|
+
if retval.nil?
|
190
|
+
# These other validations should just return the value set.
|
191
|
+
%w(is minimum maximum).each do |o|
|
192
|
+
retval = options[o.to_sym] if options.has_key?(o.to_sym)
|
193
|
+
break
|
194
|
+
end
|
195
|
+
end
|
196
|
+
retval
|
191
197
|
end
|
192
198
|
|
193
199
|
def get_inclusion_values(instance, validation)
|
194
200
|
return unless has_inclusion_validation?(instance, validation)
|
195
201
|
options = instance.class.inclusion_validations.select{|v| v.name == validation.name }.first.options
|
196
|
-
return unless options.has_key?(:in)
|
197
202
|
options[:in]
|
198
203
|
end
|
199
204
|
|
@@ -31,20 +31,32 @@ module Koujou #:nodoc:
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def generate_string
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
34
|
+
retval = ''
|
35
|
+
# I don't really like all these elsif's but, apparently
|
36
|
+
# it's more efficient than the numerous explicit returns
|
37
|
+
# we used to have. SEE: http://gist.github.com/160718
|
38
|
+
if @validation.name.to_s.match(/email/)
|
39
|
+
retval = format_if_sequenced(Faker::Internet.email)
|
40
|
+
elsif @validation.name.to_s == 'first_name'
|
41
|
+
retval = format_if_sequenced(Faker::Name.first_name)
|
42
|
+
elsif @validation.name.to_s == 'last_name'
|
43
|
+
retval = format_if_sequenced(Faker::Name.last_name)
|
44
|
+
elsif @validation.name.to_s.match(/login|user_name/)
|
45
|
+
retval = format_if_sequenced(Faker::Internet.user_name)
|
46
|
+
elsif @validation.name.to_s.match(/city/)
|
47
|
+
retval = format_if_sequenced(Faker::Address.city)
|
48
|
+
elsif @validation.name.to_s.match(/state|province/)
|
49
|
+
retval = format_if_sequenced(Faker::Address.us_state)
|
50
|
+
elsif @validation.name.to_s.match(/zip|postal/)
|
51
|
+
retval = format_if_sequenced(Faker::Address.zip_code)
|
52
|
+
else
|
53
|
+
# If we don't match any standard stuff, just return a regular bs lorem string comprised of 10 words.
|
54
|
+
# 10 is sort of a "magic number" I might make a constant for that.
|
55
|
+
standard_text = format_if_sequenced(Faker::Lorem.words(10).to_s)
|
56
|
+
# So if there's a length validation set, we need to return just that amount of data.
|
57
|
+
retval = @required_length ? standard_text[0..@required_length - 1].to_s : standard_text
|
58
|
+
end
|
59
|
+
retval
|
48
60
|
end
|
49
61
|
|
50
62
|
def generate_text
|
@@ -62,9 +62,9 @@ module Koujou # :nodoc:
|
|
62
62
|
ignore_subvalidations = false
|
63
63
|
base.class_eval <<-"end_eval"
|
64
64
|
class << self
|
65
|
-
def #{validation_type}_with_reflection(*attr_names)
|
65
|
+
def #{validation_type}_with_reflection(*attr_names, &block)
|
66
66
|
ignoring_subvalidations(#{ignore_subvalidations}) do
|
67
|
-
#{validation_type}_without_reflection(*attr_names)
|
67
|
+
#{validation_type}_without_reflection(*attr_names, &block)
|
68
68
|
remember_validation_metadata(:#{validation_type}, *attr_names)
|
69
69
|
end
|
70
70
|
end
|
data/test/test_builder.rb
CHANGED
@@ -90,7 +90,11 @@ class TestBuilder < Test::Unit::TestCase
|
|
90
90
|
p = Post.koujou_build(:name => clever)
|
91
91
|
assert_equal clever, p.name
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
|
+
should 'cause a model to be invalid if you override a field that has validates_presence_of with a nil value' do
|
95
|
+
u = User.koujou_build(:email => nil)
|
96
|
+
assert !u.valid?
|
97
|
+
end
|
94
98
|
end
|
95
99
|
|
96
100
|
context 'associations' do
|