Module: Snails::ViewHelpers

Defined in:
lib/snails.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



284
285
286
# File 'lib/snails.rb', line 284

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

Instance Method Details

#actionObject



288
289
290
# File 'lib/snails.rb', line 288

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


422
423
424
425
426
427
# File 'lib/snails.rb', line 422

def delete_link(options = {})
  post_button(options[:text], options[:path], options.merge({
    input_html: "<input type='hidden' name='_method' value='delete' />",
    css_class: 'danger'
  }))
end

#form_checkbox(object, field, options = {}) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/snails.rb', line 370

def form_checkbox(object, field, options = {})
  id, name, index, label = input_base(object, field, options)

  type  = options[:type] || :checkbox
  value = options[:value] ? "value='#{options[:value]}'" : ''

  if type.to_sym == :radio
    checked = object.send(field) == options[:value]
    value += checked ? " selected='selected'" : ''
  else
    checked = object.send(field)
    value += checked ? " checked='true'" : ''
  end

  label + raw_input(index, type, id, name, options[:placeholder], value)
end

#form_input(object, field, options = {}) ⇒ Object

forms



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

def form_input(object, field, options = {})
  id, name, index, label = input_base(object, field, options)
  type  = (options[:type] || :text).to_sym
  value = options[:value] ? "value='#{options[:value]}'" : (type == :password ? '' : "value='#{object.send(field)}'")

  classes = object.errors[field].any? ? 'has-errors' : ''
  label + raw_input(index, type, id, name, value, options[:placeholder], classes, options[:required])
end

#form_password(object, field, options = {}) ⇒ Object



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

def form_password(object, field, options = {})
  form_input(object, field, {:type => 'password'}.merge(options))
end

#form_putObject



401
402
403
# File 'lib/snails.rb', line 401

def form_put
  '<input type="hidden" name="_method" value="put" />'
end

#form_select_options(list, selected = nil) ⇒ Object



393
394
395
396
397
398
399
# File 'lib/snails.rb', line 393

def form_select_options(list, selected = nil)
  list.map do |name, val|
    val = name if val.nil?
    sel = val == selected ? 'selected="selected"' : ''
    "<option #{sel} value='#{val}'>#{name}</option>"
  end.join("\n")
end

#form_submit(text = 'Actualizar', classes = '') ⇒ Object



405
406
407
408
# File 'lib/snails.rb', line 405

def form_submit(text = 'Actualizar', classes = '')
  @tabindex = @tabindex ? @tabindex + 1 : 1
  "<button tabindex='#{@tabindex}' class='primary #{classes}' type='submit'>#{text}</button>"
end

#form_textarea(object, field, options = {}) ⇒ Object



387
388
389
390
391
# File 'lib/snails.rb', line 387

def form_textarea(object, field, options = {})
  id, name, index, label = input_base(object, field, options)
  style = options[:style] ? "style='#{options[:style]}'" : ''
  label + "<textarea #{style} tabindex='#{index}' id='#{id}' name='#{name}'>#{object.send(field)}</textarea>"
end

#get_page(counter) ⇒ Object

pagination



305
306
307
308
309
310
311
# File 'lib/snails.rb', line 305

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



292
293
294
295
# File 'lib/snails.rb', line 292

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

#post_button(text, path, opts = {}) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
# File 'lib/snails.rb', line 410

def post_button(text, path, opts = {})
  form_id      = opts.delete(:form_id) || path.gsub(/[^a-z0-9]/, '_').gsub(/_+/, '_')
  css_class    = opts.delete(:css_class) || ''
  input_html   = opts.delete(:input_html) || ''
  confirm_text = opts.delete(:confirm_text) || 'Seguro?'
  submit_val   = opts.delete(:value) || text
  '<form id="' + form_id + '" style="display:inline" method="post" action="' + url(path) + '" onsubmit="return confirm(\'' + confirm_text + '\');">
    ' + input_html + '
    <button name="submit" type="submit" class="' + css_class + ' button" value="' + submit_val + '">' + text + '</button>
  </form>'
end

#show_pager(array, path) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
# File 'lib/snails.rb', line 313

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

#simple_format(text, options = {}) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
# File 'lib/snails.rb', line 342

def simple_format(text, options = {})
  t = options.delete(:tag) || :p
  start_tag = tag(t, options, true)
  text = text.to_s.dup
  text.gsub!(/\r\n?/, "\n")                      # \r\n and \r -> \n
  text.gsub!(/\n\n+/, "</#{t}>\n\n#{start_tag}") # 2+ newline  -> paragraph
  text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />')   # 1 newline   -> br
  text.insert 0, start_tag
  text << "</#{t}>"
  text
end

#tag(name, options = nil, open = false) ⇒ Object

formatting



328
329
330
331
# File 'lib/snails.rb', line 328

def tag(name, options = nil, open = false)
  attributes = tag_attributes(options)
  "<#{name}#{attributes}#{open ? '>' : ' />'}"
end

#tag_attributes(options) ⇒ Object



333
334
335
336
337
338
339
340
# File 'lib/snails.rb', line 333

def tag_attributes(options)
  return '' unless options
  options.inject('') do |all,(key,value)|
    next all unless value
    all << ' ' if all.empty?
    all << %(#{key}="#{value}" )
  end.chomp!(' ')
end

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



297
298
299
300
# File 'lib/snails.rb', line 297

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