Module: Workbench::ListHelper

Defined in:
app/helpers/workbench/list_helper.rb

Overview

Helpers that generate simple lists, tables etc.

Instance Method Summary collapse

Instance Method Details

#list_tag(object, method) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/workbench/list_helper.rb', line 31

def list_tag(object, method)
  return (:div, object_tag(object) + "has no method #{method}") unless object.respond_to?(method)
  if object.send(method).any?
    (:ul) do
      object.send(method).collect{|i|
        (:li, object_tag(i))
      }.join.html_safe
    end
  else
    (:span, "No #{method} attached.", class: :warning)
  end
end

#recent_objects_list(model, recent_objects) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/workbench/list_helper.rb', line 18

def recent_objects_list(model, recent_objects)
  partial = nil
  base = model.name.tableize  
  if self.respond_to?("#{base}_recent_objects_partial") 
    partial =  "/#{base}/recent_objects_list"
  else
    partial = '/shared/data/project/recent_objects_list'
  end

  render partial: partial , locals: {recent_objects: recent_objects}
end

#similarly_named_records_list(instance) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/helpers/workbench/list_helper.rb', line 4

def similarly_named_records_list(instance)
  model   = instance.class
  name    = instance.name
  records = model.where(['(name LIKE ? OR NAME like ?) AND id != ?', "#{name}%", "%#{name}%", instance]).limit(5)
  (:span) do
    (:em, 'Similarly named records: ') +
        (:span) do
          (records.count == 0 ?
              'none' :
              records.collect { |r| link_to(r.name, r) }.join(', ').html_safe)
        end
  end
end