Skip to content

Commit

Permalink
Ignore persisted? method definitions
Browse files Browse the repository at this point in the history
`ActiveModel::Conversion` defines `to_param` so models can be serialized
when passing them through the URL.

Under the hood, `to_param` calls `persisted?` which will need to be defined
on the model.

```rb
def to_param
  (persisted? && key = to_key) ? key.join('-') : nil
end
```

We should ignore `persisted?` definitions so they do not appear dead.

Signed-off-by: Alexandre Terrasa <[email protected]>
  • Loading branch information
Morriar committed Jul 10, 2024
1 parent 34a18ab commit 8c5be83
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/spoom/deadcode/plugins/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ActiveModel < Base
extend T::Sig

ignore_classes_inheriting_from("ActiveModel::EachValidator")
ignore_methods_named("validate_each")
ignore_methods_named("validate_each", "persisted?")

sig { override.params(send: Send).void }
def on_send(send)
Expand Down
11 changes: 11 additions & 0 deletions test/spoom/deadcode/plugins/active_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ def another_method; end
refute_ignored(index, "another_method")
end

def test_ignore_persisted
@project.write!("app/model/validators/my_validator.rb", <<~RB)
class MyModel
def persisted?; end
end
RB

index = index_with_plugins
assert_ignored(index, "persisted?")
end

def test_dead_validation_callbacks
@project.write!("app/models/my_model.rb", <<~RB)
class MyModel
Expand Down

0 comments on commit 8c5be83

Please sign in to comment.