-
Notifications
You must be signed in to change notification settings - Fork 12
Mongoid criteria refactor #20
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
Changes from all commits
23e8892
d3bef23
ec7ca82
4d92ba0
ebb90d1
cb301ab
dc330a1
51fe14d
b9d5aca
5bb3d81
866df04
f1b996e
613bc98
b9f298c
aba2030
77e3e5e
243a2b4
f5e029d
2ced190
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
module Mongoid | ||
class Criteria | ||
module Scrollable | ||
def scroll(cursor = nil, &_block) | ||
raise_multiple_sort_fields_error if multiple_sort_fields? | ||
criteria = dup | ||
criteria.merge!(default_sort) if no_sort_option? | ||
cursor_options = build_cursor_options(criteria) | ||
cursor = cursor.is_a?(Mongoid::Scroll::Cursor) ? cursor : new_cursor(cursor, cursor_options) | ||
cursor_criteria = build_cursor_criteria(criteria, cursor) | ||
if block_given? | ||
cursor_criteria.order_by(_id: scroll_direction(criteria)).each do |record| | ||
yield record, cursor_from_record(record, cursor_options) | ||
end | ||
else | ||
cursor_criteria | ||
end | ||
end | ||
|
||
private | ||
|
||
def raise_multiple_sort_fields_error | ||
raise Mongoid::Scroll::Errors::MultipleSortFieldsError.new(sort: criteria.options.sort) | ||
end | ||
|
||
def multiple_sort_fields? | ||
options.sort && options.sort.keys.size != 1 | ||
end | ||
|
||
def no_sort_option? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe flip it to |
||
options.sort.blank? || options.sort.empty? | ||
end | ||
|
||
def default_sort | ||
asc(:_id) | ||
end | ||
|
||
def scroll_field(criteria) | ||
criteria.options.sort.keys.first | ||
end | ||
|
||
def scroll_direction(criteria) | ||
criteria.options.sort.values.first.to_i | ||
end | ||
|
||
def build_cursor_options(criteria) | ||
{ | ||
field_type: scroll_field_type(criteria), | ||
field_name: scroll_field(criteria), | ||
direction: scroll_direction(criteria) | ||
} | ||
end | ||
|
||
def new_cursor(cursor, cursor_options) | ||
Mongoid::Scroll::Cursor.new(cursor, cursor_options) | ||
end | ||
|
||
def build_cursor_criteria(criteria, cursor) | ||
cursor_criteria = criteria.dup | ||
cursor_criteria.selector = { '$and' => [criteria.selector, cursor.criteria] } | ||
cursor_criteria | ||
end | ||
|
||
def cursor_from_record(record, cursor_options) | ||
Mongoid::Scroll::Cursor.from_record(record, cursor_options) | ||
end | ||
|
||
def scroll_field_type(criteria) | ||
scroll_field = scroll_field(criteria) | ||
field = criteria.klass.fields[scroll_field.to_s] | ||
field.foreign_key? && field.object_id_field? ? bson_type : field.type | ||
end | ||
|
||
def bson_type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add this to mongoid-compatibility, maybe |
||
Mongoid::Compatibility::Version.mongoid3? ? Moped::BSON::ObjectId : BSON::ObjectId | ||
end | ||
end | ||
end | ||
end | ||
|
||
Mongoid::Criteria.send(:include, Mongoid::Criteria::Scrollable) |
This file was deleted.
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.
Should be a bang method I think, so
raise_multiple_sort_fields_error!
, for clarity that it fails loudly.