Module: Babik::QuerySet::Bounded

Included in:
AbstractBase
Defined in:
lib/babik/queryset/mixins/bounded.rb

Overview

Every QuerySet is bounded by its first and last items

Instance Method Summary collapse

Instance Method Details

#earliest(*order) ⇒ ActiveRecord::Base

Return the first element given some order

Parameters:

  • order (Array, String, Hash)

    ordering that will be applied to the QuerySet. See Sortable#order_by.

Returns:

  • (ActiveRecord::Base)

    First element according to the order.



12
13
14
# File 'lib/babik/queryset/mixins/bounded.rb', line 12

def earliest(*order)
  self.order_by(*order).first
end

#firstActiveRecord::Base

Return the first element of the QuerySet.

Returns:

  • (ActiveRecord::Base)

    First element of the QuerySet.



18
19
20
# File 'lib/babik/queryset/mixins/bounded.rb', line 18

def first
  self.all.first
end

#lastActiveRecord::Base

Return the last element of the QuerySet.

Returns:

  • (ActiveRecord::Base)

    Last element of the QuerySet.



24
25
26
# File 'lib/babik/queryset/mixins/bounded.rb', line 24

def last
  self.invert_order.all.first
end

#latest(*order) ⇒ ActiveRecord::Base

Return the last element given some order

Parameters:

  • order (Array, String, Hash)

    ordering that will be applied to the QuerySet. See Sortable#order_by.

Returns:

  • (ActiveRecord::Base)

    Last element according to the order.



32
33
34
# File 'lib/babik/queryset/mixins/bounded.rb', line 32

def latest(*order)
  self.order_by(*order).last
end