sample_models 1.2.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +5 -5
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/sample_models/model.rb +102 -52
- data/sample_models.gemspec +2 -2
- data/spec_or_test/setup.rb +9 -0
- data/spec_or_test/specs_or_test_cases.rb +10 -3
- metadata +4 -4
data/Gemfile.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activerecord (2.3.
|
5
|
-
activesupport (= 2.3.
|
6
|
-
activesupport (2.3.
|
4
|
+
activerecord (2.3.5)
|
5
|
+
activesupport (= 2.3.5)
|
6
|
+
activesupport (2.3.5)
|
7
7
|
git (1.2.5)
|
8
8
|
jeweler (1.6.0)
|
9
9
|
bundler (~> 1.0.0)
|
@@ -17,8 +17,8 @@ PLATFORMS
|
|
17
17
|
ruby
|
18
18
|
|
19
19
|
DEPENDENCIES
|
20
|
-
activerecord (= 2.3.
|
21
|
-
activesupport (= 2.3.
|
20
|
+
activerecord (= 2.3.5)
|
21
|
+
activesupport (= 2.3.5)
|
22
22
|
bundler (~> 1.0.0)
|
23
23
|
jeweler (~> 1.6.0)
|
24
24
|
sqlite3
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.2.
|
1
|
+
1.2.3
|
data/lib/sample_models/model.rb
CHANGED
@@ -32,8 +32,9 @@ module SampleModels
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def construct_finder_sql(*args)
|
35
|
-
if @model_class.method(:scoped).arity == -1
|
36
|
-
|
35
|
+
if @model_class.method(:scoped).arity == -1 &&
|
36
|
+
@model_class.scoped(nil).respond_to?(:apply_finder_options)
|
37
|
+
@model_class.scoped(nil).apply_finder_options(*args).arel.to_sql
|
37
38
|
else
|
38
39
|
@model_class.send(:construct_finder_sql, *args)
|
39
40
|
end
|
@@ -57,82 +58,131 @@ module SampleModels
|
|
57
58
|
def validates_presence_of?(attr)
|
58
59
|
@validation_collections[attr].includes_presence?
|
59
60
|
end
|
60
|
-
|
61
|
+
|
62
|
+
def unique?(field, value)
|
63
|
+
!@model_class.first(:conditions => {field => value})
|
64
|
+
end
|
65
|
+
|
61
66
|
class ValidationCollection
|
62
67
|
def initialize(model, field)
|
63
68
|
@model, @field = model, field
|
64
|
-
@
|
65
|
-
@validations = {}
|
69
|
+
@value_streams = []
|
66
70
|
end
|
67
71
|
|
68
|
-
def add(
|
69
|
-
|
72
|
+
def add(validation_method, config)
|
73
|
+
stream_class_name = validation_method.to_s.camelize + 'ValueStream'
|
74
|
+
if ValidationCollection.const_defined?(stream_class_name)
|
75
|
+
stream_class = ValidationCollection.const_get(stream_class_name)
|
76
|
+
if stream_class == ValidatesUniquenessOfValueStream
|
77
|
+
@validates_uniqueness_config = config
|
78
|
+
else
|
79
|
+
input = @value_streams.last
|
80
|
+
@value_streams << stream_class.new(@model, @field, config, input)
|
81
|
+
end
|
82
|
+
end
|
70
83
|
end
|
71
84
|
|
72
|
-
def
|
73
|
-
@
|
74
|
-
a.association_foreign_key.to_sym == @field.to_sym
|
75
|
-
}
|
85
|
+
def includes_presence?
|
86
|
+
@value_streams.any? { |vs| vs.is_a?(ValidatesPresenceOfValueStream) }
|
76
87
|
end
|
77
88
|
|
78
|
-
def
|
79
|
-
@
|
89
|
+
def includes_uniqueness?
|
90
|
+
@value_streams.any? { |vs| vs.is_a?(ValidatesUniquenessOfValueStream) }
|
80
91
|
end
|
81
92
|
|
82
|
-
def
|
83
|
-
@
|
93
|
+
def satisfying_value
|
94
|
+
if @validates_uniqueness_config
|
95
|
+
input = @value_streams.last
|
96
|
+
@value_streams << ValidatesUniquenessOfValueStream.new(
|
97
|
+
@model, @field, @validates_uniqueness_config, input
|
98
|
+
)
|
99
|
+
@validates_uniqueness_config = nil
|
100
|
+
end
|
101
|
+
@value_streams.last.satisfying_value if @value_streams.last
|
84
102
|
end
|
85
103
|
|
86
|
-
|
87
|
-
|
104
|
+
class ValueStream
|
105
|
+
attr_reader :input
|
106
|
+
|
107
|
+
def initialize(model, field, config, input)
|
108
|
+
@model, @field, @config, @input = model, field, config, input
|
109
|
+
@sequence_number = 0
|
110
|
+
end
|
111
|
+
|
112
|
+
def column
|
113
|
+
@model.columns.detect { |c| c.name == @field.to_s }
|
114
|
+
end
|
115
|
+
|
116
|
+
def increment
|
117
|
+
@sequence_number += 1
|
118
|
+
input.increment if input
|
119
|
+
end
|
88
120
|
end
|
89
121
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
else
|
94
|
-
association.klass.first || association.klass.sample
|
122
|
+
class ValidatesEmailFormatOfValueStream < ValueStream
|
123
|
+
def satisfying_value
|
124
|
+
"john.doe#{@sequence_number}@example.com"
|
95
125
|
end
|
96
|
-
value = value.id if value
|
97
|
-
value
|
98
126
|
end
|
99
127
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
128
|
+
class ValidatesInclusionOfValueStream < ValueStream
|
129
|
+
def satisfying_value
|
130
|
+
@config[:in].first
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
class ValidatesPresenceOfValueStream < ValueStream
|
135
|
+
def association
|
136
|
+
@model.belongs_to_associations.detect { |a|
|
137
|
+
a.association_foreign_key.to_sym == @field.to_sym
|
138
|
+
}
|
139
|
+
end
|
140
|
+
|
141
|
+
def increment
|
142
|
+
if association
|
143
|
+
association.klass.create_sample
|
108
144
|
else
|
109
|
-
|
145
|
+
super
|
110
146
|
end
|
111
147
|
end
|
112
|
-
end
|
113
148
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
149
|
+
def satisfying_associated_value
|
150
|
+
value = association.klass.last || association.klass.sample
|
151
|
+
value = value.id if value
|
152
|
+
value
|
153
|
+
end
|
154
|
+
|
155
|
+
def satisfying_value
|
156
|
+
prev_value = input.satisfying_value if input
|
157
|
+
if association
|
158
|
+
satisfying_associated_value
|
159
|
+
else
|
160
|
+
if prev_value.present?
|
161
|
+
prev_value
|
162
|
+
elsif column && column.type == :date
|
163
|
+
Date.today + @sequence_number
|
164
|
+
elsif column && column.type == :datetime
|
165
|
+
Time.utc(1970, 1, 1) + @sequence_number.days
|
166
|
+
elsif column && column.type == :integer
|
167
|
+
@sequence_number
|
168
|
+
else
|
169
|
+
"#{@field} #{@sequence_number}"
|
170
|
+
end
|
125
171
|
end
|
126
172
|
end
|
127
|
-
value = unique_value if value.nil? && includes_uniqueness?
|
128
|
-
value
|
129
173
|
end
|
130
174
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
175
|
+
class ValidatesUniquenessOfValueStream < ValueStream
|
176
|
+
def satisfying_value
|
177
|
+
value = input.satisfying_value if input
|
178
|
+
if !@model.unique?(@field, value)
|
179
|
+
my_input = input || ValidatesPresenceOfValueStream.new(@model, @field, nil, @input)
|
180
|
+
until @model.unique?(@field, value)
|
181
|
+
my_input.increment
|
182
|
+
value = my_input.satisfying_value
|
183
|
+
end
|
184
|
+
end
|
185
|
+
value
|
136
186
|
end
|
137
187
|
end
|
138
188
|
end
|
data/sample_models.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sample_models}
|
8
|
-
s.version = "1.2.
|
8
|
+
s.version = "1.2.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Francis Hwang"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-06-11}
|
13
13
|
s.description = %q{
|
14
14
|
A library for making it extremely fast for Rails developers to set up and save ActiveRecord instances when writing test cases. It aims to:
|
15
15
|
|
data/spec_or_test/setup.rb
CHANGED
@@ -92,6 +92,10 @@ def initialize_db
|
|
92
92
|
user.string 'email', 'gender', 'homepage', 'login', 'password'
|
93
93
|
end
|
94
94
|
|
95
|
+
create_table 'user2s', :force => true do |user2|
|
96
|
+
user2.string 'login'
|
97
|
+
end
|
98
|
+
|
95
99
|
create_table 'user_with_passwords', :force => true do |user|
|
96
100
|
end
|
97
101
|
|
@@ -195,6 +199,11 @@ class User < ActiveRecord::Base
|
|
195
199
|
validates_uniqueness_of :email, :login, :case_sensitive => false
|
196
200
|
end
|
197
201
|
|
202
|
+
class User2 < ActiveRecord::Base
|
203
|
+
validates_presence_of :login
|
204
|
+
validates_uniqueness_of :login
|
205
|
+
end
|
206
|
+
|
198
207
|
class UserWithPassword < ActiveRecord::Base
|
199
208
|
attr_accessor :password
|
200
209
|
|
@@ -91,6 +91,7 @@ describe "Model.sample" do
|
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
|
+
|
94
95
|
describe 'Model with a belongs_to association' do
|
95
96
|
it 'should be associated with the belongs_to recipient by default' do
|
96
97
|
blog_post = BlogPost.sample
|
@@ -333,7 +334,6 @@ describe 'Model with a unique associated attribute' do
|
|
333
334
|
end
|
334
335
|
end
|
335
336
|
|
336
|
-
|
337
337
|
describe 'Model with a unique scoped associated attribute' do
|
338
338
|
it 'should create a new instance when you create_sample with the same scope variable as before' do
|
339
339
|
video_fav1 = VideoFavorite.sample
|
@@ -541,13 +541,13 @@ describe 'Model with a named sample' do
|
|
541
541
|
assert_equal 'Funny haha', bp.title
|
542
542
|
assert_equal 3.0, bp.average_rating
|
543
543
|
end
|
544
|
-
|
544
|
+
|
545
545
|
it 'should allow you to override fields' do
|
546
546
|
bp = BlogPost.sample :funny, :average_rating => 4.0
|
547
547
|
assert_equal 'Funny haha', bp.title
|
548
548
|
assert_equal 4.0, bp.average_rating
|
549
549
|
end
|
550
|
-
|
550
|
+
|
551
551
|
it 'should let you create_sample too' do
|
552
552
|
bp1 = BlogPost.sample :funny
|
553
553
|
bp2 = BlogPost.create_sample :funny
|
@@ -569,3 +569,10 @@ describe 'Model with a required accessor' do
|
|
569
569
|
end
|
570
570
|
end
|
571
571
|
|
572
|
+
describe 'Model which validates the presence and uniqueness of a string field' do
|
573
|
+
it "should not get tripped up by a pre-existing record" do
|
574
|
+
User2.create!(:login => 'login 1')
|
575
|
+
User2.create_sample
|
576
|
+
end
|
577
|
+
end
|
578
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sample_models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 3
|
10
|
+
version: 1.2.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Francis Hwang
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-11 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|