Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Remove find_by() due to unclear semantics
Fixes #145
- Loading branch information
Showing
2 changed files
with
9 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,24 @@ | ||
module NoBrainer::Criteria::Find | ||
extend ActiveSupport::Concern | ||
|
||
def find_by?(*args, &block) | ||
where(*args, &block).first | ||
end | ||
|
||
def find_by(*args, &block) | ||
find_by?(*args, &block).tap { |doc| raise_not_found(args) unless doc } | ||
raise "find_by() has unclear semantics. Please use where().first instead" | ||
end | ||
alias_method :find_by!, :find_by | ||
alias_method :find_by?, :find_by | ||
|
||
def find?(pk) | ||
without_ordering.find_by?(model.pk_name => pk) | ||
without_ordering.where(model.pk_name => pk).first | ||
end | ||
|
||
def find(pk) | ||
without_ordering.find_by(model.pk_name => pk) | ||
find?(pk).tap { |doc| raise_not_found(pk) unless doc } | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
end | ||
alias_method :find!, :find | ||
|
||
private | ||
|
||
def raise_not_found(args) | ||
raise NoBrainer::Error::DocumentNotFound, "#{model} #{args.inspect.gsub(/\[{(.*)}\]/, '\1')} not found" | ||
def raise_not_found(pk) | ||
raise NoBrainer::Error::DocumentNotFound, "#{model} :#{model.pk_name}=>#{pk.inspect} not found" | ||
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
I think we can directly write
find?(pk) || raise_not_found(pk)
. wdyt?