Sha256: 38e0c164728b73dfe100431d1675891f5ad57da64478f47b01761f080fe4c5b4
Contents?: true
Size: 1.47 KB
Versions: 5
Compression:
Stored size: 1.47 KB
Contents
require 'active_support/concern' module ActsAsPreviousNext extend ActiveSupport::Concern included do def self.acts_as_previous_next(options = {}) configuration = { column: 'id', with_cancan: false } configuration.update(options) if options.is_a?(Hash) if options.is_a? Symbol column = options.to_s with_cancan = false elsif options.is_a? Hash column = options[:column] || "id" with_cancan = options[:with_cancan] end class_eval <<-EOF if with_cancan def next(ability) self.class.accessible_by(ability).where("#{column} > ?", self.send('#{column}')).order("#{column}").first || self.class.accessible_by(ability).order("#{column}").first end def previous(ability) self.class.accessible_by(ability).where("#{column} < ?", self.send('#{column}')).order("#{column} DESC").first || self.class.accessible_by(ability).order("#{column} DESC").first end else def next self.class.where("#{column} > ?", self.send('#{column}')).order("#{column}").first || self.class.order("#{column}").first end def previous self.class.where("#{column} < ?", self.send('#{column}')).order("#{column} DESC").first || self.class.order("#{column} DESC").first end end EOF end end end ActiveRecord::Base.send :include, ActsAsPreviousNext
Version data entries
5 entries across 5 versions & 1 rubygems