-
Notifications
You must be signed in to change notification settings - Fork 359
[ISSUE-408] Lazy Network Calls on Collections #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0a0b151
b2836d9
ea9a994
b570702
c4347c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -776,6 +776,33 @@ def format_extension | |
include_format_in_path ? ".#{format.extension}" : "" | ||
end | ||
|
||
# Instantiates a new record with the given options. | ||
# | ||
# This method creates a new instance of the class with the provided record and sets its `prefix_options` attribute. | ||
# | ||
# ==== Options | ||
# | ||
# +record+ [Object] The record to be instantiated. | ||
# +prefix_options+ [Hash, nil] Optional hash containing prefix options for the resource. Defaults to an empty hash. | ||
# | ||
# ==== Returns | ||
# | ||
# [Object] The newly instantiated resource. | ||
# | ||
# ==== Examples | ||
# | ||
# MyResource.instantiate_record(record) | ||
# # Creates a new MyResource instance with default prefix options. | ||
# | ||
# MyResource.instantiate_record(record, { prefix: "admin" }) | ||
# # Creates a new MyResource instance with prefix set to "admin". | ||
# | ||
def instantiate_record(record, prefix_options = {}) | ||
new(record, true).tap do |resource| | ||
resource.prefix_options = prefix_options | ||
end | ||
end | ||
Comment on lines
+800
to
+804
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed from private to public |
||
|
||
# Gets the element path for the given ID in +id+. If the +query_options+ parameter is omitted, Rails | ||
# will split from the \prefix options. | ||
# | ||
|
@@ -1096,23 +1123,12 @@ def find_every(options) | |
params = options[:params] | ||
prefix_options, query_options = split_options(params) | ||
|
||
response = | ||
case from = options[:from] | ||
when Symbol | ||
get(from, params) | ||
when String | ||
path = "#{from}#{query_string(query_options)}" | ||
format.decode(connection.get(path, headers).body) | ||
else | ||
path = collection_path(prefix_options, query_options) | ||
format.decode(connection.get(path, headers).body) | ||
end | ||
|
||
instantiate_collection(response || [], query_options, prefix_options) | ||
rescue ActiveResource::ResourceNotFound | ||
# Swallowing ResourceNotFound exceptions and return nil - as per | ||
# ActiveRecord. | ||
nil | ||
collection_parser.new([], options[:from]).tap do |parser| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Network call now gets made within the |
||
parser.resource_class = self | ||
parser.query_params = query_options | ||
parser.prefix_options = prefix_options | ||
parser.path_params = params | ||
end | ||
end | ||
|
||
# Find a single resource from a one-off URL | ||
|
@@ -1140,13 +1156,6 @@ def instantiate_collection(collection, original_params = {}, prefix_options = {} | |
end.collect! { |record| instantiate_record(record, prefix_options) } | ||
end | ||
|
||
def instantiate_record(record, prefix_options = {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made public |
||
new(record, true).tap do |resource| | ||
resource.prefix_options = prefix_options | ||
end | ||
end | ||
|
||
|
||
# Accepts a URI and creates the site URI from that. | ||
def create_site_uri_from(site) | ||
site.is_a?(URI) ? site.dup : URI.parse(site) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this is now lazy, no network call is actually made.