Skip to content

Instantly share code, notes, and snippets.

@arthurnn
Created May 25, 2017 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurnn/79c1f7c61bb729ae5d78c4e7e1ec5b86 to your computer and use it in GitHub Desktop.
Save arthurnn/79c1f7c61bb729ae5d78c4e7e1ec5b86 to your computer and use it in GitHub Desktop.
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