class Middleman::Pagination::Pageable

Attributes

name[R]

Public Class Methods

new(name, &block) click to toggle source
# File lib/middleman/pagination/pageable.rb, line 7
def initialize(name, &block)
  @name = name
  @set = block
end

Public Instance Methods

new_resources(extension_context, resources) click to toggle source
# File lib/middleman/pagination/pageable.rb, line 12
def new_resources(extension_context, resources)
  pagination_indexes(resources).map do |resource|
    new_pages_for_index(extension_context, resource, resources)
  end.compact
end

Private Instance Methods

add_pagination_to(resource, attributes = {}) click to toggle source
# File lib/middleman/pagination/pageable.rb, line 58
def add_pagination_to(resource, attributes = {})
  in_page_context = InPageContext.new(attributes)
  resource.add_metadata(locals: { 'pagination' => in_page_context })
end
new_pages_for_index(extension_context, index, resources) click to toggle source
# File lib/middleman/pagination/pageable.rb, line 34
def new_pages_for_index(extension_context, index, resources)
  symbolic_replacement_path = pagination_data(index, :path)

  pageable_context = PageableContext.new(
    per_page: pagination_data(index, :per_page) || 20,
    set: set(extension_context, resources),
    index_resources: [index]
  )

  add_pagination_to(index, pageable_context: pageable_context, page_num: 1)

  (2..pageable_context.total_page_num).map do |page_num|
    new_index = IndexPage.new(extension_context,
                              index,
                              pageable_context,
                              page_num,
                              symbolic_replacement_path).resource

    pageable_context.index_resources << new_index

    new_index
  end
end
pagination_data(resource, key) click to toggle source
# File lib/middleman/pagination/pageable.rb, line 63
def pagination_data(resource, key)
  keys = [:pagination, key]

  [resource.data, resource.metadata[:options]].inject(nil) do |result, data_source|
    result or keys.inject(data_source) { |source, key| source.try(:[], key) }
  end
end
pagination_indexes(resources) click to toggle source
# File lib/middleman/pagination/pageable.rb, line 24
def pagination_indexes(resources)
  Enumerator.new do |enum| 
    resources.each do |resource|
      if !resource.ignored? && pagination_data(resource, :for) == name.to_s
        enum.yield resource
      end
    end
  end
end
set(extension_context, resources) click to toggle source
# File lib/middleman/pagination/pageable.rb, line 20
def set(extension_context, resources)
  OpenStruct.new(resources: resources, data: extension_context.data).instance_eval(&@set)
end