Module: Snails::ViewHelpers

Defined in:
lib/snails/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



324
325
326
327
328
# File 'lib/snails/app.rb', line 324

def self.included(base)
  Time.include(RelativeTime) unless Time.instance_methods.include?(:relative)
  base.include(FormHelpers)
  base.include(SimpleFormat)
end

Instance Method Details

#action ⇒ Object



330
331
332
# File 'lib/snails/app.rb', line 330

def action
  request.path_info.gsub('/','').blank? ? 'home' : request.path_info.gsub('/',' ')
end

#get_page(counter) ⇒ Object

pagination



347
348
349
350
351
352
353
# File 'lib/snails/app.rb', line 347

def get_page(counter)
  curr = params[:page].to_i
  i = (curr == 0 && counter == 1)  ? 2
    : (curr == 2 && counter == -1) ? 0
    : curr + counter
  i == 0 ? "" : "/page/#{i}"
end

#partial(name, opts = {}) ⇒ Object



334
335
336
337
# File 'lib/snails/app.rb', line 334

def partial(name, opts = {})
  partial_name = name.to_s["/"] ? name.to_s.reverse.sub("/", "_/").reverse : "_#{name}"
  erb(partial_name.to_sym, { layout: false }.merge(opts))
end

#show_pager(array, path) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
# File 'lib/snails/app.rb', line 355

def show_pager(array, path)
  # remove page from path
  path = (env['SCRIPT_NAME'] + path.gsub(/[?|&|\/]page[=|\/]\d+/,''))

  prevlink = '<li>' + link_to("#{path}#{get_page(-1)}", '&larr; Prev').sub('//', '/') + '</li>'
  nextlink = array.count != Routes::PER_PAGE ? ""
    : '<li>' + link_to("#{path}#{get_page(1)}", 'Next &rarr;').sub('//', '/') + '</li>'

  str = params[:page] ? prevlink + nextlink : nextlink
  str != "" ? "<ul class='pager'>" + str + "</ul>" : ''
end

#view(view_name, opts = {}) ⇒ Object



339
340
341
342
# File 'lib/snails/app.rb', line 339

def view(view_name, opts = {})
  layout = request.xhr? ? false : true
  erb(view_name.to_sym, { layout: layout }.merge(opts))
end