module OrderQuery::ClassMethods

Top-level functions.

Public Instance Methods

seek(*spec) click to toggle source

@return [OrderQuery::Space]

# File lib/order_query.rb, line 34
def seek(*spec)
  # allow passing without a splat, as we can easily distinguish
  spec = spec.first if spec.length == 1 && spec.first.first.is_a?(Array)
  Space.new(all, spec)
end

Protected Instance Methods

order_query(name, *spec) click to toggle source
@param [Symbol] name
@param [Array<Array<Symbol,String>>] order_spec
@example
  class Post < ActiveRecord::Base
    include OrderQuery
    order_query :order_home,
               [:pinned, [true, false]]
               [:published_at, :desc],
               [:id, :desc]
  end

Scopes

.order_home
  #<ActiveRecord::Relation...>
.order_home_reverse
  #<ActiveRecord::Relation...>

Class methods

.order_home_at(post)
  #<OrderQuery::Point...>
.order_home_space
  #<OrderQuery::Space...>

Instance methods

.order_home(scope)
  #<OrderQuery::Point...>
# File lib/order_query.rb, line 70
    def order_query(name, *spec)
      define_singleton_method(:"#{name}_space") { seek(*spec) }
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        scope :#{name}, -> { #{name}_space.scope }
        scope :#{name}_reverse, -> { #{name}_space.scope_reverse }
        def self.#{name}_at(record)
          #{name}_space.at(record)
        end
        def #{name}(scope = self.class)
          scope.#{name}_space.at(self)
        end
      RUBY
    end