From 3b2ebcd3f338790271d0e4c490ea2699f3b73824 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 27 Oct 2014 23:28:50 +0300 Subject: [PATCH] #369: added BootstrapLinkRenderer --- app/helpers/paginate_helper.rb | 10 ++++ lib/ext/bootstrap_link_renderer.rb | 62 +++++++++++++++++++++ lib/templates/haml/scaffold/_form.html.haml | 10 ++++ 3 files changed, 82 insertions(+) create mode 100644 lib/ext/bootstrap_link_renderer.rb create mode 100644 lib/templates/haml/scaffold/_form.html.haml diff --git a/app/helpers/paginate_helper.rb b/app/helpers/paginate_helper.rb index 6da203cf4..569ae2e88 100644 --- a/app/helpers/paginate_helper.rb +++ b/app/helpers/paginate_helper.rb @@ -9,6 +9,16 @@ module PaginateHelper {page: page, per_page: per_page} end + def will_paginate(collection_or_options = nil, options = {}) + if collection_or_options.is_a? Hash + options, collection_or_options = collection_or_options, nil + end + options.merge!(renderer: BootstrapLinkRenderer) unless options[:renderer] + options.merge!(next_label: I18n.t('datatables.next_label')) unless options[:next_label] + options.merge!(previous_label: I18n.t('datatables.previous_label')) unless options[:previous_label] + super *[collection_or_options, options].compact + end + def angularjs_will_paginate(collection_or_options = nil, options = {}) if collection_or_options.is_a? Hash options, collection_or_options = collection_or_options, nil diff --git a/lib/ext/bootstrap_link_renderer.rb b/lib/ext/bootstrap_link_renderer.rb new file mode 100644 index 000000000..577c544c9 --- /dev/null +++ b/lib/ext/bootstrap_link_renderer.rb @@ -0,0 +1,62 @@ +# See: https://github.com/bootstrap-ruby/will_paginate-bootstrap/blob/master/lib/bootstrap_pagination/bootstrap_renderer.rb +class BootstrapLinkRenderer < WillPaginate::ActionView::LinkRenderer + + ELLIPSIS = "…" + + def to_html + list_items = pagination.map do |item| + case item + when Fixnum + page_number(item) + else + send(item) + end + end.join(@options[:link_separator]) + + tag("ul", list_items, class: ul_class) + end + + def container_attributes + super.except(*[:link_options]) + end + + protected + + def page_number(page) + link_options = @options[:link_options] || {} + + if page == current_page + tag("li", tag("span", page), class: "active") + else + tag("li", link(page, page, link_options.merge(rel: rel_value(page)))) + end + end + + def previous_or_next_page(page, text, classname) + link_options = @options[:link_options] || {} + + if page + tag("li", link(text, page, link_options), class: classname) + else + tag("li", tag("span", text), class: "%s disabled" % classname) + end + end + + def gap + tag("li", tag("span", ELLIPSIS), class: "disabled") + end + + def previous_page + num = @collection.current_page > 1 && @collection.current_page - 1 + previous_or_next_page(num, @options[:previous_label], "prev") + end + + def next_page + num = @collection.current_page < @collection.total_pages && @collection.current_page + 1 + previous_or_next_page(num, @options[:next_label], "next") + end + + def ul_class + ["pagination", @options[:class]].compact.join(" ") + end +end diff --git a/lib/templates/haml/scaffold/_form.html.haml b/lib/templates/haml/scaffold/_form.html.haml new file mode 100644 index 000000000..ac3aa7bc1 --- /dev/null +++ b/lib/templates/haml/scaffold/_form.html.haml @@ -0,0 +1,10 @@ += simple_form_for(@<%= singular_table_name %>) do |f| + = f.error_notification + + .form-inputs + <%- attributes.each do |attribute| -%> + = f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> + <%- end -%> + + .form-actions + = f.button :submit