sample_models 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -280,6 +280,18 @@ By default, SampleModels fills polymorphic associations with any record, chosen
280
280
  bookmark.bookmarkable.default_class BlogPost
281
281
  end
282
282
 
283
+ force_email_format
284
+ ------------------
285
+
286
+ Use `force_email_format` if you want to ensure that for every newly
287
+ created instance, the field will be a valid email. This has the same
288
+ effect as `validates_email_format_of`, but won't change the behavior of
289
+ production code.
290
+
291
+ SampleModels.configure(User) do |user|
292
+ user.email.force_email_format
293
+ end
294
+
283
295
  force_unique
284
296
  ------------
285
297
  Use `force_unique` if you want to ensure that for every newly created instance, the field will be unique. This has the same effect as `validates_uniqueness_of`, but won't change how production code behaves.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.1
1
+ 2.1.0
@@ -37,9 +37,9 @@ module SampleModels
37
37
  end
38
38
 
39
39
  class Builder
40
- def initialize(pass, model, column, force_unique)
41
- @pass, @model, @column, @force_unique =
42
- pass, model, column, force_unique
40
+ def initialize(pass, model, column, force_unique, force_email_format)
41
+ @pass, @model, @column, @force_unique, @force_email_format =
42
+ pass, model, column, force_unique, force_email_format
43
43
  end
44
44
 
45
45
  def base
@@ -51,6 +51,11 @@ module SampleModels
51
51
 
52
52
  def run
53
53
  input = base
54
+ if @force_email_format
55
+ input = ValidatesEmailFormatOfAttributeSequence.new(
56
+ @model, @column, nil, input
57
+ )
58
+ end
54
59
  uniqueness_validation = if @force_unique
55
60
  Model::Validation.new(:validates_uniqueness_of)
56
61
  end
@@ -8,6 +8,7 @@ module SampleModels
8
8
  @attribute_sequences = Hash.new { |h,k| h[k] = {} }
9
9
  @defaults = HashWithIndifferentAccess.new
10
10
  @forced_unique = []
11
+ @forced_email_format = []
11
12
  @named_samples = HashWithIndifferentAccess.new
12
13
  @polymorphic_default_classes = HashWithIndifferentAccess.new
13
14
  end
@@ -15,7 +16,8 @@ module SampleModels
15
16
  def attribute_sequence(pass, column)
16
17
  @attribute_sequences[pass][column.name] ||= begin
17
18
  AttributeSequence.build(
18
- pass, model, column, @forced_unique.include?(column.name)
19
+ pass, model, column, @forced_unique.include?(column.name),
20
+ @forced_email_format.include?(column.name)
19
21
  )
20
22
  end
21
23
  end
@@ -29,10 +31,18 @@ module SampleModels
29
31
  attribute_sequence(:first, column)
30
32
  end
31
33
 
34
+ def force_email_format(attr)
35
+ @forced_email_format << attr.to_s
36
+ end
37
+
32
38
  def force_unique(attr)
33
39
  @forced_unique << attr.to_s
34
40
  end
35
41
 
42
+ def force_email_format(attr)
43
+ @forced_email_format << attr.to_s
44
+ end
45
+
36
46
  def model
37
47
  SampleModels.models[@model_class]
38
48
  end
@@ -81,6 +91,10 @@ module SampleModels
81
91
  def default_class(dc)
82
92
  @sampler.polymorphic_default_classes[@attribute] = dc
83
93
  end
94
+
95
+ def force_email_format
96
+ @sampler.force_email_format(@attribute)
97
+ end
84
98
 
85
99
  def force_unique
86
100
  @sampler.force_unique(@attribute)
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "sample_models"
8
- s.version = "2.0.1"
8
+ s.version = "2.1.0"
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"]
data/test/setup/models.rb CHANGED
@@ -190,6 +190,10 @@ SampleModels.configure(User) do |user|
190
190
  user.external_user_id.default nil
191
191
  end
192
192
 
193
+ SampleModels.configure(User2) do |user2|
194
+ user2.email.force_email_format
195
+ end
196
+
193
197
  SampleModels.configure(Video) do |video|
194
198
  video.before_save do |v, sample_attrs|
195
199
  if v.episode && v.episode.show != v.show
data/test/setup/schema.rb CHANGED
@@ -88,6 +88,7 @@ silence_stream(STDOUT) do
88
88
 
89
89
  create_table 'user2s', :force => true do |user2|
90
90
  user2.string 'login'
91
+ user2.string 'email'
91
92
  end
92
93
 
93
94
  create_table 'user_with_passwords', :force => true do |user|
@@ -42,6 +42,13 @@ class ConfigurationTest < SampleModelsTestCase
42
42
  assert_nil bp.published_at
43
43
  end
44
44
 
45
+ def test_force_email_format
46
+ 10.times do
47
+ user2 = User2.sample
48
+ assert_match /^.*@.*\..*/, user2.email
49
+ end
50
+ end
51
+
45
52
  def test_before_save
46
53
  assert_difference('Video.count') do
47
54
  assert_difference('Episode.count') do
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + "/../test_helper"
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '/../test_helper'))
2
2
 
3
3
  class SampleTest < SampleModelsTestCase
4
4
  def test_fills_non_validated_non_configured_fields_with_a_non_blank_value
@@ -109,6 +109,7 @@ class SampleTest < SampleModelsTestCase
109
109
 
110
110
  def test_string_which_is_required_to_be_present_and_unique
111
111
  # Ensuring that it doesn't get tripped up by a pre-existing record
112
+ User2.destroy_all
112
113
  User2.create!(:login => 'login 1')
113
114
  User2.sample
114
115
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: sample_models
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.0.1
5
+ version: 2.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Francis Hwang