Sha256: 117349702c7274cfcd4a0c7417bba224dbcf73515e72fb472f4c5dd3f21e37b3

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module ByStar
  module ActiveRecord
    extend ActiveSupport::Concern

    module ClassMethods
      include ::ByStar::Base

      # Returns all records between a given start and finish time.
      #
      # Currently only supports Time objects.
      def between_times_query(start, finish, options={})
        scope = if options[:strict] || by_star_start_field == by_star_end_field
          where("#{by_star_start_field} >= ? AND #{by_star_end_field} <= ?", start, finish)
        else
          where("#{by_star_end_field} > ? AND #{by_star_start_field} < ?", start, finish)
        end
        scope = scope.order(options[:order]) if options[:order]
        scope
      end

      alias_method :between, :between_times

      protected

      def by_star_default_field
        "#{self.table_name}.created_at"
      end

      def before_query(time, options={})
        field = by_star_start_field
        where("#{field} <= ?", time)
      end

      def after_query(time, options={})
        field = by_star_start_field
        where("#{field} >= ?", time)
      end
    end

    def previous(options={})
      field = self.class.by_star_start_field
      value = self.send(field.split(".").last)
      self.class.where("#{field} < ?", value).reorder("#{field} DESC").first
    end

    def next(options={})
      field = self.class.by_star_start_field
      value = self.send(field.split(".").last)
      self.class.where("#{field} > ?", value).reorder("#{field} ASC").first
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
by_star-2.2.0.rc1 lib/by_star/orm/active_record/by_star.rb