Shoulda macro for the Userstamp plugin
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
-
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? . . . -
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? . . . -
Thanks for the comment Adam, I applied your change to it, thanks for the catch.
-
*-)








