From ed7da081560bcc0e067fe41db1f4a26c29b231d5 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Mon, 2 Sep 2024 15:43:22 -0700 Subject: [PATCH 1/8] Relax gemspec to allow Rails 8.0; add Rails main branch to CI Rails 8 does not have an alpha or beta rubygems release, but it sounds like a release is imminent (to coincide with Rails World in late September). To prepare for Rails 8, this PR relaxes the Rails requirement in the audited gemspec to allow "< 8.1" instead of "< 8.0". This will allow audited to be used with Rails 8.0 when it is released. To test that audited will work with Rails 8, I added a `rails-main` Gemfile to `Appraisals` that targets the Rails main branch, and included this in the CI matrix. --- .github/workflows/ci.yml | 15 +++++++++++++++ Appraisals | 7 +++++++ audited.gemspec | 6 +++--- gemfiles/rails_main.gemfile | 10 ++++++++++ spec/rails_app/config/application.rb | 4 ++++ 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 gemfiles/rails_main.gemfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed0a8dc4..1296c6d3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,7 @@ jobs: - rails70 - rails71 - rails72 + - rails_main db: [POSTGRES, MYSQL, SQLITE] exclude: # MySQL has issues on Ruby 2.3 @@ -98,6 +99,20 @@ jobs: - appraisal: rails72 ruby: "3.0" + # Rails main supports Ruby 3.1+ + - appraisal: rails_main + ruby: "2.3" + - appraisal: rails_main + ruby: "2.4" + - appraisal: rails_main + ruby: "2.5" + - appraisal: rails_main + ruby: "2.6" + - appraisal: rails_main + ruby: "2.7" + - appraisal: rails_main + ruby: "3.0" + services: postgres: image: postgres diff --git a/Appraisals b/Appraisals index 6df14d46..17d2d81e 100644 --- a/Appraisals +++ b/Appraisals @@ -44,3 +44,10 @@ appraise "rails72" do gem "pg", "~> 1.1" gem "sqlite3", ">= 1.4" end + +appraise "rails_main" do + gem "rails", github: "rails/rails", branch: "main" + gem "mysql2", "~> 0.5" + gem "pg", "~> 1.1" + gem "sqlite3", ">= 2.0" +end diff --git a/audited.gemspec b/audited.gemspec index bc1b17ff..8257f8b2 100644 --- a/audited.gemspec +++ b/audited.gemspec @@ -16,11 +16,11 @@ Gem::Specification.new do |gem| gem.required_ruby_version = ">= 2.3.0" - gem.add_dependency "activerecord", ">= 5.2", "< 8.0" - gem.add_dependency "activesupport", ">= 5.2", "< 8.0" + gem.add_dependency "activerecord", ">= 5.2", "< 8.1" + gem.add_dependency "activesupport", ">= 5.2", "< 8.1" gem.add_development_dependency "appraisal" - gem.add_development_dependency "rails", ">= 5.2", "< 8.0" + gem.add_development_dependency "rails", ">= 5.2", "< 8.1" gem.add_development_dependency "rspec-rails" gem.add_development_dependency "standard" gem.add_development_dependency "single_cov" diff --git a/gemfiles/rails_main.gemfile b/gemfiles/rails_main.gemfile new file mode 100644 index 00000000..c0bd890a --- /dev/null +++ b/gemfiles/rails_main.gemfile @@ -0,0 +1,10 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "rails", github: "rails/rails", branch: "main" +gem "mysql2", "~> 0.5" +gem "pg", "~> 1.1" +gem "sqlite3", ">= 2.0" + +gemspec name: "audited", path: "../" diff --git a/spec/rails_app/config/application.rb b/spec/rails_app/config/application.rb index 6de73c8e..a485f848 100644 --- a/spec/rails_app/config/application.rb +++ b/spec/rails_app/config/application.rb @@ -33,6 +33,10 @@ class Application < Rails::Application if Rails.gem_version >= Gem::Version.new("7.1") config.active_support.cache_format_version = 7.1 end + + if Rails.gem_version >= Gem::Version.new("8.0.0.alpha") + config.active_support.to_time_preserves_timezone = :zone + end end end From 1a7b90959a0a068805dbd6460d5d2ebd25542e13 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Wed, 2 Oct 2024 08:25:45 -0700 Subject: [PATCH 2/8] Rails 8 has dropped support for Ruby 3.1 --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1296c6d3..7a208ce3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -99,7 +99,7 @@ jobs: - appraisal: rails72 ruby: "3.0" - # Rails main supports Ruby 3.1+ + # Rails main supports Ruby 3.2+ - appraisal: rails_main ruby: "2.3" - appraisal: rails_main @@ -112,6 +112,8 @@ jobs: ruby: "2.7" - appraisal: rails_main ruby: "3.0" + - appraisal: rails_main + ruby: "3.1" services: postgres: From e3a9d1c9b87b83096877a9682fe5f81042ed00bb Mon Sep 17 00:00:00 2001 From: mohammednasser-32 Date: Mon, 21 Oct 2024 20:39:30 +0300 Subject: [PATCH 3/8] allow calling audited multiple times --- lib/audited/auditor.rb | 38 +++++++++++++++++++++++----- spec/audited/auditor_spec.rb | 16 ++++++++++++ spec/support/active_record/models.rb | 6 +++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/audited/auditor.rb b/lib/audited/auditor.rb index c9c867ae..ae43fdb4 100644 --- a/lib/audited/auditor.rb +++ b/lib/audited/auditor.rb @@ -59,9 +59,16 @@ module ClassMethods # end # def audited(options = {}) - # don't allow multiple calls - return if included_modules.include?(Audited::Auditor::AuditedInstanceMethods) + audited? ? update_audited_options(options) : set_audit(options) + end + + private + + def audited? + included_modules.include?(Audited::Auditor::AuditedInstanceMethods) + end + def set_audit(options) extend Audited::Auditor::AuditedClassMethods include Audited::Auditor::AuditedInstanceMethods @@ -69,10 +76,7 @@ def audited(options = {}) class_attribute :audited_options, instance_writer: false attr_accessor :audit_version, :audit_comment - self.audited_options = options - normalize_audited_options - - self.audit_associated_with = audited_options[:associated_with] + set_audited_options(options) if audited_options[:comment_required] validate :presence_of_audit_comment @@ -100,6 +104,23 @@ def audited(options = {}) def has_associated_audits has_many :associated_audits, as: :associated, class_name: Audited.audit_class.name end + + def update_audited_options(new_options) + previous_audit_options = self.audited_options + set_audited_options(new_options) + self.reset_audited_columns + + log_message = "#{self.name} is already audited, audit options will be updated\n"\ + "before: #{previous_audit_options}\n"\ + "after: #{self.audited_options}" + Logger.new($stdout).info(log_message) + end + + def set_audited_options(options) + self.audited_options = options + normalize_audited_options + self.audit_associated_with = audited_options[:associated_with] + end end module AuditedInstanceMethods @@ -530,6 +551,11 @@ def calculate_non_audited_columns def class_auditing_enabled Audited.store.fetch("#{table_name}_auditing_enabled", true) end + + def reset_audited_columns + @audited_columns = nil + @non_audited_columns = nil + end end end end diff --git a/spec/audited/auditor_spec.rb b/spec/audited/auditor_spec.rb index cff4044b..15ff2f77 100644 --- a/spec/audited/auditor_spec.rb +++ b/spec/audited/auditor_spec.rb @@ -1270,4 +1270,20 @@ def stub_global_max_audits(max_audits) }.to_not change(Audited::Audit, :count) end end + + describe "call audit multiple times" do + it "should update audit options" do + user = Models::ActiveRecord::UserOnlyName.create + user.update(password: "new password 1", name: "new name 1") + expect(user.audits.last.audited_changes.keys).to eq(%w[name]) + + user.class.class_eval do + audited only: :password + end + + user = Models::ActiveRecord::UserOnlyName.last + user.update(password: "new password 2", name: "new name 2") + expect(user.audits.last.audited_changes.keys).to eq(%w[password]) + end + end end diff --git a/spec/support/active_record/models.rb b/spec/support/active_record/models.rb index 34dde868..53b57863 100644 --- a/spec/support/active_record/models.rb +++ b/spec/support/active_record/models.rb @@ -36,6 +36,12 @@ class UserOnlyPassword < ::ActiveRecord::Base audited only: :password end + class UserOnlyName < ::ActiveRecord::Base + self.table_name = :users + attribute :non_column_attr if Rails.gem_version >= Gem::Version.new("5.1") + audited only: :name + end + class UserRedactedPassword < ::ActiveRecord::Base self.table_name = :users audited redacted: :password From d1c1b681a86e40238894db4615e4123b5e9e152e Mon Sep 17 00:00:00 2001 From: mohammednasser-32 Date: Tue, 22 Oct 2024 18:59:21 +0300 Subject: [PATCH 4/8] return if options did not change --- lib/audited/auditor.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/audited/auditor.rb b/lib/audited/auditor.rb index ae43fdb4..6b0eef15 100644 --- a/lib/audited/auditor.rb +++ b/lib/audited/auditor.rb @@ -108,6 +108,9 @@ def has_associated_audits def update_audited_options(new_options) previous_audit_options = self.audited_options set_audited_options(new_options) + + return if previous_audit_options == self.audited_options + self.reset_audited_columns log_message = "#{self.name} is already audited, audit options will be updated\n"\ From 882d1cb7742b7c779c5f6612578353e11bfa9f54 Mon Sep 17 00:00:00 2001 From: mohammednasser-32 Date: Tue, 22 Oct 2024 21:58:46 +0300 Subject: [PATCH 5/8] remove log message --- lib/audited/auditor.rb | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lib/audited/auditor.rb b/lib/audited/auditor.rb index 6b0eef15..a164f72a 100644 --- a/lib/audited/auditor.rb +++ b/lib/audited/auditor.rb @@ -108,15 +108,7 @@ def has_associated_audits def update_audited_options(new_options) previous_audit_options = self.audited_options set_audited_options(new_options) - - return if previous_audit_options == self.audited_options - self.reset_audited_columns - - log_message = "#{self.name} is already audited, audit options will be updated\n"\ - "before: #{previous_audit_options}\n"\ - "after: #{self.audited_options}" - Logger.new($stdout).info(log_message) end def set_audited_options(options) From f2c9d7dca247561d69997467f778ff9d68f90e92 Mon Sep 17 00:00:00 2001 From: Bran Liang Date: Wed, 30 Oct 2024 20:36:15 +0800 Subject: [PATCH 6/8] Relax gemspec to allow Rails 8.1 --- audited.gemspec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/audited.gemspec b/audited.gemspec index 8257f8b2..386cdf67 100644 --- a/audited.gemspec +++ b/audited.gemspec @@ -16,11 +16,11 @@ Gem::Specification.new do |gem| gem.required_ruby_version = ">= 2.3.0" - gem.add_dependency "activerecord", ">= 5.2", "< 8.1" - gem.add_dependency "activesupport", ">= 5.2", "< 8.1" + gem.add_dependency "activerecord", ">= 5.2", "< 8.2" + gem.add_dependency "activesupport", ">= 5.2", "< 8.2" gem.add_development_dependency "appraisal" - gem.add_development_dependency "rails", ">= 5.2", "< 8.1" + gem.add_development_dependency "rails", ">= 5.2", "< 8.2" gem.add_development_dependency "rspec-rails" gem.add_development_dependency "standard" gem.add_development_dependency "single_cov" From e64b4e383801665e57f9fba10022875d99726a1d Mon Sep 17 00:00:00 2001 From: Daniel Morrison Date: Fri, 8 Nov 2024 15:25:26 -0500 Subject: [PATCH 7/8] Add Rails 8.0 to build matrix --- .github/workflows/ci.yml | 17 +++++++++++++++++ Appraisals | 7 +++++++ gemfiles/rails80.gemfile | 10 ++++++++++ 3 files changed, 34 insertions(+) create mode 100644 gemfiles/rails80.gemfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7a208ce3..4f5bf267 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,7 @@ jobs: - rails70 - rails71 - rails72 + - rails80 - rails_main db: [POSTGRES, MYSQL, SQLITE] exclude: @@ -99,6 +100,22 @@ jobs: - appraisal: rails72 ruby: "3.0" + # Rails 8.0 supports Ruby 3.2+ + - appraisal: rails80 + ruby: "2.3" + - appraisal: rails80 + ruby: "2.4" + - appraisal: rails80 + ruby: "2.5" + - appraisal: rails80 + ruby: "2.6" + - appraisal: rails80 + ruby: "2.7" + - appraisal: rails80 + ruby: "3.0" + - appraisal: rails80 + ruby: "3.1" + # Rails main supports Ruby 3.2+ - appraisal: rails_main ruby: "2.3" diff --git a/Appraisals b/Appraisals index 17d2d81e..d23f3ef0 100644 --- a/Appraisals +++ b/Appraisals @@ -45,6 +45,13 @@ appraise "rails72" do gem "sqlite3", ">= 1.4" end +appraise "rails80" do + gem "rails", "~> 8.0.0" + gem "mysql2", "~> 0.5" + gem "pg", "~> 1.1" + gem "sqlite3", ">= 1.4" +end + appraise "rails_main" do gem "rails", github: "rails/rails", branch: "main" gem "mysql2", "~> 0.5" diff --git a/gemfiles/rails80.gemfile b/gemfiles/rails80.gemfile new file mode 100644 index 00000000..2942329f --- /dev/null +++ b/gemfiles/rails80.gemfile @@ -0,0 +1,10 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "rails", "~> 8.0.0" +gem "mysql2", "~> 0.5" +gem "pg", "~> 1.1" +gem "sqlite3", ">= 1.4" + +gemspec name: "audited", path: "../" From f935285c27151dff179ef274776f865302d40b8d Mon Sep 17 00:00:00 2001 From: Daniel Morrison Date: Fri, 8 Nov 2024 15:40:08 -0500 Subject: [PATCH 8/8] Bump version --- CHANGELOG.md | 6 ++++++ lib/audited/version.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d470758a..6395c14f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Audited ChangeLog +### 5.8.0 (2024-11-08) +- Allow calling audited multiple times - @mohammednasser-32 + [734](https://github.com/collectiveidea/audited/pull/734) +- Relax gemspec to allow Rails 8.1 - @BranLiang + [738](https://github.com/collectiveidea/audited/pull/738) + ### 5.7.0 (2024-08-13) - Support for Rails 7.2 and Ruby 3.3, and testing cleanups - @mattbrictson diff --git a/lib/audited/version.rb b/lib/audited/version.rb index 54c7f198..4e622287 100644 --- a/lib/audited/version.rb +++ b/lib/audited/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Audited - VERSION = "5.7.0" + VERSION = "5.8.0" end