Sha256: 9d56ae24201ad2be672abc48c1d60941d929f74909816db3402ac1d2492e2147

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

format do
  def limit
    default_limit
  end

  def offset
    search_params[:offset] || 0
  end
end

format :html do
  def with_paging path_args={}
    paging_path_args path_args
    output [yield, _optional_render_paging]
  end

  view :paging, cache: :never do
    return "" unless paging_needed?
    <<-HTML
      <nav>
        <ul class="pagination paging">
          #{paging_links.join}
        </ul>
      </nav>
    HTML
  end

  def paging_links
    total_pages = ((count_with_params - 1) / limit).to_i
    current_page = (offset / limit).to_i
    PagingLinks.new(total_pages, current_page)
               .build do |text, page, status, options|
      page_link_li text, page, status, options
    end
  end

  # First page is 0 (not 1)
  def page_link_li text, page, status, options={}
    wrap_with :li, class: status do
      page_link text, page, options
    end
  end

  def page_link text, page, options
    return text unless page
    paging_path_args[:offset] = page * limit
    options.merge! class: "card-paging-link slotter",
                   remote: true,
                   path: paging_path_args
    link_to raw(text), options
  end

  def paging_path_args local_args={}
    @paging_path_args ||= {
      limit: limit,
      view: voo.home_view,
      slot: voo.slot_options
    }.merge(extra_paging_path_args).merge local_args
  end

  def paging_view
    (voo && voo.home_view) || :content
  end

  def extra_paging_path_args
    {}
  end

  def paging_needed?
    return false if limit < 1
    return false if fewer_results_than_limit? # avoid extra count search
    # count search result instead
    limit < count_with_params
  end

  # clear we don't need paging even before running count query
  def fewer_results_than_limit?
    return false unless offset.zero?
    limit > offset + search_with_params.length
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
card-1.20.0 mod/standard/set/abstract/search/paging.rb