-
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
- Loading branch information
Matthew McGarvey
committed
Sep 7, 2020
1 parent
fedd1be
commit 0acd88a
Showing
3 changed files
with
29 additions
and
16 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,27 @@ | ||
require "./errors" | ||
require "./queryable" | ||
|
||
module Avram::PrimaryKeyQueryable(T) | ||
abstract def id | ||
abstract def id(id_val) | ||
|
||
macro included | ||
include Avram::Queryable(T) | ||
|
||
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 ordered_query | ||
if query.ordered? | ||
query | ||
else | ||
id.asc_order.query | ||
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