Created
October 15, 2012 07:43
-
-
Save anonymous/3891253 to your computer and use it in GitHub Desktop.
Testcode for embedded pagination
This file contains 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
class EmbeddedSomething | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
field :name, type: String | |
embedded_in :something | |
end | |
class Something | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
field :name, type: String | |
embeds_many :embedded_somethings | |
def self.create_objects | |
10.times do |n| | |
something = Something.create(name: "test object #{n}") | |
10.times do |x| | |
EmbeddedSomething.create(something: something, name: "embedded #{n}-#{x}") | |
end | |
end | |
end | |
def self.test | |
# setup | |
Something.destroy_all | |
Something.create_objects | |
# pagination working fine with relation | |
Something.pagination_test(1) | |
Something.pagination_test(2) | |
Something.pagination_test(3) | |
# pagination of embedded objects working fine | |
Something.embedded_pagination_test(1) | |
Something.embedded_pagination_test(2) | |
# pagination of embedded objects breaks if page is empty: | |
Something.embedded_pagination_test(3) | |
# cleanup | |
Something.destroy_all | |
end | |
def self.pagination_test(page = 1) | |
limit = 6 | |
offset = (page - 1) * limit | |
puts "test pagination with page #{page} and limit #{limit}" | |
scope = Something.limit(limit).offset(offset).map(&:name) | |
puts scope.to_a.inspect | |
end | |
def self.embedded_pagination_test(page = 1) | |
limit = 6 | |
offset = (page - 1) * limit | |
puts "test embedded pagination with page #{page} and limit #{limit}" | |
something = Something.first | |
scope = something.embedded_somethings.limit(limit).offset(offset).map(&:name) | |
puts scope.to_a.inspect | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment