Module: HasDigest::ClassMethods
- Defined in:
- lib/has_digest.rb
Instance Method Summary collapse
-
#dependent_has_digest_attributes ⇒ Object
:nodoc:.
-
#has_digest(attribute, options = {}) ⇒ Object
Gives the class it is called on a
before_save
callback that writes a 40-character hexadecimal string into the givenattribute
. -
#has_digest_attributes ⇒ Object
:nodoc:.
-
#standalone_has_digest_attributes ⇒ Object
:nodoc:.
Instance Method Details
#dependent_has_digest_attributes ⇒ Object
:nodoc:
122 123 124 |
# File 'lib/has_digest.rb', line 122 def dependent_has_digest_attributes # :nodoc: has_digest_attributes.reject { |name, | !.has_key?(:dependencies) } end |
#has_digest(attribute, options = {}) ⇒ Object
Gives the class it is called on a before_save
callback that writes a 40-character hexadecimal string into the given attribute
. The generated string may depend on other (possibly synthetic) attributes of the model, being automatically regenerated when they change. One key is supported in the options
hash:
-
depends
: either a single attribute name or a list of attribute names. If any of these values change,attribute
will be re-written. Setting any (non-synthetic) one of these attributes tonil
will effectively also setattribute
tonil
.
Magic Salting
If the model in question has a salt
attribute, its salt
be automatically populated on create and automatically mixed into any digests with dependencies on other attributes, saving you a little bit of work when dealing with passwords.
Magic Synthetic Attributes
If the model in question doesn’t have a database column for one of your digest dependencies, an attr_accessor
for that synthetic dependency will be created automatically. For example, if you write has_digest :encrypted_password, :depends => :password
and don’t have a password
column for your model, the attr_accessor
for password
will be automatically created, saving you a redundant line of code.
Examples
# token will be generated on create
class Order < ActiveRecord::Base
has_digest :token
end
# encrypted_password will be generated on save whenever @password is not nil
# (Automatically calls attr_accessor :password.)
class User < ActiveRecord::Base
has_digest :encrypted_password, :depends => :password
end
# remember_me_token will be generated on save whenever login or remember_me_until have changed.
# User.update_attributes(:remember_me_until => nil) will set remember_me_token to nil.
class User < ActiveRecord::Base
has_digest :remember_me_token, :depends => [:login, :remember_me_until]
end
# api_token will be blank until user.update_attributes(:generate_api_token => true).
# (Automatically calls attr_accessor :generate_api_token.)
class User < ActiveRecord::Base
has_digest :api_token, :depends => :generate_api_token
end
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/has_digest.rb', line 100 def has_digest(attribute, = {}) .assert_valid_keys(:depends) if [:depends] dependencies = [] dependencies << :salt if column_names.include?('salt') dependencies << [:depends] dependencies.flatten! synthetic_dependencies = dependencies - column_names.map(&:to_sym) synthetic_dependencies.each { |name| attr_accessor name } write_inheritable_hash :has_digest_attributes, attribute => { :dependencies => dependencies, :synthetic_dependencies => synthetic_dependencies } else write_inheritable_hash :has_digest_attributes, attribute => {} end end |
#has_digest_attributes ⇒ Object
:nodoc:
118 119 120 |
# File 'lib/has_digest.rb', line 118 def has_digest_attributes # :nodoc: read_inheritable_attribute(:has_digest_attributes) || write_inheritable_attribute(:has_digest_attributes, {}) end |
#standalone_has_digest_attributes ⇒ Object
:nodoc:
126 127 128 |
# File 'lib/has_digest.rb', line 126 def standalone_has_digest_attributes # :nodoc: has_digest_attributes.reject { |name, | .has_key?(:dependencies) } end |