Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore callbacks if not specifed on the model #679

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ class User < ActiveRecord::Base
end
```

You can ignore the default callbacks globally unless the callback action is specified in your model using the `:on` option. To configure default callback exclusion, put the following in an initializer file (`config/initializers/audited.rb`):

```ruby
Audited.ignored_default_callbacks = [:create, :update] # ignore callbacks create and delete
danielmorrison marked this conversation as resolved.
Show resolved Hide resolved
```

### Comments

You can attach comments to each audit using an `audit_comment` attribute on your model.
Expand Down
2 changes: 2 additions & 0 deletions lib/audited.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class << self
:auditing_enabled,
:current_user_method,
:ignored_attributes,
:ignored_default_callbacks,
:max_audits,
:store_synthesized_enums
attr_writer :audit_class
Expand All @@ -34,6 +35,7 @@ def config
end

@ignored_attributes = %w[lock_version created_at updated_at created_on updated_on]
@ignored_default_callbacks = []

@current_user_method = :current_user
@auditing_enabled = true
Expand Down
2 changes: 1 addition & 1 deletion lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def default_ignored_attributes

def normalize_audited_options
audited_options[:on] = Array.wrap(audited_options[:on])
audited_options[:on] = [:create, :update, :touch, :destroy] if audited_options[:on].empty?
audited_options[:on] = ([:create, :update, :touch, :destroy] - Audited.ignored_default_callbacks) if audited_options[:on].empty?
audited_options[:only] = Array.wrap(audited_options[:only]).map(&:to_s)
audited_options[:except] = Array.wrap(audited_options[:except]).map(&:to_s)
max_audits = audited_options[:max_audits] || Audited.max_audits
Expand Down
21 changes: 21 additions & 0 deletions spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,27 @@ def non_column_attr=(val)
expect(user.audits.last.audited_changes["password"]).to eq(["My", "Custom", "Value", 7])
end

context "when ignored_default_callbacks is set" do
before { Audited.ignored_default_callbacks = [:create] }
after { Audited.ignored_default_callbacks = [] }

it "should remove create callback" do
class DefaultCallback < ::ActiveRecord::Base
audited
end

expect(DefaultCallback.audited_options[:on]).to eq([:update, :touch, :destroy])
end

it "should keep create callback if specified" do
class CallbacksSpecified < ::ActiveRecord::Base
audited on: [:create, :update, :destroy]
end

expect(CallbacksSpecified.audited_options[:on]).to eq([:create, :update, :destroy])
end
end

if ::ActiveRecord::VERSION::MAJOR >= 7
it "should filter encrypted attributes" do
user = Models::ActiveRecord::UserWithEncryptedPassword.create(password: "password")
Expand Down