hyperstack-org / hyperstack-org/hyperstack

relationship/collection .first transfers entire scope to client

Aperta
#296 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug good first issue
Lingua principale
JavaScript
Stelle
538
Fork
41
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

`SomeModelWithLotsOfData.first` will transfer the entire table to the client then select the first item.

This patch appears to fix, and can be added as is to your ApplicationRecord file at the start.

```ruby
module ReactiveRecord
class Collection

# THIS IS THE KEY PART OF THE PATCH... DO THE SAME THING AS last FOR first
def first(n = nil)
if n
apply_scope(:__hyperstack_internal_scoped_first_n, n)
else
__hyperstack_internal_scoped_first
end
end
end

def method_missing(method, *args, &block)
if args.count == 1 && method.start_with?('find_by_')
find_by(method.sub(/^find_by_/, '') => args[0])
elsif [].respond_to? method
all.send(method, *args, &block)
elsif ScopeDescription.find(@target_klass, method)
apply_scope(method, *args)
elsif @target_klass.respond_to?(method) && ScopeDescription.find(@target_klass, "_#{method}")
# .first was being used here. change to [0] so we don't get into infinite recursion
apply_scope("_#{method}", *args)[0]
else
super
end
end
end if RUBY_ENGINE == 'opal'

module ActiveRecord
class Base
# define the scope and finder method used by the first method above

finder_method :__hyperstack_internal_scoped_first do
first
end

scope :__hyperstack_internal_scoped_first_n, ->(n) { first(n) }

# this method was using collection.first rather than collection[0]

def self.__hyperstack_internal_scoped_find_by(attrs)
collection = all.apply_scope(:___hyperstack_internal_scoped_find_by, attrs)
if !collection.collection
collection._find_by_initializer(self, attrs)
else
# use collection[0] rather than collection.first to avoid infinite recursion
collection[0]
end
end
end unless Base.respond_to? :__hyperstack_internal_scoped_first_n # only needed for the patch
end
```

Test case might be interesting to create....

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.