Permalink
Browse files

Fixed non activemodel validation issues.

  • Loading branch information...
1 parent 05decc5 commit 41222794c50dcb8c5c5db35ee484f1aee58cec43 @treeder treeder committed Feb 12, 2011
Showing with 74 additions and 58 deletions.
  1. +1 −1 VERSION.yml
  2. +52 −51 lib/simple_record.rb
  3. +2 −2 lib/simple_record/validations.rb
  4. +2 −2 simple_record.gemspec
  5. +6 −1 test/my_model.rb
  6. +11 −1 test/test_validations.rb
View
@@ -1,5 +1,5 @@
---
:major: 2
:minor: 1
-:patch: 2
+:patch: 3
:build:
View
@@ -179,6 +179,7 @@ class Base < SimpleRecord::ActiveSdb::Base
if defined?(ActiveModel)
+ puts "Using ActiveModel validations."
@@active_model = true
extend ActiveModel::Naming
include ActiveModel::Conversion
@@ -188,11 +189,9 @@ class Base < SimpleRecord::ActiveSdb::Base
define_model_callbacks :save, :create, :update, :destroy
include SimpleRecord::Callbacks3
alias_method :am_valid?, :valid?
-
-
else
@@active_model = false
- puts "using rails2 validations."
+ puts "NOT using ActiveModel validations."
attr_accessor :errors
include SimpleRecord::Callbacks
end
@@ -454,81 +453,80 @@ def save(options={})
# puts '@dirtyA=' + @dirty.inspect
return true if @dirty.size == 0 # Nothing to save so skip it
end
- is_create = self[:id].nil?
- ok = pre_save(options) # Validates and sets ID
- if ok
-# puts 'ok'
- begin
- dirty = @dirty
-# puts 'dirty before=' + @dirty.inspect
- if options[:dirty]
-# puts '@dirty=' + @dirty.inspect
- return true if @dirty.size == 0 # This should probably never happen because after pre_save, created/updated dates are changed
- options[:dirty_atts] = @dirty
- end
- to_delete = get_atts_to_delete
-
- if self.class.is_sharded?
- options[:domain] = sharded_domain
- end
-
- if @@active_model
- x = save_super(dirty, is_create, options, to_delete)
-# puts 'save_super result = ' + x.to_s
- return x
- else
-# puts 'not activemodel callbacks'
- return save_super(dirty, is_create, options, to_delete)
- end
- rescue Aws::AwsError => ex
- raise ex
+ ok = pre_save(options) # Validates and sets ID
+ if ok
+ if @@active_model
+ ok = create_or_update(options)
+ else
+ ok = do_actual_save(options)
end
- else
-# puts 'returning false'
- return false
end
+ return ok
end
- if @@active_model
- alias_method :old_save, :save
+ def do_actual_save(options)
+ begin
+ is_create = new_record? # self[:id].nil?
+
+ dirty = @dirty
+# puts 'dirty before=' + @dirty.inspect
+ if options[:dirty]
+# puts '@dirty=' + @dirty.inspect
+ return true if @dirty.size == 0 # This should probably never happen because after pre_save, created/updated dates are changed
+ options[:dirty_atts] = @dirty
+ end
+ to_delete = get_atts_to_delete
- def save(options={})
-# puts 'extended save'
- x = create_or_update
-# puts 'save x=' + x.to_s
- x
+ if self.class.is_sharded?
+ options[:domain] = sharded_domain
+ end
+ return save_super(dirty, is_create, options, to_delete)
+ rescue Aws::AwsError => ex
+ raise ex
end
+
end
- def create_or_update #:nodoc:
+# if @@active_model
+# alias_method :old_save, :save
+#
+# def save(options={})
+## puts 'extended save'
+# x = create_or_update
+## puts 'save x=' + x.to_s
+# x
+# end
+# end
+
+ def create_or_update(options) #:nodoc:
# puts 'create_or_update'
ret = true
_run_save_callbacks do
- result = new_record? ? create : update
+ result = new_record? ? create(options) : update(options)
# puts 'save_callbacks result=' + result.inspect
- ret = result
+ ret = result
end
ret
end
- def create #:nodoc:
+ def create(options) #:nodoc:
puts '3 create'
ret = true
_run_create_callbacks do
- x = old_save
+ x = do_actual_save(options)
# puts 'create old_save result=' + x.to_s
ret = x
end
ret
end
#
- def update(*) #:nodoc:
+ def update(options) #:nodoc:
puts '3 update'
ret = true
_run_update_callbacks do
- x = old_save
+ x = do_actual_save(options)
# puts 'update old_save result=' + x.to_s
ret = x
end
@@ -700,6 +698,7 @@ def self.get_encryption_key()
def pre_save(options)
+# puts '@@active_model ? ' + @@active_model.inspect
ok = true
is_create = self[:id].nil?
@@ -728,11 +727,13 @@ def pre_save(options)
# Now for callbacks
unless @@active_model
- ok = respond_to?('before_save') ? before_save : true
+ ok = respond_to?(:before_save) ? before_save : true
if ok
- if is_create && respond_to?('before_create')
+# puts 'ok'
+ if is_create && respond_to?(:before_create)
+# puts 'responsds to before_create'
ok = before_create
- elsif !is_create && respond_to?('before_update')
+ elsif !is_create && respond_to?(:before_update)
ok = before_update
end
end
@@ -18,10 +18,10 @@ def self.included(base)
end
def valid?
- puts 'in rails2 valid?'
+# puts 'in rails2 valid?'
errors.clear
- if defined?(:am_valid?)
+ if respond_to?(:am_valid?)
# And now ActiveModel validations too
am_valid?
end
View
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{simple_record}
- s.version = "2.1.2"
+ s.version = "2.1.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Travis Reeder", "Chad Arimura", "RightScale"]
- s.date = %q{2011-02-09}
+ s.date = %q{2011-02-11}
s.description = %q{ActiveRecord like interface for Amazon SimpleDB. Store, query, shard, etc. By http://www.appoxy.com}
s.email = %q{travis@appoxy.com}
s.extra_rdoc_files = [
View
@@ -19,7 +19,7 @@ class MyModel < MyBaseModel
has_clobs :clob1, :clob2
- attr_accessor :attr_before_save, :attr_after_save, :attr_before_create, :attr_after_create
+ attr_accessor :attr_before_save, :attr_after_save, :attr_before_create, :attr_after_create, :attr_after_update
#callbacks
before_create :set_nickname
@@ -28,6 +28,7 @@ class MyModel < MyBaseModel
before_save :before_save
after_save :after_save
+ after_update :after_update
def set_nickname
puts 'before_create set nickname'
@@ -50,6 +51,10 @@ def after_save
@attr_after_save = true
bump_save_count
end
+ def after_update
+ puts "after_update"
+ @attr_after_update = true
+ end
def bump_save_count
puts 'after_save bump save_count=' + save_count.to_s
View
@@ -21,7 +21,7 @@ def test_validations
assert !mm.attr_before_create
assert !mm.valid?
assert mm.save == false, mm.errors.inspect
- assert mm.attr_before_create
+ assert !mm.attr_before_create # should not get called if not valid
assert !mm.attr_after_save
assert !mm.attr_after_create
mm.name = "abcd"
@@ -36,10 +36,20 @@ def test_validations
assert mm.save, mm.errors.inspect
p mm
+ assert mm.attr_before_create
assert mm.attr_after_save
assert mm.attr_after_create
+ assert !mm.attr_after_update
assert mm.valid?, mm.errors.inspect
assert mm.save_count == 1
+
+ mm.name = "abc123"
+ assert mm.save
+
+ assert mm.attr_after_update
+
+
+
end
end

0 comments on commit 4122279

Please sign in to comment.