-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
ActiveResource::WhereClause
Closes [#408][] Introduce a simple chainable `WhereClause` class inspired by [Active Record][]. All methods (including those that integrate with [Enumerable][]) are delegated to `WhereClause#all`, which itself delegates to `Base.find`. By merging parameters through `.where`-chaining, this commit supports deferred fetching: ```ruby people = Person.where(id: 2).where(name: "david") # => GET /people.json?id=2&name=david people = Person.where(id: 2).all(params: { name: "david" }) # => GET /people.json?id=2&name=david people = Person.all(from: "/records.json").where(id: 2) # => GET /records.json?id=2 ``` [#408]: #408 [Active Record]: https://github.com/rails/rails/blob/main/activerecord/lib/active_record/relation/where_clause.rb [Enumerable]: https://ruby-doc.org/3.4.1/Enumerable.html
- Loading branch information
1 parent
9c8a2ee
commit 6acb99d
Showing
4 changed files
with
111 additions
and
2 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
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,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveResource | ||
class WhereClause < BasicObject | ||
delegate :find, to: :@resource_class | ||
delegate_missing_to :resources | ||
|
||
def initialize(resource_class, options = {}) | ||
@resource_class = resource_class | ||
@options = options | ||
@resources = nil | ||
@loaded = false | ||
end | ||
|
||
def where(clauses = {}) | ||
all(params: clauses) | ||
end | ||
|
||
def all(options = {}) | ||
WhereClause.new(@resource_class, @options.deep_merge(options)) | ||
end | ||
|
||
def load | ||
unless @loaded | ||
@resources = find(:all, @options) | ||
@loaded = true | ||
end | ||
|
||
self | ||
end | ||
|
||
def reload | ||
reset | ||
load | ||
end | ||
|
||
private | ||
def resources | ||
load | ||
@resources | ||
end | ||
|
||
def reset | ||
@resources = nil | ||
@loaded = false | ||
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