Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Don't assume ActiveModel is available

Like ActiveRecord, it is optional and its railtie may not
have been required.
  • Loading branch information...
commit d41bd9b3ebbcaffb7f469224f5866321b2ecf4c7 1 parent fe4e8f3
John Firebaugh jfirebaugh authored
Showing with 22 additions and 19 deletions.
  1. +22 −19 lib/rspec/rails/extensions/active_record/base.rb
41 lib/rspec/rails/extensions/active_record/base.rb
View
@@ -24,24 +24,27 @@ class ::ActiveRecord::Base
end
end
-module ::ActiveModel::Validations
- # Extension to enhance `should have` on AR Model instances. Calls
- # model.valid? in order to prepare the object's errors object. Accepts
- # a :context option to specify the validation context.
- #
- # You can also use this to specify the content of the error messages.
- #
- # @example
- #
- # model.should have(:no).errors_on(:attribute)
- # model.should have(1).error_on(:attribute)
- # model.should have(n).errors_on(:attribute)
- # model.should have(n).errors_on(:attribute, :context => :create)
- #
- # model.errors_on(:attribute).should include("can't be blank")
- def errors_on(attribute, options = {})
- self.valid?(options[:context])
- [self.errors[attribute]].flatten.compact
+if defined?(::ActiveModel)
+ module ::ActiveModel::Validations
+ # Extension to enhance `should have` on AR Model instances. Calls
+ # model.valid? in order to prepare the object's errors object. Accepts
+ # a :context option to specify the validation context.
+ #
+ # You can also use this to specify the content of the error messages.
+ #
+ # @example
+ #
+ # model.should have(:no).errors_on(:attribute)
+ # model.should have(1).error_on(:attribute)
+ # model.should have(n).errors_on(:attribute)
+ # model.should have(n).errors_on(:attribute, :context => :create)
+ #
+ # model.errors_on(:attribute).should include("can't be blank")
+ def errors_on(attribute, options = {})
+ self.valid?(options[:context])
+ [self.errors[attribute]].flatten.compact
+ end
+
+ alias :error_on :errors_on
end
- alias :error_on :errors_on
end
Please sign in to comment.
Something went wrong with that request. Please try again.