Module: Ditty::Helpers::Views

Defined in:
lib/ditty/helpers/views.rb

Instance Method Summary collapse

Instance Method Details

#delete_form(entity, label = 'Delete') ⇒ Object



44
45
46
47
# File 'lib/ditty/helpers/views.rb', line 44

def delete_form(entity, label = 'Delete')
  locals = { delete_label: label, entity: entity }
  haml :'partials/delete_form', locals: locals
end

#filter_control(filter, opts = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/ditty/helpers/views.rb', line 25

def filter_control(filter, opts = {})
  meth = "#{filter[:name]}_options".to_sym
  return unless respond_to? meth
  haml :'partials/filter_control', locals: {
    name: filter[:name],
    label: opts[:label] || filter[:name].to_s.titlecase,
    options: send(meth)
  }
end

#flash_messages(key = :flash) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/ditty/helpers/views.rb', line 35

def flash_messages(key = :flash)
  return '' if flash(key).empty?
  id = (key == :flash ? 'flash' : "flash_#{key}")
  messages = flash(key).collect do |message|
    "  <div class='alert alert-#{message[0]} alert-dismissable' role='alert'>#{message[1]}</div>\n"
  end
  "<div id='#{id}'>\n" + messages.join + '</div>'
end

#form_control(name, model, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ditty/helpers/views.rb', line 6

def form_control(name, model, opts = {})
  label = opts.delete(:label) || name.to_s.titlecase
  klass = opts.delete(:class) || 'form-control' unless opts[:type] == 'file'
  group = opts.delete(:group) || model.class.to_s.demodulize.underscore
  field = opts.delete(:field) || name
  default = opts.delete(:default) || nil

  attributes = { type: 'text', id: name, name: "#{group}[#{name}]", class: klass }.merge(opts)
  haml :'partials/form_control', locals: {
    model: model,
    label: label,
    attributes: attributes,
    name: name,
    group: group,
    field: field,
    default: default
  }
end

#pagination(list, base_path) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/ditty/helpers/views.rb', line 49

def pagination(list, base_path)
  locals = {
    next_link: list.last_page? ? '#' : "#{base_path}?page=#{list.next_page}&count=#{list.page_size}",
    prev_link: list.first_page? ? '#' : "#{base_path}?page=#{list.prev_page}&count=#{list.page_size}",
    base_path: base_path,
    list: list
  }
  haml :'partials/pager', locals: locals
end