[ISSUE-408] Lazy Network Calls on Collections#409
Closed
jlurena wants to merge 5 commits intorails:mainfrom
jlurena:ISSUE-408-lazy-network-calls
Closed
[ISSUE-408] Lazy Network Calls on Collections#409jlurena wants to merge 5 commits intorails:mainfrom jlurena:ISSUE-408-lazy-network-calls
jlurena wants to merge 5 commits intorails:mainfrom
jlurena:ISSUE-408-lazy-network-calls
Conversation
This is helpful so that `where` method calls can be chained
Simplified delegation of Array, added docs
jlurena
commented
Nov 23, 2024
| 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)) |
Author
There was a problem hiding this comment.
Because this is now lazy, no network call is actually made.
jlurena
commented
Nov 23, 2024
Comment on lines
+800
to
+804
| def instantiate_record(record, prefix_options = {}) | ||
| new(record, true).tap do |resource| | ||
| resource.prefix_options = prefix_options | ||
| end | ||
| end |
jlurena
commented
Nov 23, 2024
| # Swallowing ResourceNotFound exceptions and return nil - as per | ||
| # ActiveRecord. | ||
| nil | ||
| collection_parser.new([], options[:from]).tap do |parser| |
Author
There was a problem hiding this comment.
Network call now gets made within the ActiveResource::Collection class, so this is simply initializing it.
jlurena
commented
Nov 23, 2024
| end.collect! { |record| instantiate_record(record, prefix_options) } | ||
| end | ||
|
|
||
| def instantiate_record(record, prefix_options = {}) |
jlurena
commented
Nov 23, 2024
Comment on lines
-12
to
-18
| def test_collection_respond_to_collect! | ||
| assert @collection.respond_to?(:collect!) | ||
| end | ||
|
|
||
| def test_collection_respond_to_map! | ||
| assert @collection.respond_to?(:map!) | ||
| end |
jlurena
commented
Nov 23, 2024
Comment on lines
-36
to
-47
| 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 |
Similar to Rails ORM, check if request was made and re-use that instead of making another http call.
This was referenced Nov 29, 2024
Author
Member
|
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. |
Author
|
Based on @byroot comment this won't be merged in, so closing the PR |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This is for #408 .
It is a breaking change
ActiveResourceallows you to chainwheremethods such asThis improves it so that network calls are only performed when resources are accessed.
With this PR