Sha256: d270133ff57289214c381114c8b852b997cced4d746e18cb02732de3b4606e15

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

# In keeping with Mongoid standards, this module must be included into your model class, i.e.
#
#   include Mongoid::ByStar
#
module Mongoid
  module ByStar
    extend ActiveSupport::Concern

    module ClassMethods
      include ::ByStar::Base

      def between_times_query(start, finish, options={})
        start_field = by_star_start_field(options)
        end_field = by_star_end_field(options)

        scope = by_star_scope(options)
        scope = if options[:strict] || start_field == end_field
          scope.gte(start_field => start).lte(end_field => finish)
        else
          scope.gt(end_field => start).lt(start_field => finish)
        end
        scope = scope.order_by(field => options[:order]) if options[:order]
        scope
      end

      def by_star_end_field_with_mongoid(options = {})
        database_field_name by_star_end_field_without_mongoid(options)
      end
      alias_method_chain :by_star_end_field, :mongoid

      def by_star_start_field_with_mongoid(options = {})
        database_field_name by_star_start_field_without_mongoid(options)
      end
      alias_method_chain :by_star_start_field, :mongoid

      protected

      def by_star_default_field
        :created_at
      end

      def before_query(time, options={})
        field = by_star_start_field(options)
        by_star_scope(options).lte(field => time)
      end

      def after_query(time, options={})
        field = by_star_start_field(options)
        by_star_scope(options).gte(field => time)
      end
    end

    def previous(options={})
      field = self.class.by_star_start_field
      self.class.by_star_scope(options).lt(field => self.send(field)).desc(field).first
    end

    def next(options={})
      field = self.class.by_star_start_field
      self.class.by_star_scope(options).gt(field => self.send(field)).asc(field).first
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
by_star-2.2.0 lib/by_star/orm/mongoid/by_star.rb