Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/securely_hashed_attributes.rb
Overview
Adds the singleton method securely_hashes to ActiveRecord::Base
Class Method Summary collapse
-
.securely_hashes(attrib, opts = {}) ⇒ Object
Adds a secure hash serialization to a column with the help of
bcrypt
.
Class Method Details
.securely_hashes(attrib, opts = {}) ⇒ Object
Adds a secure hash serialization to a column with the help of bcrypt
. By default, attrib
will serve as both the attribute to serialize and the name of the column that will hold the serialized value. If you want to serialize the value to a different column, use the :to
option. Hashing of the value assigned to attrib
will not occur until the model is persisted.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/securely_hashed_attributes.rb', line 55 def securely_hashes attrib, opts={} opts = SecurelyHashedAttributes::DEFAULT_ATTRIBUTE_OPTIONS.merge opts attrib = attrib.to_sym if opts[:to] col = opts[:to].to_sym serialize col, opts[:with] attr_reader attrib define_method :"#{attrib}=" do |val| instance_variable_set(:"@#{attrib}", val) self[col] = val end else serialize attrib, opts[:with] end end |