Skip to content

Commit

Permalink
Adds a specific ArgumentError when passing nil to dom_id. Which can h…
Browse files Browse the repository at this point in the history
…appen if you do something like pass a non-existent ivar dom_id(@something_non_existant)

Before this would raise: `NoMethodError: undefined method `to_key' for nil:NilClass`
After it raises `ArumentError: dom_id must be passed a record_or_class as the first parameter, you passed 'nil'`
  • Loading branch information
Austio authored and byroot committed Aug 22, 2023
1 parent 5e2d79b commit 6245c00
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions actionview/lib/action_view/record_identifier.rb
Expand Up @@ -91,6 +91,8 @@ def dom_class(record_or_class, prefix = nil)
# dom_id(Post.find(45), :edit) # => "edit_post_45"
# dom_id(Post, :custom) # => "custom_post"
def dom_id(record_or_class, prefix = nil)
raise ArgumentError, "dom_id must be passed a record_or_class as the first argument, you passed #{record_or_class.inspect}" unless record_or_class

record_id = record_key_for_dom_id(record_or_class) unless record_or_class.is_a?(Class)
if record_id
"#{dom_class(record_or_class, prefix)}#{JOIN}#{record_id}"
Expand Down
6 changes: 6 additions & 0 deletions actionview/test/template/record_identifier_test.rb
Expand Up @@ -83,6 +83,12 @@ def test_dom_id_with_prefix
assert_equal "edit_airplane_1", dom_id(@record, :edit)
end

def test_dom_id_raises_useful_error_when_passed_nil
assert_raises ArgumentError do
ActionView::RecordIdentifier.dom_id(nil)
end
end

def test_dom_class
assert_equal "airplane", dom_class(@record)
end
Expand Down

0 comments on commit 6245c00

Please sign in to comment.