Skip to content

Commit

Permalink
Only define #reload when Spec module is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
stufro committed Oct 17, 2023
1 parent 72b01d4 commit b1d2e5d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ post = Post.first! # raises when no records exist

Returns the record with the attributes reloaded from the database.

**Note:** this method is only defined when the `Spec` module is present.

```
post = Post.create(name: "Granite Rocks!", body: "Check this out.")
# record gets updated by another process
Expand Down
18 changes: 14 additions & 4 deletions src/granite/querying.cr
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,18 @@ module Granite::Querying
end
end

# reload returns the record with the attributes reloaded from the database
def reload
self.class.find!(primary_key_value)
end
{% if parse_type("Spec").resolve? %}
# Returns the record with the attributes reloaded from the database.
#
# **Note:** this method is only defined when the `Spec` module is present.
#
# ```
# post = Post.create(name: "Granite Rocks!", body: "Check this out.")
# # record gets updated by another process
# post.reload # performs another find to fetch the record again
# ```
def reload
self.class.find!(primary_key_value)
end
{% end %}
end

0 comments on commit b1d2e5d

Please sign in to comment.