simple_record 2.1.2 → 2.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/simple_record/validations.rb +2 -2
- data/lib/simple_record.rb +52 -51
- data/test/my_model.rb +6 -1
- data/test/test_validations.rb +11 -1
- metadata +4 -4
data/lib/simple_record.rb
CHANGED
@@ -179,6 +179,7 @@ module SimpleRecord
|
|
179
179
|
|
180
180
|
|
181
181
|
if defined?(ActiveModel)
|
182
|
+
puts "Using ActiveModel validations."
|
182
183
|
@@active_model = true
|
183
184
|
extend ActiveModel::Naming
|
184
185
|
include ActiveModel::Conversion
|
@@ -188,11 +189,9 @@ module SimpleRecord
|
|
188
189
|
define_model_callbacks :save, :create, :update, :destroy
|
189
190
|
include SimpleRecord::Callbacks3
|
190
191
|
alias_method :am_valid?, :valid?
|
191
|
-
|
192
|
-
|
193
192
|
else
|
194
193
|
@@active_model = false
|
195
|
-
puts "using
|
194
|
+
puts "NOT using ActiveModel validations."
|
196
195
|
attr_accessor :errors
|
197
196
|
include SimpleRecord::Callbacks
|
198
197
|
end
|
@@ -454,69 +453,68 @@ module SimpleRecord
|
|
454
453
|
# puts '@dirtyA=' + @dirty.inspect
|
455
454
|
return true if @dirty.size == 0 # Nothing to save so skip it
|
456
455
|
end
|
457
|
-
is_create = self[:id].nil?
|
458
|
-
ok = pre_save(options) # Validates and sets ID
|
459
|
-
if ok
|
460
|
-
# puts 'ok'
|
461
|
-
begin
|
462
|
-
dirty = @dirty
|
463
|
-
# puts 'dirty before=' + @dirty.inspect
|
464
|
-
if options[:dirty]
|
465
|
-
# puts '@dirty=' + @dirty.inspect
|
466
|
-
return true if @dirty.size == 0 # This should probably never happen because after pre_save, created/updated dates are changed
|
467
|
-
options[:dirty_atts] = @dirty
|
468
|
-
end
|
469
|
-
to_delete = get_atts_to_delete
|
470
456
|
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
x = save_super(dirty, is_create, options, to_delete)
|
478
|
-
# puts 'save_super result = ' + x.to_s
|
479
|
-
return x
|
480
|
-
else
|
481
|
-
# puts 'not activemodel callbacks'
|
482
|
-
return save_super(dirty, is_create, options, to_delete)
|
483
|
-
end
|
484
|
-
rescue Aws::AwsError => ex
|
485
|
-
raise ex
|
457
|
+
ok = pre_save(options) # Validates and sets ID
|
458
|
+
if ok
|
459
|
+
if @@active_model
|
460
|
+
ok = create_or_update(options)
|
461
|
+
else
|
462
|
+
ok = do_actual_save(options)
|
486
463
|
end
|
487
|
-
else
|
488
|
-
# puts 'returning false'
|
489
|
-
return false
|
490
464
|
end
|
465
|
+
return ok
|
491
466
|
end
|
492
467
|
|
493
|
-
|
494
|
-
|
468
|
+
def do_actual_save(options)
|
469
|
+
begin
|
470
|
+
is_create = new_record? # self[:id].nil?
|
471
|
+
|
472
|
+
dirty = @dirty
|
473
|
+
# puts 'dirty before=' + @dirty.inspect
|
474
|
+
if options[:dirty]
|
475
|
+
# puts '@dirty=' + @dirty.inspect
|
476
|
+
return true if @dirty.size == 0 # This should probably never happen because after pre_save, created/updated dates are changed
|
477
|
+
options[:dirty_atts] = @dirty
|
478
|
+
end
|
479
|
+
to_delete = get_atts_to_delete
|
495
480
|
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
481
|
+
if self.class.is_sharded?
|
482
|
+
options[:domain] = sharded_domain
|
483
|
+
end
|
484
|
+
return save_super(dirty, is_create, options, to_delete)
|
485
|
+
rescue Aws::AwsError => ex
|
486
|
+
raise ex
|
501
487
|
end
|
488
|
+
|
502
489
|
end
|
503
490
|
|
504
|
-
|
491
|
+
# if @@active_model
|
492
|
+
# alias_method :old_save, :save
|
493
|
+
#
|
494
|
+
# def save(options={})
|
495
|
+
## puts 'extended save'
|
496
|
+
# x = create_or_update
|
497
|
+
## puts 'save x=' + x.to_s
|
498
|
+
# x
|
499
|
+
# end
|
500
|
+
# end
|
501
|
+
|
502
|
+
def create_or_update(options) #:nodoc:
|
505
503
|
# puts 'create_or_update'
|
506
504
|
ret = true
|
507
505
|
_run_save_callbacks do
|
508
|
-
result = new_record? ? create : update
|
506
|
+
result = new_record? ? create(options) : update(options)
|
509
507
|
# puts 'save_callbacks result=' + result.inspect
|
510
|
-
ret
|
508
|
+
ret = result
|
511
509
|
end
|
512
510
|
ret
|
513
511
|
end
|
514
512
|
|
515
|
-
def create #:nodoc:
|
513
|
+
def create(options) #:nodoc:
|
516
514
|
puts '3 create'
|
517
515
|
ret = true
|
518
516
|
_run_create_callbacks do
|
519
|
-
x
|
517
|
+
x = do_actual_save(options)
|
520
518
|
# puts 'create old_save result=' + x.to_s
|
521
519
|
ret = x
|
522
520
|
end
|
@@ -524,11 +522,11 @@ module SimpleRecord
|
|
524
522
|
end
|
525
523
|
|
526
524
|
#
|
527
|
-
def update(
|
525
|
+
def update(options) #:nodoc:
|
528
526
|
puts '3 update'
|
529
527
|
ret = true
|
530
528
|
_run_update_callbacks do
|
531
|
-
x
|
529
|
+
x = do_actual_save(options)
|
532
530
|
# puts 'update old_save result=' + x.to_s
|
533
531
|
ret = x
|
534
532
|
end
|
@@ -700,6 +698,7 @@ module SimpleRecord
|
|
700
698
|
|
701
699
|
def pre_save(options)
|
702
700
|
|
701
|
+
# puts '@@active_model ? ' + @@active_model.inspect
|
703
702
|
|
704
703
|
ok = true
|
705
704
|
is_create = self[:id].nil?
|
@@ -728,11 +727,13 @@ module SimpleRecord
|
|
728
727
|
|
729
728
|
# Now for callbacks
|
730
729
|
unless @@active_model
|
731
|
-
ok = respond_to?(
|
730
|
+
ok = respond_to?(:before_save) ? before_save : true
|
732
731
|
if ok
|
733
|
-
|
732
|
+
# puts 'ok'
|
733
|
+
if is_create && respond_to?(:before_create)
|
734
|
+
# puts 'responsds to before_create'
|
734
735
|
ok = before_create
|
735
|
-
elsif !is_create && respond_to?(
|
736
|
+
elsif !is_create && respond_to?(:before_update)
|
736
737
|
ok = before_update
|
737
738
|
end
|
738
739
|
end
|
data/test/my_model.rb
CHANGED
@@ -19,7 +19,7 @@ class MyModel < MyBaseModel
|
|
19
19
|
|
20
20
|
has_clobs :clob1, :clob2
|
21
21
|
|
22
|
-
attr_accessor :attr_before_save, :attr_after_save, :attr_before_create, :attr_after_create
|
22
|
+
attr_accessor :attr_before_save, :attr_after_save, :attr_before_create, :attr_after_create, :attr_after_update
|
23
23
|
|
24
24
|
#callbacks
|
25
25
|
before_create :set_nickname
|
@@ -28,6 +28,7 @@ class MyModel < MyBaseModel
|
|
28
28
|
before_save :before_save
|
29
29
|
|
30
30
|
after_save :after_save
|
31
|
+
after_update :after_update
|
31
32
|
|
32
33
|
def set_nickname
|
33
34
|
puts 'before_create set nickname'
|
@@ -50,6 +51,10 @@ class MyModel < MyBaseModel
|
|
50
51
|
@attr_after_save = true
|
51
52
|
bump_save_count
|
52
53
|
end
|
54
|
+
def after_update
|
55
|
+
puts "after_update"
|
56
|
+
@attr_after_update = true
|
57
|
+
end
|
53
58
|
|
54
59
|
def bump_save_count
|
55
60
|
puts 'after_save bump save_count=' + save_count.to_s
|
data/test/test_validations.rb
CHANGED
@@ -21,7 +21,7 @@ class TestSimpleRecord < TestBase
|
|
21
21
|
assert !mm.attr_before_create
|
22
22
|
assert !mm.valid?
|
23
23
|
assert mm.save == false, mm.errors.inspect
|
24
|
-
assert mm.attr_before_create
|
24
|
+
assert !mm.attr_before_create # should not get called if not valid
|
25
25
|
assert !mm.attr_after_save
|
26
26
|
assert !mm.attr_after_create
|
27
27
|
mm.name = "abcd"
|
@@ -36,10 +36,20 @@ class TestSimpleRecord < TestBase
|
|
36
36
|
assert mm.save, mm.errors.inspect
|
37
37
|
|
38
38
|
p mm
|
39
|
+
assert mm.attr_before_create
|
39
40
|
assert mm.attr_after_save
|
40
41
|
assert mm.attr_after_create
|
42
|
+
assert !mm.attr_after_update
|
41
43
|
|
42
44
|
assert mm.valid?, mm.errors.inspect
|
43
45
|
assert mm.save_count == 1
|
46
|
+
|
47
|
+
mm.name = "abc123"
|
48
|
+
assert mm.save
|
49
|
+
|
50
|
+
assert mm.attr_after_update
|
51
|
+
|
52
|
+
|
53
|
+
|
44
54
|
end
|
45
55
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,12 +11,12 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2011-02-
|
14
|
+
date: 2011-02-11 00:00:00.000000000 -08:00
|
15
15
|
default_executable:
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: aws
|
19
|
-
requirement: &
|
19
|
+
requirement: &26752296 !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
22
|
- - ! '>='
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
version: '0'
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
|
-
version_requirements: *
|
27
|
+
version_requirements: *26752296
|
28
28
|
description: ActiveRecord like interface for Amazon SimpleDB. Store, query, shard,
|
29
29
|
etc. By http://www.appoxy.com
|
30
30
|
email: travis@appoxy.com
|