mleung-koujou 0.0.5 → 0.0.6

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.
@@ -11,7 +11,7 @@ require 'koujou/validation_reflection'
11
11
  require 'koujou/custom_validation'
12
12
 
13
13
  module Koujou
14
- VERSION = '0.0.5'
14
+ VERSION = '0.0.6'
15
15
  end
16
16
 
17
17
  if ENV["RAILS_ENV"] == "test"
@@ -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 { |o| return options[o.to_sym].entries.first if options.has_key?(o.to_sym) }
187
- # These other validations should just return the value set.
188
- %w(is minimum maximum).each { |o| return options[o.to_sym] if options.has_key?(o.to_sym) }
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
- return format_if_sequenced(Faker::Internet.email) if @validation.name.to_s.match(/email/)
35
- return format_if_sequenced(Faker::Name.first_name) if @validation.name.to_s == 'first_name'
36
- return format_if_sequenced(Faker::Name.last_name) if @validation.name.to_s == 'last_name'
37
- return format_if_sequenced(Faker::Internet.user_name) if @validation.name.to_s.match(/login|user_name/)
38
- return format_if_sequenced(Faker::Address.city) if @validation.name.to_s.match(/city/)
39
- return format_if_sequenced(Faker::Address.us_state) if @validation.name.to_s.match(/state|province/)
40
- return format_if_sequenced(Faker::Address.zip_code) if @validation.name.to_s.match(/zip|postal/)
41
-
42
- # If we don't match any standard stuff, just return a regular bs lorem string comprised of 10 words.
43
- # 10 is sort of a "magic number" I might make a constant for that.
44
- standard_text = format_if_sequenced(Faker::Lorem.words(10).to_s)
45
- # So if there's a length validation set, we need to return just that amount of data.
46
- standard_text = standard_text[0..@required_length - 1].to_s if @required_length
47
- standard_text
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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mleung-koujou
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Leung