Repository URL to install this package:
Version:
0.1.0 ▾
|
require "neoteric-navigator/version"
module Neoteric
module Navigator
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def navigate(options = {})
@navigator_options = options
end
def navigator_scope_option
(@navigator_options[:scope] || :unscoped)
end
def navigator_field
@navigator_options[:field]
end
def navigator_order_clause
@navigator_options[:order] || navigator_field
end
end
def next
return @next unless @next.nil?
@next = if self == last_next_item
nil
else
future_posts.reject { |p| p == self }.last
end
end
def prev
return @prev unless @prev.nil?
@prev = if self == last_prev_item
nil
else
past_posts.reject { |p| p == self }.first
end
end
private
def last_next_item
future_posts.first
end
def last_prev_item
past_posts.last
end
def future_posts
@future_posts ||= posts('>=')
end
def past_posts
@past_posts ||= posts('<=')
end
def posts(operator)
self.class.send(navigator_scope_option)
.order(navigator_order_clause)
.where("#{navigator_field.to_s} #{operator} ?",
send(navigator_field))
end
def navigator_field
self.class.navigator_field
end
def navigator_scope_option
self.class.navigator_scope_option
end
def navigator_order_clause
self.class.navigator_order_clause
end
end
end