Permalink
Browse files

Errors suport and added upsert method for scaffolds

  • Loading branch information...
1 parent d87e8f5 commit 75c3dd2ff7d12e33a1d2677faf7e492936225db0 @jrwest committed Nov 16, 2010
Showing with 27 additions and 0 deletions.
  1. +27 −0 lib/rails/mongomatic/support.rb
@@ -3,6 +3,7 @@ module Mongomatic
module Support
def self.included(base)
base.extend ClassMethods
+ ::Mongomatic::Errors.send(:include, Errors)
end
def merge!(hash)
@@ -11,11 +12,37 @@ def merge!(hash)
update
end
+ def upsert
+ if new?
+ insert
+ else
+ update
+ end
+ end
+
+ def upsert!
+ if new?
+ insert!
+ else
+ update!
+ end
+ end
+
module ClassMethods
def model_name
@_model_name ||= ActiveModel::Name.new(self)
end
end
+
+ module Errors
+ def any?
+ !empty?
+ end
+
+ def count
+ @errors.values.inject(0) { |sum, errors| sum += errors.size }
+ end
+ end
end
end
end

0 comments on commit 75c3dd2

Please sign in to comment.