-
Notifications
You must be signed in to change notification settings - Fork 802
Losing sort order when decorate records with draper #169
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
Comments
Hey, so let's stick with the first problem -- the response.records.map
=> #<Enumerator: ...> I'm all for Draper support! |
Sorry, forgot that I'm using context "Mongoid integration with kaminari" do
class ::MongoidArticleKaminari < ::MongoidArticle
include ::Kaminari::ConfigurationMethods
end
setup do
Elasticsearch::Model::Adapter.register \
Elasticsearch::Model::Adapter::Mongoid,
lambda { |klass| !!defined?(::Mongoid::Document) && klass.ancestors.include?(::Mongoid::Document) }
MongoidArticleKaminari.__elasticsearch__.create_index! force: true
MongoidArticleKaminari.delete_all
10.times do
MongoidArticleKaminari.create! title: "test "*rand(1..5)
end
MongoidArticleKaminari.__elasticsearch__.refresh_index!
MongoidArticleKaminari.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
end
should "not lose sort order on map" do
records = MongoidArticleKaminari.search(query: {match: {title: {query: 'test'}}}).page(1).limit(100).records
assert_equal records.map(&:id) , records.to_a.map(&:id)
end
end Output:
|
This is weird, do you have any idea why |
In my opinion that happening because I could be wrong, but on debugging he just do not go in. |
And I remind, this problem occurs only when kaminari |
I'm facing the same issue, on the same setup (Mongoid + Kaminari). Anyone though of any workaround yet? Edit: found a workaround: I'm calling |
Still don't get it -- map is implemented via |
Hi. I ran into this problem while I was working on to_a issue with mongoid (#99) a while back. Here's what you have to do: class ObjectsDecorator < PaginationDecorator
def decorated_collection
@decorated_collection ||= object.to_a.map{|item| decorate_item(item)}
end
end
#And PaginationDecorator is something you should be familiar with:
class PaginationDecorator < Draper::CollectionDecorator
delegate :current_page, :prev_page, :next_page, :total_pages, :limit_value
end |
I dug in a little more and I don't think map is implemented via each (could you point me out where?). |
@zoer So, I was digging into this... The problem appears to be that As soon as I add this to the Mongoid adapter: def map(&block)
records.to_a.map(&block)
end your tests pass. Of course, that is the same as adding this: def each(&block)
records.to_a.each(&block)
end So, this is probably what we have to do... @dwkoogt In Ruby, class MyCollection
include Enumerable
def each(&block)
[1, 2, 3].each { |i| yield i }
end
end
p MyCollection.new.map { |i| i * 100 }
# => [100, 200, 300] |
@zoer Any new info, feedback here? |
Yes, I think it's something that should be. I already use this workaround in my code. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Seems this happened because
map
method is not delegated to the records:https://github.com/elasticsearch/elasticsearch-rails/blob/v0.1.4/elasticsearch-model/lib/elasticsearch/model/response/records.rb#L13
But
draper
usemap
for decorate collections:https://github.com/drapergem/draper/blob/v1.3.1/lib/draper/collection_decorator.rb#L39
I just fixed this by override
decorated_collection
This is not an issue, but I think you should know about this.
The text was updated successfully, but these errors were encountered: