Shoulda macro for the Userstamp plugin

written by Rahvin on December 28th, 2008 @ 02:03 AM

The following is a shoulda macro that I’ve written for the userstamp plugin. I recommend creating a file should_be_stampable.rb in the test/shoulda_macros folder. It will be loaded automagically.

The test is acts_as_paranoid aware so it also checks for the deleter part if it is available.

You can call

should_be_stampable

for models that have

class Post < ActiveRecord::Base
  stampable
end

and there is

should_stamp_models

for your User model that contains

class User < ActiveRecord::Base
  model_stamper
end

should_be_stampable.rb:

class Test::Unit::TestCase
  # Use this to test models that are equiped with 'stampable'
  def self.should_be_stampable
    klass = self.name.gsub(/Test$/, '').constantize
    include_deleted_by = klass.respond_to?(:paranoid?) && klass.paranoid?

    context "#{klass}" do
      should_have_db_column :creator_id, :type => "integer"
      should_have_db_column :updater_id, :type => "integer"
      should_have_db_column :deleter_id, :type => "integer" if include_deleted_by

      should_belong_to :creator
      should_belong_to :updater
      should_belong_to :deleter if include_deleted_by

      should_have_class_methods :without_stamps, :stamper_class, :stampable

      should_have_instance_methods :creator, :creator=, :updater, :updater=
      should_have_instance_methods :deleter, :deleter= if include_deleted_by
    end 
  end

  # Use this to test the user model that has 'model_stamper'
  def self.should_stamp_models
    klass = self.name.gsub(/Test$/, '').constantize

    context "#{klass}" do
      should_have_class_methods :stamper, :stamper=
    end
  end
end

The tests are quite simple, so if you have anything to add to it, please let me know.

Comments

  • Adam Maddox on 31 Dec 22:15

    Thanks for the Macro Rahvin

    I added a respond_to? check for paranoid?, as I’m not using that gem.

    class Test::Unit::TestCase
      # Use this to test models that are equiped with 'stampable'
      def self.should_be_stampable
        klass = self.name.gsub(/Test$/, '').constantize
        include_deleted_by = klass.respond_to?(:paranoid?) && klass.paranoid?
    .
    .
    .
    
  • Adam on 31 Dec 22:19

    Thanks for posting this Macro Rahvin

    I recommend adding a respond_to? check for those not using the acts_as_paranoid gem.

    class Test::Unit::TestCase
      # Use this to test models that are equiped with 'stampable'
      def self.should_be_stampable
        klass = self.name.gsub(/Test$/, '').constantize
        include_deleted_by = klass.respond_to?(:paranoid?) && klass.paranoid?
    .
    .
    .
    
  • Rahvin on 01 Jan 15:45

    Thanks for the comment Adam, I applied your change to it, thanks for the catch.

  • Manon on 08 Feb 23:02

    *-)

Post a comment

Options:

Size

Colors