Module: Kaminari::Helpers::SinatraHelpers::SinatraHelperMethods

Defined in:
lib/kaminari/helpers/sinatra_helpers.rb

Overview

Some helper methods that are used in Kaminari::Helpers::HelperMethods. Plus, monkey-patch for ‘paginate` to pass in an ActionViewTemplateProxy instance.

Instance Method Summary collapse

Instance Method Details



72
73
74
75
76
77
78
79
80
81
# File 'lib/kaminari/helpers/sinatra_helpers.rb', line 72

def link_to_if(condition, name, options = {}, html_options = {}, &block)
  options = url_for(options) if options.is_a? Hash
  if condition
    link_to(name, options, html_options)
  elsif block_given?
    capture_html(&block).html_safe
  else
    name
  end
end


83
84
85
# File 'lib/kaminari/helpers/sinatra_helpers.rb', line 83

def link_to_unless(condition, name, options = {}, html_options = {}, &block)
  link_to_if !condition, name, options, html_options, &block
end

#paginate(scope, options = {}) ⇒ Object

Overriding ‘paginate` to pass in an Action View renderer. If you’re sure your custom template is compatible with Padrino renderer, you might not need it. In that case, please add ‘template: self` option when calling this method.



90
91
92
93
94
95
96
97
# File 'lib/kaminari/helpers/sinatra_helpers.rb', line 90

def paginate(scope, options = {})
  current_path = env['PATH_INFO'] rescue nil
  current_params = Rack::Utils.parse_query(env['QUERY_STRING']).symbolize_keys rescue {}

  template = ActionViewTemplateProxy.new current_params: current_params, current_path: current_path, param_name: options[:param_name] || Kaminari.config.param_name

  super scope, {template: template}.merge(options)
end

#url_for(params) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kaminari/helpers/sinatra_helpers.rb', line 60

def url_for(params)
  current_path = env['PATH_INFO'] rescue nil
  current_params = Rack::Utils.parse_query(env['QUERY_STRING']) rescue {}

  extra_params = params.dup
  extra_params.delete :only_path

  query = current_params.merge(extra_params)
  res = current_path + (query.empty? ? '' : "?#{query.to_query}")
  res
end