lib/active_set/paginating/operation.rb in activeset-0.7.1 vs lib/active_set/paginating/operation.rb in activeset-0.8.0

- old
+ new

@@ -1,7 +1,10 @@ # frozen_string_literal: true +require_relative './enumerable_strategy' +require_relative './active_record_strategy' + class ActiveSet module Paginating class Operation def initialize(set, instructions_hash) @set = set @@ -43,60 +46,9 @@ # https://work.stevegrossi.com/2015/04/25/how-to-count-with-activerecord/ maybe_integer_or_hash = set.size return maybe_integer_or_hash.count if maybe_integer_or_hash.is_a?(Hash) maybe_integer_or_hash - end - end - - class EnumerableStrategy - def initialize(set, operation_instructions) - @set = set - @operation_instructions = operation_instructions - end - - def execute - return [] if @set.count <= @operation_instructions[:size] && - @operation_instructions[:page] > 1 - - @set[page_start..page_end] || [] - end - - private - - def page_start - return 0 if @operation_instructions[:page] == 1 - - @operation_instructions[:size] * (@operation_instructions[:page] - 1) - end - - def page_end - return page_start if @operation_instructions[:size] == 1 - - page_start + @operation_instructions[:size] - 1 - end - end - - class ActiveRecordStrategy - def initialize(set, operation_instructions) - @set = set - @operation_instructions = operation_instructions - end - - def execute - return false unless @set.respond_to? :to_sql - return @set.none if @set.length <= @operation_instructions[:size] && - @operation_instructions[:page] > 1 - - @set.limit(@operation_instructions[:size]).offset(page_offset) - end - - private - - def page_offset - return 0 if @operation_instructions[:page] == 1 - - @operation_instructions[:size] * (@operation_instructions[:page] - 1) end end end end