Skip to content

Commit

Permalink
Merge pull request #25 from optimalworkshop/make_deidentify_associati…
Browse files Browse the repository at this point in the history
…ons_appendable

Aggregate associations when calling deidentify_associations more than once per class
  • Loading branch information
bduran82 authored Jul 15, 2021
2 parents b264119 + 8e59027 commit b633fbf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
deidentify (2.0.0)
deidentify (2.0.1)
rails (>= 5.0.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion deidentify.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = 'deidentify'
s.version = '2.0.0'
s.version = '2.0.1'
s.summary = 'Deidentify a rails model'
s.description = 'A gem to allow deidentification of certain fields'
s.authors = ['Lucy Dement']
Expand Down
3 changes: 2 additions & 1 deletion lib/deidentify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def configure
self.deidentify_configuration = {}

class_attribute :associations_to_deidentify
self.associations_to_deidentify = []

define_model_callbacks :deidentify
after_deidentify :deidentify_associations!, if: -> { associations_to_deidentify.present? }
Expand All @@ -53,7 +54,7 @@ def deidentify(column, method:, **options)
end

def deidentify_associations(*associations)
self.associations_to_deidentify = associations
self.associations_to_deidentify += associations
end
end

Expand Down
19 changes: 19 additions & 0 deletions spec/deidentify_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@
end
end

context 'when called more than once' do
let(:second_bubble) { Bubble.create!(colour: 'red') }
let(:party) { Party.create! }

before do
Party.deidentify_associations :bubbles
Party.deidentify_associations :main_bubble
Bubble.deidentify :colour, method: :replace, new_value: new_value

party.update!(bubbles: [second_bubble], main_bubble: bubble)
end

it 'call deidentify on all associations' do
party.deidentify!
expect(bubble.reload.colour).to eq new_value
expect(second_bubble.reload.colour).to eq new_value
end
end

context 'when a loop is created by deidentifing associations' do
let(:party) { Party.create! }
let(:second_bubble) { Bubble.create!(party: party, colour: 'red') }
Expand Down
7 changes: 4 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Party < ActiveRecord::Base
include Deidentify

has_many :bubbles
belongs_to :main_bubble, class_name: 'Bubble'
end

RSpec.configure do |config|
Expand All @@ -30,15 +31,15 @@ class Party < ActiveRecord::Base
'CREATE TABLE bubbles (id INTEGER NOT NULL PRIMARY KEY, party_id INTEGER, colour VARCHAR(32), quantity INTEGER)'
)
ActiveRecord::Base.connection.execute(
'CREATE TABLE parties (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(32))'
'CREATE TABLE parties (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(32), main_bubble_id INTEGER)'
)
end

config.before(:each) do
Bubble.deidentify_configuration = {}
Bubble.associations_to_deidentify = nil
Bubble.associations_to_deidentify = []

Party.deidentify_configuration = {}
Party.associations_to_deidentify = nil
Party.associations_to_deidentify = []
end
end

0 comments on commit b633fbf

Please sign in to comment.