From c43ed2ffd21c5dd6f4af31a087abbb65c1171529 Mon Sep 17 00:00:00 2001 From: Jill Klang Date: Thu, 25 Apr 2024 12:23:26 -0600 Subject: [PATCH] Add compatibility for attr_encrypted 4+ (#262) This method assumes that the class is using attr_encrypted for its encryption. That gem introduced a breaking change in its most recent major version upgrade to help with Rails 7.0 compatibility. This ensures both versions of attr_encrypted will be compatible with data_taster and clarifies the intention for consumers that want to use this method. This may be compatible with ActiveRecord encryption but is untested. --- packages/data_taster/Gemfile.lock | 2 +- packages/data_taster/docs/CHANGELOG.md | 4 ++++ .../data_taster/gemfiles/rails_6_0.gemfile.lock | 2 +- .../data_taster/gemfiles/rails_6_1.gemfile.lock | 2 +- .../data_taster/gemfiles/rails_7_0.gemfile.lock | 2 +- packages/data_taster/lib/data_taster/flavors.rb | 16 +++++++++++++++- packages/data_taster/lib/data_taster/version.rb | 2 +- 7 files changed, 24 insertions(+), 6 deletions(-) diff --git a/packages/data_taster/Gemfile.lock b/packages/data_taster/Gemfile.lock index 0ef2f12d..3b6b76b5 100644 --- a/packages/data_taster/Gemfile.lock +++ b/packages/data_taster/Gemfile.lock @@ -11,7 +11,7 @@ PATH PATH remote: . specs: - data_taster (0.2.2) + data_taster (0.3.0) rails (>= 6.0) GEM diff --git a/packages/data_taster/docs/CHANGELOG.md b/packages/data_taster/docs/CHANGELOG.md index 8ce68de8..98f1abc5 100644 --- a/packages/data_taster/docs/CHANGELOG.md +++ b/packages/data_taster/docs/CHANGELOG.md @@ -1,5 +1,9 @@ ## [Unreleased] +## [0.2.0] - 2024-04-25 + +- Add compatibility for encryption with newer versions of attr_encrypted + ## [0.2.0] - 2023-12-24 - Rollout of basic features. See README for details. diff --git a/packages/data_taster/gemfiles/rails_6_0.gemfile.lock b/packages/data_taster/gemfiles/rails_6_0.gemfile.lock index 2da8a517..861d033b 100644 --- a/packages/data_taster/gemfiles/rails_6_0.gemfile.lock +++ b/packages/data_taster/gemfiles/rails_6_0.gemfile.lock @@ -11,7 +11,7 @@ PATH PATH remote: .. specs: - data_taster (0.2.2) + data_taster (0.3.0) rails (>= 6.0) GEM diff --git a/packages/data_taster/gemfiles/rails_6_1.gemfile.lock b/packages/data_taster/gemfiles/rails_6_1.gemfile.lock index 2314621f..25b04f15 100644 --- a/packages/data_taster/gemfiles/rails_6_1.gemfile.lock +++ b/packages/data_taster/gemfiles/rails_6_1.gemfile.lock @@ -11,7 +11,7 @@ PATH PATH remote: .. specs: - data_taster (0.2.2) + data_taster (0.3.0) rails (>= 6.0) GEM diff --git a/packages/data_taster/gemfiles/rails_7_0.gemfile.lock b/packages/data_taster/gemfiles/rails_7_0.gemfile.lock index 3ca99a6b..cb7910fb 100644 --- a/packages/data_taster/gemfiles/rails_7_0.gemfile.lock +++ b/packages/data_taster/gemfiles/rails_7_0.gemfile.lock @@ -11,7 +11,7 @@ PATH PATH remote: .. specs: - data_taster (0.2.2) + data_taster (0.3.0) rails (>= 6.0) GEM diff --git a/packages/data_taster/lib/data_taster/flavors.rb b/packages/data_taster/lib/data_taster/flavors.rb index b15f8bfa..2830918a 100644 --- a/packages/data_taster/lib/data_taster/flavors.rb +++ b/packages/data_taster/lib/data_taster/flavors.rb @@ -30,7 +30,21 @@ def skip_sanitization def encrypt(klass, column, value = nil) value_to_encrypt = value || default_value_for(column) - klass.new.encrypt(column, value_to_encrypt) + klass_instance = klass.new + if klass_instance.respond_to?(:attr_encrypted_encrypt) + klass_instance.attr_encrypted_encrypt(column, value_to_encrypt) + elsif klass_instance.respond_to?(:encrypt) + klass_instance.encrypt(column, value_to_encrypt) + else + raise encryption_error_message + end + end + + def encryption_error_message + [ + "DataTaster only supports encryption if your model is configured with attr_encrypted.", + "Please visit https://github.com/attr-encrypted/attr_encrypted for more details on setup.", + ].join(" ") end def default_value_for(column) diff --git a/packages/data_taster/lib/data_taster/version.rb b/packages/data_taster/lib/data_taster/version.rb index 4b158d2c..fcae3e07 100644 --- a/packages/data_taster/lib/data_taster/version.rb +++ b/packages/data_taster/lib/data_taster/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module DataTaster - VERSION = "0.2.2" + VERSION = "0.3.0" end