Sha256: a9c172aa605301cfe379f8eeaf24e85f51b7cb02c2240465bb7ef8437568e7ee

Contents?: true

Size: 723 Bytes

Versions: 13

Compression:

Stored size: 723 Bytes

Contents

module Ohm
  # Dirt cheap ::first and ::last support.
  #
  # @example
  #
  #   class Post < Ohm::Model
  #     include Ohm::Boundaries
  #   end
  #
  #   post1 = Post.create
  #   post2 = Post.create
  #   post1 == Post.first
  #   # => true
  #
  #   post2 == Post.last
  #   # => true
  module Boundaries
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def first(opts = {})
        if opts.any?
          find(opts).first
        else
          all.first(opts)
        end
      end

      def last(opts = {})
        if opts.any?
          find(opts).first(:order => "DESC")
        else
          all.first(:order => "DESC")
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ohm-contrib-0.1.2 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.1.1 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.1.0 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.0.42 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.0.41 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.0.40 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.0.39 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.0.38 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.0.37 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.0.36 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.0.35 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.0.34 lib/ohm/contrib/boundaries.rb
ohm-contrib-0.0.33 lib/ohm/contrib/boundaries.rb