public
Rubygem
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
100644 36 lines (30 sloc) 1.445 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module HasDigest
  module Shoulda
    # Asserts that <tt>has_digest :name</tt> has been called with the given
    # options, and that the necessary database columns are present. +options+
    # may contain two keys:
    # * +depends+: either a single attribute name or an array of attribtues
    # names. (Specifying <tt>:salt</tt> here is unnecessary.)
    # * +limit+: if your db column for the given digest doesn't have
    # <tt>:limit => 40</tt>, you may specify its size here.
    def should_have_digest(name, options = {})
      options.assert_valid_keys(:depends, :limit)
 
      context "#{model_class.name} with has_digest :#{name}" do
        should_have_db_column name, :type => :string, :limit => (options[:limit] || 40)
 
        should "generate digest for :#{name}" do
          assert_not_nil self.class.model_class.has_digest_attributes[name]
        end
 
        if options[:depends]
          dependencies = options[:depends]
          dependencies = [dependencies] unless dependencies.respond_to?(:each)
          dependencies.unshift(:salt) if model_class.column_names.include?('salt')
 
          should "generate digest for :#{name} from #{dependencies.to_sentence}" do
            attributes = self.class.model_class.has_digest_attributes[name] || {}
            assert_equal dependencies, attributes[:dependencies]
          end
        end
      end
    end
  end
end
 
Test::Unit::TestCase.extend(HasDigest::Shoulda)