Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem 'bcrypt'
gem "activerecord", "~> 5.0.0"
gem 'sqlite3'
gem 'attr_encrypted', github: 'attr-encrypted/attr_encrypted', require: false
end
require 'active_record'
require 'attr_encrypted'
require 'minitest/autorun'
logger = Logger.new STDOUT
logger.level = 0
ActiveRecord::Base.logger = logger
KEY = SecureRandom.random_bytes(32)
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define(version: 1) do
self.verbose = false
create_table :users do |t|
t.string :name
t.string :encrypted_email
t.string :encrypted_email_iv
t.timestamps null: true
end
end
class User < ActiveRecord::Base
attr_encrypted :email, key: KEY
end
p ActiveRecord.version
class FooTest < Minitest::Test
def test_email
foo = User.new(name: 'Arthur')
foo.email = 'foo@test.com'
foo.save!
assert_equal 1, User.count
refute_nil foo.encrypted_email
refute_nil User.last.encrypted_email
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.