Sha256: 12a340a1c9dfd1200446ac623da20084d644532ca7d0a89ec25e693b6598dd9a

Contents?: true

Size: 1.78 KB

Versions: 5

Compression:

Stored size: 1.78 KB

Contents

module BootstrapPostPaginateHelper
  extend ActiveSupport::Concern

  include BootstrapHelper

  def post_will_paginate(collection=nil, options={})
    options, collection = collection, nil if collection.is_a? Hash
    # Taken from original will_paginate code to handle if the helper is not passed a collection object.
    collection ||= infer_collection_from_controller
    options[:renderer] ||= PostBootstrapLinkRenderer
    will_paginate(collection, options).try :html_safe
  end

  class PostBootstrapLinkRenderer < ::WillPaginate::ActionView::BootstrapLinkRenderer
    protected

    def url(page)
      @base_url_params ||= begin
        merge_optional_params(default_url_params)
      end
      js = ''
      @base_url_params.each do |k, v|
        js << %Q(
          var #{k.to_s} = document.createElement('input');
          $(page).attr('type', 'hidden');
          $(page).attr('id', '#{k.to_s}');
          $(page).attr('name', '#{k.to_s}');
          $(page).val('#{v.to_s}');
        )
      end

      js << %Q(
        var page = document.createElement('input');
        $(page).attr('type', 'hidden');
        $(page).attr('id', '#{@options[:param_name]||'page'}');
        $(page).attr('name', '#{@options[:param_name]||'page'}');
        $(page).val('#{page}');
        $('##{@options[:form]}').append($(page));
        $('##{@options[:form]}').get(0).submit();
        $(page).remove();
      )

      @base_url_params.each do |k, v|
        js << %Q(
          $('##{k.to_s}').remove();
        )
      end

      js
    end

    def link(text, target, attributes = {})
      if target.is_a? Fixnum
        attributes[:rel] = rel_value(target)
        target = url(target)
      end
      attributes[:href] = '#'
      attributes[:onclick] = target
      tag(:a, text, attributes)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bs-helper-0.1.4 app/helpers/bootstrap_post_paginate_helper.rb
bs-helper-0.1.3 app/helpers/bootstrap_post_paginate_helper.rb
bs-helper-0.1.2 app/helpers/bootstrap_post_paginate_helper.rb
bs-helper-0.1.1 app/helpers/bootstrap_post_paginate_helper.rb
bs-helper-0.1.0 app/helpers/bootstrap_post_paginate_helper.rb