Sha256: 6c714de4b145bd422678c1f45e2d038b002a6e5eab593da87945c8d57380f535

Contents?: true

Size: 846 Bytes

Versions: 4

Compression:

Stored size: 846 Bytes

Contents

# frozen_string_literal: true

require_relative '../base_adapter'
require_relative '../base_processor'

class ActiveSet
  class PaginateProcessor < BaseProcessor
    class ActiveRecordAdapter < BaseAdapter
      def process
        return return_set unless @set.respond_to? :to_sql

        return_set(paginated_set)
      end

      private

      def paginated_set
        return @set.none if @set.length <= page_size && page_number > 1
        @set.limit(page_size).offset(page_offset)
      end

      def page_offset
        return 0 if page_number == 1
        page_size * (page_number - 1)
      end

      def page_size
        @instruction.value
      end

      def page_number
        num = @instruction.attribute.to_i
        return 1 if (num - 1).negative?
        return 1 if (num - 1).zero?
        num
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activeset-0.5.8 lib/active_set/processors/paginate/active_record_adapter.rb
activeset-0.5.7 lib/active_set/processors/paginate/active_record_adapter.rb
activeset-0.5.6 lib/active_set/processors/paginate/active_record_adapter.rb
activeset-0.5.5 lib/active_set/processors/paginate/active_record_adapter.rb