public
Description: ActiveRecord macro that helps encrypt passwords and generate api tokens before_save.
Homepage:
Clone URL: git://github.com/matthewtodd/has_digest.git
has_digest / shoulda_macros / has_digest.rb
812e36da » matthewtodd 2008-11-07 Quick shoulda macro. 1 module HasDigest
2 module Shoulda
ef24ee9f » matthewtodd 2008-11-08 Doco tweak. 3 # Asserts that <tt>has_digest :name</tt> has been called with the given
457e783d » matthewtodd 2008-11-09 should_have_digest can take... 4 # options, and that the necessary database columns are present. +options+
5 # may contain two keys:
6 # * +depends+: either a single attribute name or an array of attribtues
7 # names. (Specifying <tt>:salt</tt> here is unnecessary.)
8 # * +limit+: if your db column for the given digest doesn't have
9 # <tt>:limit => 40</tt>, you may specify its size here.
695012b3 » matthewtodd 2008-11-07 Magic synthetic attributes. 10 def should_have_digest(name, options = {})
457e783d » matthewtodd 2008-11-09 should_have_digest can take... 11 options.assert_valid_keys(:depends, :limit)
f23993ec » matthewtodd 2008-11-07 Working on shoulda macro. 12
700d10bd » matthewtodd 2009-09-02 Fixed should_have_digest to... 13 context "#{described_type.name} with has_digest :#{name}" do
457e783d » matthewtodd 2008-11-09 should_have_digest can take... 14 should_have_db_column name, :type => :string, :limit => (options[:limit] || 40)
5c5d75e0 » matthewtodd 2008-11-08 Just register the digests i... 15
16 should "generate digest for :#{name}" do
700d10bd » matthewtodd 2009-09-02 Fixed should_have_digest to... 17 assert_not_nil self.class.described_type.has_digest_attributes[name]
5c5d75e0 » matthewtodd 2008-11-08 Just register the digests i... 18 end
695012b3 » matthewtodd 2008-11-07 Magic synthetic attributes. 19
f23993ec » matthewtodd 2008-11-07 Working on shoulda macro. 20 if options[:depends]
21 dependencies = options[:depends]
22 dependencies = [dependencies] unless dependencies.respond_to?(:each)
700d10bd » matthewtodd 2009-09-02 Fixed should_have_digest to... 23 dependencies.unshift(:salt) if described_type.column_names.include?('salt')
695012b3 » matthewtodd 2008-11-07 Magic synthetic attributes. 24
5c5d75e0 » matthewtodd 2008-11-08 Just register the digests i... 25 should "generate digest for :#{name} from #{dependencies.to_sentence}" do
700d10bd » matthewtodd 2009-09-02 Fixed should_have_digest to... 26 attributes = self.class.described_type.has_digest_attributes[name] || {}
5c5d75e0 » matthewtodd 2008-11-08 Just register the digests i... 27 assert_equal dependencies, attributes[:dependencies]
f23993ec » matthewtodd 2008-11-07 Working on shoulda macro. 28 end
695012b3 » matthewtodd 2008-11-07 Magic synthetic attributes. 29 end
812e36da » matthewtodd 2008-11-07 Quick shoulda macro. 30 end
31 end
32 end
33 end
34
35 Test::Unit::TestCase.extend(HasDigest::Shoulda)