def stub_model(model_class, stubs={})
      model_class.new.tap do |m|
        m.extend ActiveModelStubExtensions
        if defined?(ActiveRecord) && model_class < ActiveRecord::Base && model_class.primary_key
          m.extend ActiveRecordStubExtensions
          primary_key = model_class.primary_key.to_sym
          stubs = {primary_key => next_id}.merge(stubs)
          stubs = {:persisted? => !!stubs[primary_key]}.merge(stubs)
        else
          stubs = {:id => next_id}.merge(stubs)
          stubs = {:persisted? => !!stubs[:id]}.merge(stubs)
        end
        stubs = {:blank? => false}.merge(stubs)

        stubs.each do |message, return_value|
          if m.respond_to?("#{message}=")
            m.__send__("#{message}=", return_value)
          else
            RSpec::Mocks.allow_message(m, message).and_return(return_value)
          end
        end

        yield m if block_given?
      end
    end
def stub_model(model_class, stubs={})
      model_class.new.tap do |m|
        m.extend ActiveModelStubExtensions
        if defined?(ActiveRecord) && model_class < ActiveRecord::Base && model_class.primary_key
          m.extend ActiveRecordStubExtensions
          primary_key = model_class.primary_key.to_sym
          stubs = {primary_key => next_id}.merge(stubs)
          stubs = {:persisted? => !!stubs[primary_key]}.merge(stubs)
        else
          stubs = {:id => next_id}.merge(stubs)
          stubs = {:persisted? => !!stubs[:id]}.merge(stubs)
        end
        stubs = {:blank? => false}.merge(stubs)

        stubs.each do |message, return_value|
          if m.respond_to?("#{message}=")
            m.__send__("#{message}=", return_value)
          else
            RSpec::Mocks.allow_message(m, message).and_return(return_value)
          end
        end

        yield m if block_given?
      end
    end
Creates an instance of `Model` with `to_param` stubbed using a
 generated value that is unique to each object. If `Model` is an
 `ActiveRecord` model, it is prohibited from accessing the database.

 For each key in `stubs`, if the model has a matching attribute
 (determined by `respond_to?`) it is simply assigned the submitted values.
 If the model does not have a matching attribute, the key/value pair is
 assigned as a stub return value using RSpec's mocking/stubbing
 framework.

 <tt>persisted?</tt> is overridden to return the result of !id.nil?
 This means that by default persisted? will return true. If  you want
 the object to behave as a new record, sending it `as_new_record` will
 set the id to nil. You can also explicitly set :id => nil, in which
 case persisted? will return false, but using `as_new_record` makes the
 example a bit more descriptive.

 While you can use stub_model in any example (model, view, controller,
 helper), it is especially useful in view examples, which are
 inherently more state-based than interaction-based.

 @example

     stub_model(Person)
     stub_model(Person).as_new_record
     stub_model(Person, :to_param => 37)
     stub_model(Person) {|person| person.first_name = "David"}