From a189c9e05ab58236d5d3cd4b1fd08357d8d72939 Mon Sep 17 00:00:00 2001 From: Brian Freese Date: Fri, 3 Feb 2023 16:33:47 -0600 Subject: [PATCH 1/4] Special branch used for migrating from Ruby 2.x/Rails 6.x to Ruby 3.x/Rails 7.x --- .gitignore | 1 + lib/attr_encrypted.rb | 48 ++++---- lib/attr_encrypted/adapters/active_record.rb | 49 ++++++--- test/active_record_test.rb | 109 +++++++++---------- test/attr_encrypted_test.rb | 22 ++-- test/legacy_attr_encrypted_test.rb | 12 +- 6 files changed, 127 insertions(+), 114 deletions(-) diff --git a/.gitignore b/.gitignore index a2e2d631..b2dc395e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .bundle +.idea .DS_Store .ruby-version pkg diff --git a/lib/attr_encrypted.rb b/lib/attr_encrypted.rb index 88e5f65e..844e8661 100644 --- a/lib/attr_encrypted.rb +++ b/lib/attr_encrypted.rb @@ -10,7 +10,7 @@ def self.extended(base) # :nodoc: base.class_eval do include InstanceMethods attr_writer :attr_encrypted_options - @attr_encrypted_options, @encrypted_attributes = {}, {} + @attr_encrypted_options, @attr_encrypted_encrypted_attributes = {}, {} end end @@ -160,11 +160,11 @@ def attr_encrypted(*attributes) end define_method(attribute) do - instance_variable_get("@#{attribute}") || instance_variable_set("@#{attribute}", decrypt(attribute, send(encrypted_attribute_name))) + instance_variable_get("@#{attribute}") || instance_variable_set("@#{attribute}", __attr_decrypt__(attribute, send(encrypted_attribute_name))) end define_method("#{attribute}=") do |value| - send("#{encrypted_attribute_name}=", encrypt(attribute, value)) + send("#{encrypted_attribute_name}=", __attr_encrypt__(attribute, value)) instance_variable_set("@#{attribute}", value) end @@ -173,7 +173,7 @@ def attr_encrypted(*attributes) value.respond_to?(:empty?) ? !value.empty? : !!value end - encrypted_attributes[attribute.to_sym] = options.merge(attribute: encrypted_attribute_name) + self.attr_encrypted_encrypted_attributes[attribute.to_sym] = options.merge(attribute: encrypted_attribute_name) end end @@ -223,7 +223,7 @@ def attr_encrypted_default_options # User.attr_encrypted?(:name) # false # User.attr_encrypted?(:email) # true def attr_encrypted?(attribute) - encrypted_attributes.has_key?(attribute.to_sym) + attr_encrypted_encrypted_attributes.has_key?(attribute.to_sym) end # Decrypts a value for the attribute specified @@ -235,8 +235,8 @@ def attr_encrypted?(attribute) # end # # email = User.decrypt(:email, 'SOME_ENCRYPTED_EMAIL_STRING') - def decrypt(attribute, encrypted_value, options = {}) - options = encrypted_attributes[attribute.to_sym].merge(options) + def __attr_decrypt__(attribute, encrypted_value, options = {}) + options = attr_encrypted_encrypted_attributes[attribute.to_sym].merge(options) if options[:if] && !options[:unless] && not_empty?(encrypted_value) encrypted_value = encrypted_value.unpack(options[:encode]).first if options[:encode] value = options[:encryptor].send(options[:decrypt_method], options.merge!(value: encrypted_value)) @@ -261,8 +261,8 @@ def decrypt(attribute, encrypted_value, options = {}) # end # # encrypted_email = User.encrypt(:email, 'test@example.com') - def encrypt(attribute, value, options = {}) - options = encrypted_attributes[attribute.to_sym].merge(options) + def __attr_encrypt__(attribute, value, options = {}) + options = attr_encrypted_encrypted_attributes[attribute.to_sym].merge(options) if options[:if] && !options[:unless] && (options[:allow_empty_value] || not_empty?(value)) value = options[:marshal] ? options[:marshaler].send(options[:dump_method], value) : value.to_s encrypted_value = options[:encryptor].send(options[:encrypt_method], options.merge!(value: value)) @@ -286,9 +286,9 @@ def not_empty?(value) # attr_encrypted :email, key: 'my secret key' # end # - # User.encrypted_attributes # { email: { attribute: 'encrypted_email', key: 'my secret key' } } - def encrypted_attributes - @encrypted_attributes ||= superclass.encrypted_attributes.dup + # User.attr_encrypted_encrypted_attributes # { email: { attribute: 'encrypted_email', key: 'my secret key' } } + def attr_encrypted_encrypted_attributes + @attr_encrypted_encrypted_attributes ||= superclass.attr_encrypted_encrypted_attributes.dup end # Forwards calls to :encrypt_#{attribute} or :decrypt_#{attribute} to the corresponding encrypt or decrypt method @@ -325,10 +325,10 @@ module InstanceMethods # # @user = User.new('some-secret-key') # @user.decrypt(:email, 'SOME_ENCRYPTED_EMAIL_STRING') - def decrypt(attribute, encrypted_value) - encrypted_attributes[attribute.to_sym][:operation] = :decrypting - encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(encrypted_value) - self.class.decrypt(attribute, encrypted_value, evaluated_attr_encrypted_options_for(attribute)) + def __attr_decrypt__(attribute, encrypted_value) + attr_encrypted_encrypted_attributes[attribute.to_sym][:operation] = :decrypting + attr_encrypted_encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(encrypted_value) + self.class.__attr_decrypt__(attribute, encrypted_value, evaluated_attr_encrypted_options_for(attribute)) end # Encrypts a value for the attribute specified using options evaluated in the current object's scope @@ -346,19 +346,19 @@ def decrypt(attribute, encrypted_value) # # @user = User.new('some-secret-key') # @user.encrypt(:email, 'test@example.com') - def encrypt(attribute, value) - encrypted_attributes[attribute.to_sym][:operation] = :encrypting - encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(value) - self.class.encrypt(attribute, value, evaluated_attr_encrypted_options_for(attribute)) + def __attr_encrypt__(attribute, value) + attr_encrypted_encrypted_attributes[attribute.to_sym][:operation] = :encrypting + attr_encrypted_encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(value) + self.class.__attr_encrypt__(attribute, value, evaluated_attr_encrypted_options_for(attribute)) end # Copies the class level hash of encrypted attributes with virtual attribute names as keys # and their corresponding options as values to the instance # - def encrypted_attributes - @encrypted_attributes ||= begin + def attr_encrypted_encrypted_attributes + @attr_encrypted_encrypted_attributes ||= begin duplicated= {} - self.class.encrypted_attributes.map { |key, value| duplicated[key] = value.dup } + self.class.attr_encrypted_encrypted_attributes.map { |key, value| duplicated[key] = value.dup } duplicated end end @@ -368,7 +368,7 @@ def encrypted_attributes # Returns attr_encrypted options evaluated in the current object's scope for the attribute specified def evaluated_attr_encrypted_options_for(attribute) evaluated_options = Hash.new - attributes = encrypted_attributes[attribute.to_sym] + attributes = attr_encrypted_encrypted_attributes[attribute.to_sym] attribute_option_value = attributes[:attribute] [:if, :unless, :value_present, :allow_empty_value].each do |option| diff --git a/lib/attr_encrypted/adapters/active_record.rb b/lib/attr_encrypted/adapters/active_record.rb index fca9343e..a72a3a27 100644 --- a/lib/attr_encrypted/adapters/active_record.rb +++ b/lib/attr_encrypted/adapters/active_record.rb @@ -4,6 +4,8 @@ module AttrEncrypted module Adapters module ActiveRecord + RAILS_VERSION = Gem::Version.new(::ActiveRecord::VERSION::STRING).freeze + def self.extended(base) # :nodoc: base.class_eval do @@ -11,7 +13,7 @@ def self.extended(base) # :nodoc: alias_method :reload_without_attr_encrypted, :reload def reload(*args, &block) result = reload_without_attr_encrypted(*args, &block) - self.class.encrypted_attributes.keys.each do |attribute_name| + self.class.attr_encrypted_encrypted_attributes.keys.each do |attribute_name| instance_variable_set("@#{attribute_name}", nil) end result @@ -27,12 +29,12 @@ class << self def perform_attribute_assignment(method, new_attributes, *args) return if new_attributes.blank? - send method, new_attributes.reject { |k, _| self.class.encrypted_attributes.key?(k.to_sym) }, *args - send method, new_attributes.reject { |k, _| !self.class.encrypted_attributes.key?(k.to_sym) }, *args + send method, new_attributes.reject { |k, _| self.class.attr_encrypted_encrypted_attributes.key?(k.to_sym) }, *args + send method, new_attributes.reject { |k, _| !self.class.attr_encrypted_encrypted_attributes.key?(k.to_sym) }, *args end private :perform_attribute_assignment - if ::ActiveRecord::VERSION::STRING > "3.1" + if Gem::Requirement.new('> 3.1').satisfied_by?(RAILS_VERSION) alias_method :assign_attributes_without_attr_encrypted, :assign_attributes def assign_attributes(*args) perform_attribute_assignment :assign_attributes_without_attr_encrypted, *args @@ -53,21 +55,15 @@ def attr_encrypted(*attrs) super options = attrs.extract_options! attr = attrs.pop - attribute attr if ::ActiveRecord::VERSION::STRING >= "5.1.0" - options.merge! encrypted_attributes[attr] + attribute attr + options.merge! attr_encrypted_encrypted_attributes[attr] define_method("#{attr}_was") do attribute_was(attr) end - if ::ActiveRecord::VERSION::STRING >= "4.1" - define_method("#{attr}_changed?") do |options = {}| - attribute_changed?(attr, options) - end - else - define_method("#{attr}_changed?") do - attribute_changed?(attr) - end + define_method("#{attr}_changed?") do |options = {}| + attribute_changed?(attr, **options) end define_method("#{attr}_change") do @@ -75,7 +71,26 @@ def attr_encrypted(*attrs) end define_method("#{attr}_with_dirtiness=") do |value| - attribute_will_change!(attr) if value != __send__(attr) + current_value = __send__(attr) + value_changed = current_value != value + + ## Source: https://github.com/priyankatapar/attr_encrypted/commit/7e8702bd5418c927a39d8dd72c0adbea522d5663 + # In ActiveRecord 5.2+, due to changes to the way virtual + # attributes are handled, @attributes[attr].value is nil which + # breaks attribute_was. Setting it here returns us to the expected + # behavior. + if RAILS_VERSION >= Gem::Version.new(5.2) + if value_changed + # This is needed support attribute_was before a record has + # been saved + set_attribute_was(attr, current_value) if value_changed && RAILS_VERSION <= Gem::Version.new(6.0) + + # This is needed to support attribute_was after a record has + # been saved + @attributes.write_from_user(attr.to_s, value) + end + end + attribute_will_change!(attr) if value != current_value __send__("#{attr}_without_dirtiness=", value) end @@ -122,10 +137,10 @@ def method_missing_with_attr_encrypted(method, *args, &block) if match = /^(find|scoped)_(all_by|by)_([_a-zA-Z]\w*)$/.match(method.to_s) attribute_names = match.captures.last.split('_and_') attribute_names.each_with_index do |attribute, index| - if attr_encrypted?(attribute) && encrypted_attributes[attribute.to_sym][:mode] == :single_iv_and_salt + if attr_encrypted?(attribute) && attr_encrypted_encrypted_attributes[attribute.to_sym][:mode] == :single_iv_and_salt args[index] = send("encrypt_#{attribute}", args[index]) warn "DEPRECATION WARNING: This feature will be removed in the next major release." - attribute_names[index] = encrypted_attributes[attribute.to_sym][:attribute] + attribute_names[index] = attr_encrypted_encrypted_attributes[attribute.to_sym][:attribute] end end method = "#{match.captures[0]}_#{match.captures[1]}_#{attribute_names.join('_and_')}".to_sym diff --git a/test/active_record_test.rb b/test/active_record_test.rb index 8ec31aea..54be18fa 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -2,6 +2,7 @@ require_relative 'test_helper' +RAILS_VERSION = Gem::Version.new(::ActiveRecord::VERSION::STRING).freeze ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') def create_tables @@ -45,16 +46,14 @@ def create_tables ActiveRecord::MissingAttributeError = ActiveModel::MissingAttributeError unless defined?(ActiveRecord::MissingAttributeError) -if ::ActiveRecord::VERSION::STRING > "4.0" - module Rack - module Test - class UploadedFile; end - end +module Rack + module Test + class UploadedFile; end end - - require 'action_controller/metal/strong_parameters' end +require 'action_controller/metal/strong_parameters' + class Person < ActiveRecord::Base self.attr_encrypted_options[:mode] = :per_attribute_iv_and_salt attr_encrypted :email, key: SECRET_KEY @@ -85,7 +84,7 @@ class Account < ActiveRecord::Base attr_encrypted :password, key: :password_encryption_key def encrypting?(attr) - encrypted_attributes[attr][:operation] == :encrypting + attr_encrypted_encrypted_attributes[attr][:operation] == :encrypting end def password_encryption_key @@ -106,7 +105,6 @@ class PersonWithSerialization < ActiveRecord::Base class UserWithProtectedAttribute < ActiveRecord::Base self.table_name = 'users' attr_encrypted :password, key: SECRET_KEY - attr_protected :is_admin if ::ActiveRecord::VERSION::STRING < "4.0" end class PersonUsingAlias < ActiveRecord::Base @@ -221,52 +219,53 @@ def test_attribute_was_works_when_options_for_old_encrypted_value_are_different_ assert_equal pw.reverse, account.password end - if ::ActiveRecord::VERSION::STRING > "4.0" - def test_should_assign_attributes - @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false) - @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true).permit(:login) - assert_equal 'modified', @user.login + if Gem::Requirement.new('>= 5.2').satisfied_by?(RAILS_VERSION) + def test_should_create_will_save_change_to_predicate + person = Person.create!(email: 'test@example.com') + refute person.will_save_change_to_email? + person.email = 'test@example.com' + refute person.will_save_change_to_email? + person.email = 'test2@example.com' + assert person.will_save_change_to_email? end - def test_should_not_assign_protected_attributes - @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false) - @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true).permit(:login) - assert !@user.is_admin? + def test_should_create_saved_change_to_predicate + person = Person.create!(email: 'test@example.com') + assert person.saved_change_to_email? + person.reload + person.email = 'test@example.com' + refute person.saved_change_to_email? + person.email = nil + refute person.saved_change_to_email? + person.email = 'test2@example.com' + refute person.saved_change_to_email? + person.save + assert person.saved_change_to_email? end + end - def test_should_raise_exception_if_not_permitted - @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false) - assert_raises ActiveModel::ForbiddenAttributesError do - @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true) - end - end + def test_should_assign_attributes + @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false) + @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true).permit(:login) + assert_equal 'modified', @user.login + end - def test_should_raise_exception_on_init_if_not_permitted - assert_raises ActiveModel::ForbiddenAttributesError do - @user = UserWithProtectedAttribute.new ActionController::Parameters.new(login: 'modified', is_admin: true) - end - end - else - def test_should_assign_attributes - @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false) - @user.attributes = { login: 'modified', is_admin: true } - assert_equal 'modified', @user.login - end + def test_should_not_assign_protected_attributes + @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false) + @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true).permit(:login) + assert !@user.is_admin? + end - def test_should_not_assign_protected_attributes - @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false) - @user.attributes = { login: 'modified', is_admin: true } - assert !@user.is_admin? + def test_should_raise_exception_if_not_permitted + @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false) + assert_raises ActiveModel::ForbiddenAttributesError do + @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true) end + end - def test_should_assign_protected_attributes - @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false) - if ::ActiveRecord::VERSION::STRING > "3.1" - @user.send(:assign_attributes, { login: 'modified', is_admin: true }, without_protection: true) - else - @user.send(:attributes=, { login: 'modified', is_admin: true }, false) - end - assert @user.is_admin? + def test_should_raise_exception_on_init_if_not_permitted + assert_raises ActiveModel::ForbiddenAttributesError do + @user = UserWithProtectedAttribute.new ActionController::Parameters.new(login: 'modified', is_admin: true) end end @@ -279,23 +278,21 @@ def test_should_allow_proc_based_mode @person = PersonWithProcMode.create(email: 'test@example.com', credentials: 'password123') # Email is :per_attribute_iv_and_salt - assert_equal @person.class.encrypted_attributes[:email][:mode].class, Proc - assert_equal @person.class.encrypted_attributes[:email][:mode].call, :per_attribute_iv_and_salt + assert_equal @person.class.attr_encrypted_encrypted_attributes[:email][:mode].class, Proc + assert_equal @person.class.attr_encrypted_encrypted_attributes[:email][:mode].call, :per_attribute_iv_and_salt refute_nil @person.encrypted_email_salt refute_nil @person.encrypted_email_iv # Credentials is :single_iv_and_salt - assert_equal @person.class.encrypted_attributes[:credentials][:mode].class, Proc - assert_equal @person.class.encrypted_attributes[:credentials][:mode].call, :single_iv_and_salt + assert_equal @person.class.attr_encrypted_encrypted_attributes[:credentials][:mode].class, Proc + assert_equal @person.class.attr_encrypted_encrypted_attributes[:credentials][:mode].call, :single_iv_and_salt assert_nil @person.encrypted_credentials_salt assert_nil @person.encrypted_credentials_iv end - if ::ActiveRecord::VERSION::STRING > "3.1" - def test_should_allow_assign_attributes_with_nil - @person = Person.new - assert_nil(@person.assign_attributes nil) - end + def test_should_allow_assign_attributes_with_nil + @person = Person.new + assert_nil(@person.assign_attributes nil) end def test_that_alias_encrypts_column diff --git a/test/attr_encrypted_test.rb b/test/attr_encrypted_test.rb index 84cb130a..320c8c29 100644 --- a/test/attr_encrypted_test.rb +++ b/test/attr_encrypted_test.rb @@ -83,11 +83,11 @@ def setup end def test_should_store_email_in_encrypted_attributes - assert User.encrypted_attributes.include?(:email) + assert User.attr_encrypted_encrypted_attributes.include?(:email) end def test_should_not_store_salt_in_encrypted_attributes - refute User.encrypted_attributes.include?(:salt) + refute User.attr_encrypted_encrypted_attributes.include?(:salt) end def test_attr_encrypted_should_return_true_for_email @@ -95,7 +95,7 @@ def test_attr_encrypted_should_return_true_for_email end def test_attr_encrypted_should_not_use_the_same_attribute_name_for_two_attributes_in_the_same_line - refute_equal User.encrypted_attributes[:email][:attribute], User.encrypted_attributes[:without_encoding][:attribute] + refute_equal User.attr_encrypted_encrypted_attributes[:email][:attribute], User.attr_encrypted_encrypted_attributes[:without_encoding][:attribute] end def test_attr_encrypted_should_return_false_for_salt @@ -154,7 +154,7 @@ def test_should_decrypt_email def test_should_decrypt_email_when_reading @user = User.new assert_nil @user.email - options = @user.encrypted_attributes[:email] + options = @user.attr_encrypted_encrypted_attributes[:email] iv = @user.send(:generate_iv, options[:algorithm]) encoded_iv = [iv].pack(options[:encode_iv]) salt = SecureRandom.random_bytes @@ -223,7 +223,7 @@ def test_should_use_options_found_in_the_attr_encrypted_options_attribute end def test_should_inherit_encrypted_attributes - assert_equal [User.encrypted_attributes.keys, :testing].flatten.collect { |key| key.to_s }.sort, Admin.encrypted_attributes.keys.collect { |key| key.to_s }.sort + assert_equal [User.attr_encrypted_encrypted_attributes.keys, :testing].flatten.collect { |key| key.to_s }.sort, Admin.attr_encrypted_encrypted_attributes.keys.collect { |key| key.to_s }.sort end def test_should_inherit_attr_encrypted_options @@ -233,7 +233,7 @@ def test_should_inherit_attr_encrypted_options def test_should_not_inherit_unrelated_attributes assert SomeOtherClass.attr_encrypted_options.empty? - assert SomeOtherClass.encrypted_attributes.empty? + assert SomeOtherClass.attr_encrypted_encrypted_attributes.empty? end def test_should_evaluate_a_symbol_option @@ -304,7 +304,7 @@ def test_should_encrypt_empty_with_truthy_allow_empty_value_option end def test_should_work_with_aliased_attr_encryptor - assert User.encrypted_attributes.include?(:aliased) + assert User.attr_encrypted_encrypted_attributes.include?(:aliased) end def test_should_always_reset_options @@ -385,8 +385,8 @@ def test_should_decrypt_second_record end def test_should_specify_the_default_algorithm - assert YetAnotherClass.encrypted_attributes[:email][:algorithm] - assert_equal YetAnotherClass.encrypted_attributes[:email][:algorithm], 'aes-256-gcm' + assert YetAnotherClass.attr_encrypted_encrypted_attributes[:email][:algorithm] + assert_equal YetAnotherClass.attr_encrypted_encrypted_attributes[:email][:algorithm], 'aes-256-gcm' end def test_should_not_encode_iv_when_encode_iv_is_false @@ -475,8 +475,8 @@ def test_encrypted_attributes_state_is_not_shared another_user = User.new - assert_equal :encrypting, user.encrypted_attributes[:ssn][:operation] - assert_nil another_user.encrypted_attributes[:ssn][:operation] + assert_equal :encrypting, user.attr_encrypted_encrypted_attributes[:ssn][:operation] + assert_nil another_user.attr_encrypted_encrypted_attributes[:ssn][:operation] end def test_should_not_by_default_generate_key_when_attribute_is_empty diff --git a/test/legacy_attr_encrypted_test.rb b/test/legacy_attr_encrypted_test.rb index 875086d2..49cbec9b 100644 --- a/test/legacy_attr_encrypted_test.rb +++ b/test/legacy_attr_encrypted_test.rb @@ -58,11 +58,11 @@ def self.call(object) class LegacyAttrEncryptedTest < Minitest::Test def test_should_store_email_in_encrypted_attributes - assert LegacyUser.encrypted_attributes.include?(:email) + assert LegacyUser.attr_encrypted_encrypted_attributes.include?(:email) end def test_should_not_store_salt_in_encrypted_attributes - assert !LegacyUser.encrypted_attributes.include?(:salt) + assert !LegacyUser.attr_encrypted_encrypted_attributes.include?(:salt) end def test_attr_encrypted_should_return_true_for_email @@ -70,7 +70,7 @@ def test_attr_encrypted_should_return_true_for_email end def test_attr_encrypted_should_not_use_the_same_attribute_name_for_two_attributes_in_the_same_line - refute_equal LegacyUser.encrypted_attributes[:email][:attribute], LegacyUser.encrypted_attributes[:without_encoding][:attribute] + refute_equal LegacyUser.attr_encrypted_encrypted_attributes[:email][:attribute], LegacyUser.attr_encrypted_encrypted_attributes[:without_encoding][:attribute] end def test_attr_encrypted_should_return_false_for_salt @@ -201,7 +201,7 @@ def test_should_use_options_found_in_the_attr_encrypted_options_attribute end def test_should_inherit_encrypted_attributes - assert_equal [LegacyUser.encrypted_attributes.keys, :testing].flatten.collect { |key| key.to_s }.sort, LegacyAdmin.encrypted_attributes.keys.collect { |key| key.to_s }.sort + assert_equal [LegacyUser.attr_encrypted_encrypted_attributes.keys, :testing].flatten.collect { |key| key.to_s }.sort, LegacyAdmin.attr_encrypted_encrypted_attributes.keys.collect { |key| key.to_s }.sort end def test_should_inherit_attr_encrypted_options @@ -211,7 +211,7 @@ def test_should_inherit_attr_encrypted_options def test_should_not_inherit_unrelated_attributes assert LegacySomeOtherClass.attr_encrypted_options.empty? - assert LegacySomeOtherClass.encrypted_attributes.empty? + assert LegacySomeOtherClass.attr_encrypted_encrypted_attributes.empty? end def test_should_evaluate_a_symbol_option @@ -268,7 +268,7 @@ def test_should_not_encrypt_with_true_unless end def test_should_work_with_aliased_attr_encryptor - assert LegacyUser.encrypted_attributes.include?(:aliased) + assert LegacyUser.attr_encrypted_encrypted_attributes.include?(:aliased) end def test_should_always_reset_options From 63ca36f4fbf26566e501fb81dff8df6b89665f71 Mon Sep 17 00:00:00 2001 From: Brian Freese Date: Fri, 3 Feb 2023 16:43:52 -0600 Subject: [PATCH 2/4] Allow branch to publish --- .github/workflows/publish-on-push.yaml | 16 ++++++++++++++++ attr_encrypted.gemspec | 9 ++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/publish-on-push.yaml diff --git a/.github/workflows/publish-on-push.yaml b/.github/workflows/publish-on-push.yaml new file mode 100644 index 00000000..f89092c7 --- /dev/null +++ b/.github/workflows/publish-on-push.yaml @@ -0,0 +1,16 @@ +name: Publish Ruby Gem (Pushed) + +on: + push + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Build and publish gem + uses: jstastny/publish-gem-to-github@v2.1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + owner: acima-credit + diff --git a/attr_encrypted.gemspec b/attr_encrypted.gemspec index 5b20c62c..4313093e 100644 --- a/attr_encrypted.gemspec +++ b/attr_encrypted.gemspec @@ -8,7 +8,14 @@ require 'date' Gem::Specification.new do |s| s.name = 'attr_encrypted' - s.version = AttrEncrypted::Version.string + current_branch = `git branch --remote --contains | sed "s|[[:space:]]*origin/||"`.strip + branch_commit = `git rev-parse HEAD`.strip[0..6] + + if current_branch == 'master' + spec.version = AttrEncrypted::Version.strin + else + spec.version = "#{AttrEncrypted::Version.strin}-#{branch_commit}" + end s.date = Date.today s.summary = 'Encrypt and decrypt attributes' From a0dd47be65efebf8f8135845e883efa93e061e33 Mon Sep 17 00:00:00 2001 From: Brian Freese Date: Fri, 3 Feb 2023 17:02:29 -0600 Subject: [PATCH 3/4] Fix gemspec --- attr_encrypted.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/attr_encrypted.gemspec b/attr_encrypted.gemspec index 4313093e..58b8163c 100644 --- a/attr_encrypted.gemspec +++ b/attr_encrypted.gemspec @@ -12,9 +12,9 @@ Gem::Specification.new do |s| branch_commit = `git rev-parse HEAD`.strip[0..6] if current_branch == 'master' - spec.version = AttrEncrypted::Version.strin + s.version = AttrEncrypted::Version.strin else - spec.version = "#{AttrEncrypted::Version.strin}-#{branch_commit}" + s.version = "#{AttrEncrypted::Version.strin}-#{branch_commit}" end s.date = Date.today From 0a240536aeeb7975548ba374dee2756f30a77468 Mon Sep 17 00:00:00 2001 From: Brian Freese Date: Fri, 3 Feb 2023 17:05:28 -0600 Subject: [PATCH 4/4] Fix gemspec part 2 --- attr_encrypted.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/attr_encrypted.gemspec b/attr_encrypted.gemspec index 58b8163c..21e1316b 100644 --- a/attr_encrypted.gemspec +++ b/attr_encrypted.gemspec @@ -12,9 +12,9 @@ Gem::Specification.new do |s| branch_commit = `git rev-parse HEAD`.strip[0..6] if current_branch == 'master' - s.version = AttrEncrypted::Version.strin + s.version = AttrEncrypted::Version.string else - s.version = "#{AttrEncrypted::Version.strin}-#{branch_commit}" + s.version = "#{AttrEncrypted::Version.string}-#{branch_commit}" end s.date = Date.today