-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract PrimaryKeyQueryable from Queryable (#455)
* Extract PrimaryKeyQueryable from Queryable * Switch to composition of modules than an inheritance structure * Remove abstract defs from PrimaryKeyQueryable Co-authored-by: Jeremy Woertink <jeremywoertink@gmail.com>
- Loading branch information
1 parent
a8651a9
commit 9a2c69a
Showing
3 changed files
with
23 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require "./errors" | ||
|
||
module Avram::PrimaryKeyQueryable(T) | ||
macro included | ||
def self.find(id) | ||
new.find(id) | ||
end | ||
|
||
def find(id) | ||
id(id).limit(1).first? || raise Avram::RecordNotFoundError.new(model: @@table_name, id: id.to_s) | ||
end | ||
|
||
private def with_ordered_query : self | ||
if query.ordered? | ||
self | ||
else | ||
id.asc_order | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters