diff --git a/lib/kaminari/models/array_extension.rb b/lib/kaminari/models/array_extension.rb index 728bf08..2731dfd 100644 --- a/lib/kaminari/models/array_extension.rb +++ b/lib/kaminari/models/array_extension.rb @@ -12,6 +12,12 @@ module Kaminari def initialize(original_array, options = {}) @_original_array, @_limit_value, @_offset_value, @_total_count = original_array, (options[:limit] || default_per_page).to_i, options[:offset].to_i, options[:total_count] + if options[:limit] && options[:offset] + class << self + include Kaminari::PageScopeMethods + end + end + if options[:total_count] super original_array else @@ -36,11 +42,7 @@ module Kaminari # returns another chunk of the original array def offset(num) - arr = self.class.new @_original_array, :limit => @_limit_value, :offset => num, :total_count => @_total_count - class << arr - include Kaminari::PageScopeMethods - end - arr + self.class.new @_original_array, :limit => @_limit_value, :offset => num, :total_count => @_total_count end end diff --git a/spec/models/array_spec.rb b/spec/models/array_spec.rb index 7704b45..779caba 100644 --- a/spec/models/array_spec.rb +++ b/spec/models/array_spec.rb @@ -1,6 +1,11 @@ require File.expand_path('../spec_helper', File.dirname(__FILE__)) describe Kaminari::PaginatableArray do + context 'specifying limit and offset when initializing' do + subject { Kaminari::PaginatableArray.new((1..100).to_a, :limit => 10, :offset => 20) } + its(:current_page) { should == 3 } + end + let(:array) { Kaminari::PaginatableArray.new((1..100).to_a) } describe '#page' do shared_examples_for 'the first page of array' do