Module: Snails::ViewHelpers

Defined in:
lib/snails/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

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

Instance Method Details

#actionObject



344
345
346
# File 'lib/snails/app.rb', line 344

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

#get_page(counter) ⇒ Object

pagination



379
380
381
382
383
384
385
# File 'lib/snails/app.rb', line 379

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



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

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

#show_date(time) ⇒ Object



366
367
368
369
# File 'lib/snails/app.rb', line 366

def show_date(time)
  time = Time.parse(time) if time.is_a?(String) && !time.blank?
  time ? time.strftime("%d de %b, %Y") : ''
end

#show_pager(array, path, per_page = 50) ⇒ Object



387
388
389
390
391
392
393
394
395
396
# File 'lib/snails/app.rb', line 387

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

  prevlink = "<li><a href='#{path}#{get_page(-1)}'>&larr; Prev</a></li>"
  nextlink = array.size != per_page ? "" : "<li><a href='#{path}#{get_page(1)}'>Next &rarr;</a></li>"

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

#show_time(time) ⇒ Object



371
372
373
374
# File 'lib/snails/app.rb', line 371

def show_time(time)
  time = Time.parse(time) if time.is_a?(String) && !time.blank?
  time ? time.strftime("%d de %b, %Y a las %H:%M") : ''
end

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

Possible render options are: :content_type The content type to use, same arguments as content_type. :layout If set to something falsy, no layout is rendered, otherwise the specified layout is used (Ignored for sass) :layout_engine Engine to use for rendering the layout. :locals A hash with local variables that should be available in the template :scope If set, template is evaluate with the binding of the given object rather than the application instance. :views Views directory to use.



361
362
363
364
# File 'lib/snails/app.rb', line 361

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