Class: Bhf::Pagination
- Inherits:
-
Object
- Object
- Bhf::Pagination
- Defined in:
- lib/bhf/pagination.rb
Instance Attribute Summary collapse
-
#offset_per_page ⇒ Object
readonly
Returns the value of attribute offset_per_page.
-
#offset_to_add ⇒ Object
readonly
Returns the value of attribute offset_to_add.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
- #create(platform) ⇒ Object
- #info(platform, options = {}) ⇒ Object
-
#initialize(offset_per_page = 10, offset_to_add = 5) ⇒ Pagination
constructor
A new instance of Pagination.
- #load_less(platform, attributes = {}) ⇒ Object
- #load_more(platform, attributes = {}, plus = true) ⇒ Object
Constructor Details
#initialize(offset_per_page = 10, offset_to_add = 5) ⇒ Pagination
Returns a new instance of Pagination.
19 20 21 22 |
# File 'lib/bhf/pagination.rb', line 19 def initialize(offset_per_page = 10, offset_to_add = 5) @offset_per_page = offset_per_page || 10 @offset_to_add = offset_to_add end |
Instance Attribute Details
#offset_per_page ⇒ Object (readonly)
Returns the value of attribute offset_per_page.
17 18 19 |
# File 'lib/bhf/pagination.rb', line 17 def offset_per_page @offset_per_page end |
#offset_to_add ⇒ Object (readonly)
Returns the value of attribute offset_to_add.
17 18 19 |
# File 'lib/bhf/pagination.rb', line 17 def offset_to_add @offset_to_add end |
#template ⇒ Object
Returns the value of attribute template.
16 17 18 |
# File 'lib/bhf/pagination.rb', line 16 def template @template end |
Instance Method Details
#create(platform) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bhf/pagination.rb', line 24 def create(platform) platform_params = template.params[platform.name] || {} links = if !(page_links = template.paginate(platform.objects, { theme: 'bhf', param_name: [platform.name, :page], params: template.params })).blank? "#{load_more(platform)} #{page_links}" elsif platform.objects.num_pages == 1 && platform.objects.size > @offset_to_add load_less(platform) end if links template.content_tag(:div, links.html_safe, {class: 'pagination'}) end end |
#info(platform, options = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bhf/pagination.rb', line 42 def info(platform, = {}) collection = platform.objects if collection.respond_to?(:num_pages) and collection.num_pages > 1 I18n.t('bhf.pagination.info.default', { name: platform.title, count: collection.total_count, offset_start: collection.offset_value + 1, offset_end: collection.offset_value + collection.length }) else I18n.t('bhf.pagination.info', { name_zero: platform.title, name_singular: platform.title_singular, name_plural: platform.title, count: collection.size }) end.html_safe end |
#load_less(platform, attributes = {}) ⇒ Object
85 86 87 |
# File 'lib/bhf/pagination.rb', line 85 def load_less(platform, attributes = {}) load_more(platform, attributes, false) end |
#load_more(platform, attributes = {}, plus = true) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/bhf/pagination.rb', line 62 def load_more(platform, attributes = {}, plus = true) platform_params = template.params[platform.name] || {} load_offset = @offset_per_page load_offset = platform_params[:per_page].to_i if platform_params[:per_page] if plus load_offset += @offset_to_add else load_offset -= @offset_to_add end platform_params.delete(:page) platform_params[:per_page] = load_offset direction = plus ? 'more' : 'less' template.link_to( I18n.t("bhf.pagination.load_#{direction}"), template.page_path( platform.page_name, template.params.merge(platform.name => platform_params) ), attributes.merge(class: "load_#{direction}") ) end |