Skip to content

Commit

Permalink
Don't assume ActiveModel is available
Browse files Browse the repository at this point in the history
Like ActiveRecord, it is optional and its railtie may not
have been required.
  • Loading branch information
jfirebaugh committed Nov 28, 2012
1 parent fe4e8f3 commit d41bd9b
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions lib/rspec/rails/extensions/active_record/base.rb
Expand Up @@ -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

0 comments on commit d41bd9b

Please sign in to comment.