Module: EffectiveResourcesHelper

Defined in:
app/helpers/effective_resources_helper.rb

Instance Method Summary collapse

Instance Method Details

#approve_icon_to(path, options = {}) ⇒ Object



54
55
56
# File 'app/helpers/effective_resources_helper.rb', line 54

def approve_icon_to(path, options = {})
  glyphicon_to('ok', path, {title: 'Approve'}.merge(options))
end

#destroy_icon_to(path, options = {}) ⇒ Object



41
42
43
44
# File 'app/helpers/effective_resources_helper.rb', line 41

def destroy_icon_to(path, options = {})
  defaults = {title: 'Destroy', data: {method: :delete, confirm: 'Delete this item?'}}
  glyphicon_to('trash', path, defaults.merge(options))
end

#edit_icon_to(path, options = {}) ⇒ Object



37
38
39
# File 'app/helpers/effective_resources_helper.rb', line 37

def edit_icon_to(path, options = {})
  glyphicon_to('edit', path, {title: 'Edit'}.merge(options))
end

#format_resource_value(value) ⇒ Object



105
106
107
108
109
110
# File 'app/helpers/effective_resources_helper.rb', line 105

def format_resource_value(value)
  @format_resource_tags ||= ActionView::Base.sanitized_allowed_tags.to_a + ['table', 'thead', 'tbody', 'tfoot', 'tr', 'td', 'th']
  @format_resource_atts ||= ActionView::Base.sanitized_allowed_attributes.to_a + ['colspan', 'rowspan']

  simple_format(sanitize(value.to_s, tags: @format_resource_tags, attributes: @format_resource_atts), {}, sanitize: false)
end

#glyphicon_to(icon, path, options = {}) ⇒ Object Also known as: bootstrap_icon_to, glyph_icon_to



62
63
64
65
66
67
68
69
70
# File 'app/helpers/effective_resources_helper.rb', line 62

def glyphicon_to(icon, path, options = {})
  (:a, options.merge(href: path)) do
    if icon.start_with?('glyphicon-')
      (:span, '', class: "glyphicon #{icon}")
    else
      (:span, '', class: "glyphicon glyphicon-#{icon}")
    end
  end
end

#number_to_duration(duration) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/effective_resources_helper.rb', line 20

def number_to_duration(duration)
  duration = duration.to_i
  value = duration.abs

  [
    ('-' if duration < 0),
    ("#{value / 60}h " if value >= 60),
    ("#{'%0.2d' % (value % 60)}m" if value > 0),
    ('0m' if value == 0),
  ].compact.join
end

#ok_icon_to(path, options = {}) ⇒ Object



50
51
52
# File 'app/helpers/effective_resources_helper.rb', line 50

def ok_icon_to(path, options = {})
  glyphicon_to('ok', path, {title: 'OK'}.merge(options))
end

#remove_icon_to(path, options = {}) ⇒ Object



58
59
60
# File 'app/helpers/effective_resources_helper.rb', line 58

def remove_icon_to(path, options = {})
  glyphicon_to('remove', path, {title: 'Remove'}.merge(options))
end

#settings_icon_to(path, options = {}) ⇒ Object



46
47
48
# File 'app/helpers/effective_resources_helper.rb', line 46

def settings_icon_to(path, options = {})
  glyphicon_to('cog', path, {title: 'Settings'}.merge(options))
end

#show_icon_to(path, options = {}) ⇒ Object

Icon Helpers for actions_column or elsewhere



33
34
35
# File 'app/helpers/effective_resources_helper.rb', line 33

def show_icon_to(path, options = {})
  glyphicon_to('eye-open', path, {title: 'Show'}.merge(options))
end

#simple_form_save(form, options = {class: 'text-right'}, &block) ⇒ Object



14
15
16
17
18
# File 'app/helpers/effective_resources_helper.rb', line 14

def simple_form_save(form, options = {class: 'text-right'}, &block)
  (:p, class: options[:class]) do
    form.button(:submit, 'Save', data: { disable_with: 'Saving...' }) + (capture(&:block) if block_given?)
  end
end

#simple_form_submit(form, options = {class: 'text-right'}, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'app/helpers/effective_resources_helper.rb', line 3

def simple_form_submit(form, options = {class: 'text-right'}, &block)
  (:p, class: options[:class]) do
    [
      form.button(:submit, 'Save', data: { disable_with: 'Saving...' }),
      form.button(:submit, 'Save and Continue', data: { disable_with: 'Saving...' }),
      form.button(:submit, 'Save and Add New', data: { disable_with: 'Saving...' }),
      (capture(&block) if block_given?)
    ].compact.join(' ').html_safe
  end
end

#tableize_hash(obj, table: 'table', th: true, sub_table: 'table', sub_th: true, flatten: true) ⇒ Object

This is used by effective_orders, effective_logging, effective_trash and effective_mergery



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/effective_resources_helper.rb', line 77

def tableize_hash(obj, table: 'table', th: true, sub_table: 'table', sub_th: true, flatten: true)
  case obj
  when Hash
    if flatten && obj[:attributes].kind_of?(Hash)
      obj = obj[:attributes].merge(obj.except(:attributes))
    end

    (:table, class: table.presence) do
      (:tbody) do
        obj.map do |key, value|
          (:tr, class: key.to_param) do
            ((th == true ? :th : :td), key) +
            (:td) { tableize_hash(value, table: sub_table, th: sub_th, sub_table: sub_table, sub_th: sub_th, flatten: flatten) }
          end
        end.join.html_safe
      end
    end
  when Array
    obj.map { |value| tableize_hash(value, table: sub_table, th: sub_th, sub_table: sub_table, sub_th: sub_th, flatten: flatten) }.join('<br>')
  when Symbol
    ":#{obj}"
  when NilClass
    '-'
  else
    obj.to_s.presence || '""'
  end.html_safe
end