Class: Spree::Amazon::ProductCollection

Inherits:
Array
  • Object
show all
Defined in:
app/models/spree/amazon/product_collection.rb

Overview

For paginate

Constant Summary

MAX_TOTAL_PAGES =
200

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(options = { }) ⇒ Object



40
41
42
43
44
45
46
# File 'app/models/spree/amazon/product_collection.rb', line 40

def build(options = { })
  @collection = new(options[:products])
  @max_total_pages = options[:search_index].to_s == "All" ? 5 : MAX_TOTAL_PAGES
  @collection.total_entries = @collection.total_pages = build_total_pages(options[:total_entries], @max_total_pages)
  @collection.current_page  = options[:current_page] || 1
  @collection
end

.build_total_pages(total_pages, max_total_pages) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'app/models/spree/amazon/product_collection.rb', line 55

def build_total_pages(total_pages, max_total_pages)
  if total_pages.to_i >= max_total_pages.to_i
    max_total_pages.to_i
  elsif total_pages.to_i == 0
    1
  else
    total_pages.to_i
  end
end

.empty_buildObject



48
49
50
51
52
53
# File 'app/models/spree/amazon/product_collection.rb', line 48

def empty_build
  @collection = new([])
  @collection.total_entries = 1
  @collection.current_page  = 1
  @collection
end

Instance Method Details

#current_pageObject



31
32
33
# File 'app/models/spree/amazon/product_collection.rb', line 31

def current_page
  @current_page
end

#current_page=(v) ⇒ Object



35
36
37
# File 'app/models/spree/amazon/product_collection.rb', line 35

def current_page=(v)
  @current_page = v
end

#next_pageObject

The next page number. Returns nil if on last page.



22
23
24
# File 'app/models/spree/amazon/product_collection.rb', line 22

def next_page
  @current_page < total_pages ? (@current_page + 1): nil
end

#per_pageObject



12
13
14
# File 'app/models/spree/amazon/product_collection.rb', line 12

def per_page
  10
end

#previous_pageObject

The previous page number. Returns nil if on first page.



17
18
19
# File 'app/models/spree/amazon/product_collection.rb', line 17

def previous_page
  @current_page > 1 ? (@current_page - 1) : nil
end

#total_pagesObject

Total number of pages with found results.



27
28
29
# File 'app/models/spree/amazon/product_collection.rb', line 27

def total_pages
  (@total_entries.to_i / per_page.to_f).ceil
end