-
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
Conversation
This is helpful so that `where` method calls can be chained
Simplified delegation of Array, added docs
@@ -150,7 +150,7 @@ def defines_has_many_finder_method(reflection) | |||
elsif !new_record? | |||
instance_variable_set(ivar_name, reflection.klass.find(:all, params: { "#{self.class.element_name}_id": self.id })) | |||
else | |||
instance_variable_set(ivar_name, self.class.collection_parser.new) | |||
instance_variable_set(ivar_name, reflection.klass.find(:all)) |
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.
def instantiate_record(record, prefix_options = {}) | ||
new(record, true).tap do |resource| | ||
resource.prefix_options = prefix_options | ||
end | ||
end |
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.
Changed from private to public
# 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Network call now gets made within the ActiveResource::Collection
class, so this is simply initializing it.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Made public
def test_collection_respond_to_collect! | ||
assert @collection.respond_to?(:collect!) | ||
end | ||
|
||
def test_collection_respond_to_map! | ||
assert @collection.respond_to?(:map!) | ||
end |
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.
these methods were removed
def test_collect_bang_modifies_elements | ||
elements = %w(a b c) | ||
@collection.elements = elements | ||
results = @collection.collect! { |i| i + "!" } | ||
assert_equal results.to_a, elements.collect! { |i| i + "!" } | ||
end | ||
|
||
def test_collect_bang_returns_collection | ||
@collection.elements = %w(a) | ||
results = @collection.collect! { |i| i + "!" } | ||
assert_kind_of ActiveResource::Collection, results | ||
end |
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.
these methods were removed
Similar to Rails ORM, check if request was made and re-use that instead of making another http call.
From my point of view Active Resource is a legacy gem. Which means I'm willing to fix simple bugs and whatever is necessary to keep it working on newer rubies and newer Rails, but not willing to do any significant change. |
Based on @byroot comment this won't be merged in, so closing the PR |
Summary
This is for #408 .
It is a breaking change
ActiveResource
allows you to chainwhere
methods such asThis improves it so that network calls are only performed when resources are accessed.
With this PR