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 CHANGED
@@ -1,9 +1,9 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activerecord (2.3.10)
5
- activesupport (= 2.3.10)
6
- activesupport (2.3.10)
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.10)
21
- activesupport (= 2.3.10)
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
@@ -3,7 +3,7 @@ require 'rake/testtask'
3
3
  require 'rake/rdoctask'
4
4
  require 'rubygems'
5
5
 
6
- ActiveRecordVersions = %w(3.0.1 2.3.10)
6
+ ActiveRecordVersions = %w(3.0.1 2.3.10 2.3.5)
7
7
 
8
8
  desc "Run all tests"
9
9
  task :test do
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.2
1
+ 1.2.3
@@ -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
- @model_class.scoped.apply_finder_options(*args).arel.to_sql
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
- @sequence_number = 0
65
- @validations = {}
69
+ @value_streams = []
66
70
  end
67
71
 
68
- def add(type, config)
69
- @validations[type] = config
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 association
73
- @model.belongs_to_associations.detect { |a|
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 column
79
- @model.columns.detect { |c| c.name == @field.to_s }
89
+ def includes_uniqueness?
90
+ @value_streams.any? { |vs| vs.is_a?(ValidatesUniquenessOfValueStream) }
80
91
  end
81
92
 
82
- def includes_presence?
83
- @validations.has_key?(:validates_presence_of)
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
- def includes_uniqueness?
87
- @validations.has_key?(:validates_uniqueness_of)
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
- def satisfying_present_associated_value
91
- value = if includes_uniqueness?
92
- association.klass.create_sample
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
- def satisfying_present_value(prev_value)
101
- if association
102
- satisfying_present_associated_value
103
- else
104
- if prev_value.present?
105
- prev_value
106
- elsif column && column.type == :date
107
- Date.today
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
- "#{@field} #{@sequence_number}"
145
+ super
110
146
  end
111
147
  end
112
- end
113
148
 
114
- def satisfying_value
115
- @sequence_number += 1 if includes_uniqueness?
116
- value = nil
117
- @validations.each do |type, config|
118
- case type
119
- when :validates_email_format_of
120
- value = "john.doe#{@sequence_number}@example.com"
121
- when :validates_inclusion_of
122
- value = config[:in].first
123
- when :validates_presence_of
124
- value = satisfying_present_value(value)
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
- def unique_value
132
- if column.type == :string
133
- "#{@field.to_s.capitalize} #{@sequence_number}"
134
- elsif column.type == :datetime
135
- Time.utc(1970, 1, 1) + @sequence_number.days
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
@@ -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.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-05-29}
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
 
@@ -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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 2
10
- version: 1.2.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-05-29 00:00:00 -04:00
18
+ date: 2011-06-11 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency