Sha256: 0b66d490ed886c0f14965dee9e01a4a1c7511e20a1ffab54a3280206efedcfa1

Contents?: true

Size: 845 Bytes

Versions: 1

Compression:

Stored size: 845 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.count <= 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

1 entries across 1 versions & 1 rubygems

Version Path
activeset-0.5.4 lib/active_set/processors/paginate/active_record_adapter.rb