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 "activerecord", "~> 5.0.0"
gem 'sqlite3'
gem 'attr_encrypted', '~> 3.0', 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)
class User < ActiveRecord::Base
establish_connection adapter: 'sqlite3', database: 'test'
connection.create_table table_name, force: true do |t|
t.string :name
t.string :encrypted_email
t.string :encrypted_email_iv
t.timestamps null: true
end
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.