diff --git a/.github/workflows/rspec.yml b/.github/workflows/rspec.yml index 5b1279b..ee9f677 100644 --- a/.github/workflows/rspec.yml +++ b/.github/workflows/rspec.yml @@ -21,8 +21,10 @@ jobs: gemfile: ["gemfiles/rails8.gemfile"] include: - ruby: "3.2" - gemfile: "gemfiles/rails7.gemfile" - - ruby: "3.3" + gemfile: "gemfiles/rails70.gemfile" + - ruby: "3.2" + gemfile: "gemfiles/rails72.gemfile" + - ruby: "3.4" gemfile: "gemfiles/rails8.gemfile" - ruby: "3.4" gemfile: "gemfiles/railsmaster.gemfile" diff --git a/CHANGELOG.md b/CHANGELOG.md index 30589ab..894351e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change log +## Unreleased + +- Proper support for Rails 7.0 and its lack of `normalizes` (https://github.com/evilmartians/callback_hell/issues/1) ([@yaroslav][]) + ## 0.2.0 - Initial public release. ([@yaroslav][]) diff --git a/gemfiles/rails70.gemfile b/gemfiles/rails70.gemfile new file mode 100644 index 0000000..b36c640 --- /dev/null +++ b/gemfiles/rails70.gemfile @@ -0,0 +1,7 @@ +source "https://rubygems.org" + +gem "rails", "~> 7.0.0" +gem "concurrent-ruby", "1.3.4" +gem "sqlite3", "~> 1.4.0" + +gemspec path: ".." diff --git a/gemfiles/rails7.gemfile b/gemfiles/rails72.gemfile similarity index 68% rename from gemfiles/rails7.gemfile rename to gemfiles/rails72.gemfile index 8aebf01..fc6f6ea 100644 --- a/gemfiles/rails7.gemfile +++ b/gemfiles/rails72.gemfile @@ -1,5 +1,5 @@ source "https://rubygems.org" -gem "rails", "~> 7.0" +gem "rails", "~> 7.2.0" gemspec path: ".." diff --git a/lib/callback_hell/analyzers/callback_analyzer.rb b/lib/callback_hell/analyzers/callback_analyzer.rb index bd15b22..2043792 100644 --- a/lib/callback_hell/analyzers/callback_analyzer.rb +++ b/lib/callback_hell/analyzers/callback_analyzer.rb @@ -10,9 +10,13 @@ class CallbackAnalyzer ].freeze RAILS_ATTRIBUTE_OWNERS = [ - defined?(ActiveRecord::Normalization) ? ActiveRecord::Normalization : ActiveModel::Attributes::Normalization, - ActiveRecord::Encryption::EncryptableRecord - ].freeze + defined?(ActiveRecord::Normalization) && + ActiveRecord::Normalization, + defined?(ActiveModel::Attributes::Normalization) && + ActiveModel::Attributes::Normalization, + defined?(ActiveRecord::Encryption::EncryptableRecord) && + ActiveRecord::Encryption::EncryptableRecord + ].compact.freeze def initialize(callback, model, defining_class) @callback = callback diff --git a/spec/internal/app/models/foo.rb b/spec/internal/app/models/foo.rb index 94885ac..0b9c2d2 100644 --- a/spec/internal/app/models/foo.rb +++ b/spec/internal/app/models/foo.rb @@ -14,7 +14,7 @@ class Foo < ApplicationRecord after_create :noop around_create :noop, if: :createable? - normalizes :name, with: -> { _1.strip } + normalizes :name, with: -> { _1.strip } if respond_to?(:normalizes) def noop = true diff --git a/spec/modelscope/integration/attribute_generated_spec.rb b/spec/modelscope/integration/attribute_generated_spec.rb index 7a74abf..3f6d655 100644 --- a/spec/modelscope/integration/attribute_generated_spec.rb +++ b/spec/modelscope/integration/attribute_generated_spec.rb @@ -1,10 +1,14 @@ # frozen_string_literal: true RSpec.describe CallbackHell, "with attribute-generated callbacks and validations" do + def normalization_supported? + ActiveRecord::Base.respond_to?(:normalizes) + end subject(:foo) { CallbackHell::Collector.new(Foo, mode: :full).collect } subject(:bar) { CallbackHell::Collector.new(Bar, mode: :full).collect } it "correctly marks the callbacks generated by attributes" do + skip "Normalization not supported in this Rails version" unless normalization_supported? expect(foo).to have_callback( callback_name: :before_validate, method_name: :cant_modify_encrypted_attributes_when_frozen, diff --git a/spec/modelscope/integration/empty_activerecord_spec.rb b/spec/modelscope/integration/empty_activerecord_spec.rb index 16eaf95..343e135 100644 --- a/spec/modelscope/integration/empty_activerecord_spec.rb +++ b/spec/modelscope/integration/empty_activerecord_spec.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true RSpec.describe CallbackHell, "with an empty ActiveRecord" do + def normalization_supported? + ActiveRecord::Base.respond_to?(:normalizes) + end + let(:options) { {} } subject(:ar) { CallbackHell::Collector.new(ApplicationRecord, **options).collect } @@ -18,10 +22,12 @@ method_name: :cant_modify_encrypted_attributes_when_frozen, origin: :rails, inherited: false ) - expect(ar).to have_callback( - method_name: :normalize_changed_in_place_attributes, - origin: :rails, inherited: false - ) + if normalization_supported? + expect(ar).to have_callback( + method_name: :normalize_changed_in_place_attributes, + origin: :rails, inherited: false + ) + end end end end