Module: Moar::Helper
- Defined in:
- lib/moar/helper.rb
Instance Method Summary collapse
-
#link_to_more(results, target, html_options = {}) ⇒ String?
Renders an anchor element that links to more paginated results.
Instance Method Details
permalink #link_to_more(results, target, html_options = {}) ⇒ String?
Renders an anchor element that links to more paginated results. If JavaScript is enabled, and the current page number is less than the number of pagination increments (see Config#increments or Controller::ClassMethods#moar_increments), the link will use Ajax to fetch the results and append them to the element matching the CSS selector specified by target
. Otherwise, the link will navigate to the next page of results.
The text of the link is determined via I18n using translation key :“moar.more”. A results_name
interpolation argument is provided containing the humanized translated pluralized downcased name of the relevant model. For example, if the translation string is "Need more %{results_name}!"
and results
is an array of CowBell
models, the link text will be “Need more cow bells!”.
If no more results are available, this helper method will return nil so that no link is rendered. Whether there are more results is estimated by comparing results.size
with the limit applied by Controller#moar. This technique eliminates the need for an extra database query, but can result in a false positive (i.e. rendering a link to an empty page) when the actual last page of results is also a full page. This is deemed an acceptable trade-off because with a large number of pages and a large total number of results per page it is an unlikely occurrence, and because an extra database query cannot entirely prevent a link to an empty page, in the case where records are deleted before the user can click through to the final page.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/moar/helper.rb', line 42 def link_to_more(results, target, = {}) raise "#{controller.class}##{action_name} did not invoke #moar" unless defined?(@moar) unless results.size < @moar.limit params = request.query_parameters.except(Moar.config.accumulation_param.to_s) params[Moar.config.page_param.to_s] = @moar.page + 1 = { controller: controller_path, action: action_name, params: params } model_name = if results.is_a?(ActiveRecord::Relation) results.model else results.first end.model_name = { results_name: model_name.human( count: 2, default: model_name.human.pluralize(I18n.locale) ).downcase, } = .dup ["data-paginates"] = target if @moar.page < @moar.increments.length ["data-accumulation-param"] = Moar.config.accumulation_param.to_s ["data-remote"] = true ["data-type"] = "html" ["data-disable-with"] = I18n.t(:"moar.loading", ) end link_to I18n.t(:"moar.more", ), , end end |