Sha256: c2de9da514bfc0a1cae655e9d42c9ae837c41c7db405250aa2eca380dc3c9aac

Contents?: true

Size: 936 Bytes

Versions: 1

Compression:

Stored size: 936 Bytes

Contents

require 'active_support/concern'

module BaseExtension
  extend ActiveSupport::Concern

  def last?(attribute = "id")
    self == self.class.order("#{attribute} ASC").last
  end

  def first?(attribute = "id")
    self == self.class.order("#{attribute} ASC").first
  end

  def next(attribute = "id")
    self.class.where("#{attribute} > #{self.send(attribute)}").order("#{attribute} ASC").limit(1).first
  end

  def previous(attribute = "id")
    self.class.where("#{attribute} < #{self.send(attribute)}").order("#{attribute} DESC").limit(1).first
  end

  def all_after(attribute = "id")
    self.class.where("#{attribute} > #{self.send(attribute)}").order("#{attribute} ASC")
  end

  def all_before(attribute = "id")
    self.class.where("#{attribute} < #{self.send(attribute)}").order("#{attribute} ASC")
  end

  def all_without
    self.class.where.not(id: self.id)
  end

  ActiveRecord::Base.send(:include, BaseExtension)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
record_neighbors-0.0.2.1 lib/record_neighbors/base_extension.rb