Skip to content

Commit

Permalink
Merge pull request #679 from JDrizzy/ignore-callbacks
Browse files Browse the repository at this point in the history
Ignore callbacks if not specifed on the model
  • Loading branch information
danielmorrison authored Jan 11, 2024
2 parents 4606723 + a609166 commit 00a1d82
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,24 @@ end

### Specifying callbacks

By default, a new audit is created for any Create, Update or Destroy action. You can, however, limit the actions audited.
By default, a new audit is created for any Create, Update, Touch (Rails 6+) or Destroy action. You can, however, limit the actions audited.

```ruby
class User < ActiveRecord::Base
# All fields and actions
# audited

# Single field, only audit Update and Destroy (not Create)
# Single field, only audit Update and Destroy (not Create or Touch)
# audited only: :name, on: [:update, :destroy]
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 update
```

### 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 @@ -35,6 +36,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 @@ -498,7 +498,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

0 comments on commit 00a1d82

Please sign in to comment.