Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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][])
Expand Down
7 changes: 7 additions & 0 deletions gemfiles/rails70.gemfile
Original file line number Diff line number Diff line change
@@ -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: ".."
2 changes: 1 addition & 1 deletion gemfiles/rails7.gemfile → gemfiles/rails72.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source "https://rubygems.org"

gem "rails", "~> 7.0"
gem "rails", "~> 7.2.0"

gemspec path: ".."
10 changes: 7 additions & 3 deletions lib/callback_hell/analyzers/callback_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/internal/app/models/foo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions spec/modelscope/integration/attribute_generated_spec.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
14 changes: 10 additions & 4 deletions spec/modelscope/integration/empty_activerecord_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }

Expand All @@ -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