-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7580344
commit 4d2ac86
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Transcryptor::ActiveRecord::ZeroDowntime | ||
def self.included(base) | ||
base.extend ClassMethods | ||
|
||
base.class_eval do | ||
after_initialize :transcryptor_migrate | ||
|
||
@transcryptor_migrate_attributes = [] | ||
class << self | ||
attr_reader :transcryptor_migrate_attributes | ||
end | ||
end | ||
end | ||
|
||
def transcryptor_migrate | ||
self.class.transcryptor_migrate_attributes.each do |attribute| | ||
send("#{attribute[:new]}=", send(attribute[:old])) | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def transcryptor_migrate(old_attribute, new_attribute) | ||
@transcryptor_migrate_attributes << {old: old_attribute, new: new_attribute} | ||
|
||
options = attr_encrypted_options.merge(encrypted_attributes[old_attribute]) | ||
encrypted_attribute_name = "#{options[:prefix]}#{old_attribute}#{options[:suffix]}" | ||
|
||
define_method("#{old_attribute}=") do |value| | ||
send("#{encrypted_attribute_name}=", encrypt(old_attribute, value)) | ||
instance_variable_set("@#{old_attribute}", value) | ||
send("#{new_attribute}=", value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'securerandom' | ||
|
||
ActiveRecord::Base.connection.create_table(:active_record_zero_downtime_specs) do |t| | ||
t.string :encrypted_column_1 | ||
t.string :encrypted_column_1_iv | ||
t.string :encrypted_new_column_1 | ||
t.string :encrypted_new_column_1_iv | ||
end | ||
|
||
class ActiveRecordZeroDowntimeSpec < ActiveRecord::Base | ||
include Transcryptor::ActiveRecord::ZeroDowntime | ||
|
||
attr_encrypted :column_1, key: '1qwe1qwe1qwe1qwe1qwe1qwe1qwe1qwe' | ||
attr_encrypted :new_column_1, key: '2asd2asd2asd2asd2asd2asd2asd2asd' | ||
|
||
transcryptor_migrate :column_1, :new_column_1 | ||
end | ||
|
||
describe Transcryptor::ActiveRecord::ZeroDowntime do | ||
let(:instance) { ActiveRecordZeroDowntimeSpec.create(column_1: 'test') } | ||
|
||
it 'assigns encrypted data for both attributes' do | ||
expect(instance.new_column_1).to eq('test') | ||
end | ||
|
||
it 'changes new attribute on change of old' do | ||
instance.column_1 = 'another' | ||
expect(instance.new_column_1).to eq('another') | ||
end | ||
end |