Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 46 lines (43 sloc) 1.23 kb
bbcebcd @dchelimsky Use Rails config to determine whether or not we're using AR
dchelimsky authored
1 module RSpec
2 module Rails
a5949a5 @dchelimsky just ask if AR is defined instead
dchelimsky authored
3 if defined?(ActiveRecord)
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
4 module Extensions
5 module ActiveRecord
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
6 # Extension to enhance `should have` on AR Model classes
6ab0bb6 @dchelimsky cleanup and add some rdoc
dchelimsky authored
7 #
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
8 # @example
e26fb34 @dchelimsky small rdoc tweak
dchelimsky authored
9 #
78c7548 @dchelimsky rdoc: correct indentation helps (markdown)
dchelimsky authored
10 # ModelClass.should have(:no).records
11 # ModelClass.should have(1).record
12 # ModelClass.should have(n).records
73991af @dchelimsky Move error[s]_on to ActiveModel::Validations so it can be used by other
dchelimsky authored
13 def records
14 find(:all)
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
15 end
73991af @dchelimsky Move error[s]_on to ActiveModel::Validations so it can be used by other
dchelimsky authored
16 alias :record :records
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
17 end
96a29c4 @dchelimsky cleanup whitespace
dchelimsky authored
18
6ab0bb6 @dchelimsky cleanup and add some rdoc
dchelimsky authored
19 class ::ActiveRecord::Base
73991af @dchelimsky Move error[s]_on to ActiveModel::Validations so it can be used by other
dchelimsky authored
20 extend RSpec::Rails::Extensions::ActiveRecord
21 end
bbcebcd @dchelimsky Use Rails config to determine whether or not we're using AR
dchelimsky authored
22 end
23 end
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
24 end
25 end
26
73991af @dchelimsky Move error[s]_on to ActiveModel::Validations so it can be used by other
dchelimsky authored
27 module ::ActiveModel::Validations
e26fb34 @dchelimsky small rdoc tweak
dchelimsky authored
28 # Extension to enhance `should have` on AR Model instances. Calls
29 # model.valid? in order to prepare the object's errors object.
30 #
31 # You can also use this to specify the content of the error messages.
6ab0bb6 @dchelimsky cleanup and add some rdoc
dchelimsky authored
32 #
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
33 # @example
6ab0bb6 @dchelimsky cleanup and add some rdoc
dchelimsky authored
34 #
78c7548 @dchelimsky rdoc: correct indentation helps (markdown)
dchelimsky authored
35 # model.should have(:no).errors_on(:attribute)
36 # model.should have(1).error_on(:attribute)
37 # model.should have(n).errors_on(:attribute)
73991af @dchelimsky Move error[s]_on to ActiveModel::Validations so it can be used by other
dchelimsky authored
38 #
78c7548 @dchelimsky rdoc: correct indentation helps (markdown)
dchelimsky authored
39 # model.errors_on(:attribute).should include("can't be blank")
73991af @dchelimsky Move error[s]_on to ActiveModel::Validations so it can be used by other
dchelimsky authored
40 def errors_on(attribute)
41 self.valid?
42 [self.errors[attribute]].flatten.compact
43 end
44 alias :error_on :errors_on
45 end
Something went wrong with that request. Please try again.