Sha256: 05c4d6c69b7169fab14b45103794c00eaf4ee6363339ae355d8c8e5aeb06ff7a

Contents?: true

Size: 938 Bytes

Versions: 7

Compression:

Stored size: 938 Bytes

Contents

module Vidibus
  module Secure
    module Mongoid
      extend ActiveSupport::Concern
      module ClassMethods

        # Sets encrypted attributes.
        def attr_encrypted(*args)
          key = ENV["VIDIBUS_SECURE_KEY"]
          options = args.extract_options!
          for field in args

            # Define Mongoid field
            encrypted_field = "#{field}_encrypted"
            self.send(:field, encrypted_field, :type => Binary)

            # Define setter
            class_eval <<-EOV
              def #{field}=(value)
                self.#{encrypted_field} = value ? Vidibus::Secure.encrypt(value, "#{key}") : nil
              end
            EOV

            # Define getter
            class_eval <<-EOV
              def #{field}
                Vidibus::Secure.decrypt(#{encrypted_field}, "#{key}") if #{encrypted_field}
              end
            EOV
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vidibus-secure-0.1.2 lib/vidibus/secure/mongoid.rb
vidibus-secure-0.1.1 lib/vidibus/secure/mongoid.rb
vidibus-secure-0.1.0 lib/vidibus/secure/mongoid.rb
vidibus-secure-0.0.4 lib/vidibus/secure/mongoid.rb
vidibus-secure-0.0.3 lib/vidibus/secure/mongoid.rb
vidibus-secure-0.0.2 lib/vidibus/secure/mongoid.rb
vidibus-secure-0.0.1 lib/vidibus/secure/mongoid.rb