Skip to content

Commit

Permalink
Move error[s]_on to ActiveModel::Validations so it can be used by other
Browse files Browse the repository at this point in the history
ActiveModel compliant ORMs

- Closes ????
  • Loading branch information
dchelimsky committed Oct 27, 2010
1 parent ddd9485 commit ab6dbea
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 31 deletions.
58 changes: 27 additions & 31 deletions lib/rspec/rails/extensions/active_record/base.rb
Expand Up @@ -3,42 +3,38 @@ module Rails
if using_active_record?
module Extensions
module ActiveRecord
module ClassMethods
# :call-seq:
# ModelClass.should have(:no).records
# ModelClass.should have(1).record
# ModelClass.should have(n).records
#
# Extension to enhance <tt>should have</tt> on AR Model classes
def records
find(:all)
end
alias :record :records
end

module InstanceMethods
# :call-seq:
# model.should have(:no).errors_on(:attribute)
# model.should have(1).error_on(:attribute)
# model.should have(n).errors_on(:attribute)
#
# Extension to enhance <tt>should have</tt> on AR Model instances.
# Calls model.valid? in order to prepare the object's errors
# object.
def errors_on(attribute)
self.valid?
[self.errors[attribute]].flatten.compact
end
alias :error_on :errors_on
# :call-seq:
# ModelClass.should have(:no).records
# ModelClass.should have(1).record
# ModelClass.should have(n).records
#
# Extension to enhance <tt>should have</tt> on AR Model classes
def records
find(:all)
end
alias :record :records
end
end

class ::ActiveRecord::Base #:nodoc:
extend RSpec::Rails::Extensions::ActiveRecord::ClassMethods
include RSpec::Rails::Extensions::ActiveRecord::InstanceMethods
class ::ActiveRecord::Base #:nodoc:
extend RSpec::Rails::Extensions::ActiveRecord
end
end
end
end
end

module ::ActiveModel::Validations
# :call-seq:
# model.should have(:no).errors_on(:attribute)
# model.should have(1).error_on(:attribute)
# model.should have(n).errors_on(:attribute)
#
# Extension to enhance <tt>should have</tt> on AR Model instances.
# Calls model.valid? in order to prepare the object's errors
# object.
def errors_on(attribute)
self.valid?
[self.errors[attribute]].flatten.compact
end
alias :error_on :errors_on
end
23 changes: 23 additions & 0 deletions spec/rspec/rails/extensions/active_model/errors_on_spec.rb
@@ -0,0 +1,23 @@
require "spec_helper"

describe "errors_on" do
let(:klass) do
Class.new do
include ActiveModel::Validations
end
end

it "calls valid?" do
model = klass.new
model.should_receive(:valid?)
model.errors_on(:foo)
end

it "returns the errors on that attribute" do
model = klass.new
model.stub(:errors) do
{ :foo => ['a', 'b'] }
end
model.errors_on(:foo).should eq(['a','b'])
end
end
9 changes: 9 additions & 0 deletions spec/rspec/rails/extensions/active_record/records_spec.rb
@@ -0,0 +1,9 @@
require 'spec_helper'

describe "records" do
it "delegates to find(:all)" do
klass = Class.new(ActiveRecord::Base)
klass.should_receive(:find).with(:all)
klass.records
end
end

0 comments on commit ab6dbea

Please sign in to comment.