ActiveRecord plugin that can find next / previous item(s) in 1 query. Super-alpha, and the query is not yet fully optimized.
No gem released yet, but you can install from the repository:
gem 'search_in_order', git: 'https://github.com/glebm/search_in_order'
class Issue < ActiveRecord::Base
include SearchInOrder
search_in_order :display_order, [
[:priority, %w(high medium low)],
[:valid_votes_count, :desc, sql: '(votes - suspicious_votes)'],
[:updated_at, :desc],
[:id, :desc]
]
def valid_votes_count
votes - suspicious_votes
end
end
scope = Issue.open.search(params[:search])
ord = Issue.find(31).display_order(scope) # scope default: Issue.all
ord.prev_item #=> Issue<...>
ord.next_item #=> Issue<...>
ord.items_after #=> ActiveRecord::Relation<...>
ord.items_before #=> ActiveRecord::Relation<...>
ord.position #=> 5
ord.ordered_scope #=> ActiveRecord::Relation<...>
This project uses MIT-LICENSE.