Module: EffectiveTrashHelper

Included in:
Effective::Datatables::Trash
Defined in:
app/helpers/effective_trash_helper.rb

Constant Summary collapse

ALLOWED_TAGS =
ActionView::Base.sanitized_allowed_tags.to_a + ['table', 'thead', 'tbody', 'tfoot', 'tr', 'td', 'th']
ALLOWED_ATTRIBUTES =
ActionView::Base.sanitized_allowed_attributes.to_a + ['colspan', 'rowspan']

Instance Method Summary collapse

Instance Method Details

#effective_trash_simple_format(value) ⇒ Object



67
68
69
# File 'app/helpers/effective_trash_helper.rb', line 67

def effective_trash_simple_format(value)
  simple_format(sanitize(value.to_s, :tags => ALLOWED_TAGS, :attributes => ALLOWED_ATTRIBUTES), {}, :sanitize => false)
end

#format_trash_details_value(trash, key) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/effective_trash_helper.rb', line 36

def format_trash_details_value(trash, key)
  value = trash.details[key]

  if value.kind_of?(Hash)
    tableize_hash(value, :class => 'table', :th => true)
  elsif value.kind_of?(Array)
    value.map { |value| effective_trash_simple_format(value) }.join.html_safe
  else
    value = value.to_s

    open = value.index('<!DOCTYPE html') || value.index('<html')
    close = value.rindex('</html>') if open.present?
    return effective_trash_simple_format(value) unless (open.present? && close.present?)

    before = value[0...open]
    after = value[(close+7)..-1]
    divide = before.sub!('<hr>', '').present?

    [
      h(before).gsub("\n", '<br>'),
      ((:hr) if divide),
      (:iframe, '',
        src: '#',
        style: 'frameborder: 0; border: 0; width: 100%; height: 100%;',
        onload: "this.style.height=(this.contentDocument.body.scrollHeight + 30) + 'px';",
        scrolling: 'no'),
      h(after).gsub("\n", '<br>')
    ].compact.join.html_safe
  end
end

#render_trash(trash) ⇒ Object



5
6
7
# File 'app/helpers/effective_trash_helper.rb', line 5

def render_trash(trash)
  render(partial: 'effective/trash/trash', locals: {trash: trash})
end

#tableize_hash(hash, options = {}) ⇒ Object

Call me with :th => true, :sub_th => false Any other options are sent to the table tag



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/effective_trash_helper.rb', line 11

def tableize_hash(hash, options = {})
  if hash.present? && hash.kind_of?(Hash)
    (:table, class: options[:class]) do
      (:tbody) do
        hash.map do |k, v|
          (:tr) do
            ((options[:th] ? :th : :td), k) +
            (:td) do
              if v.kind_of?(Hash)
                tableize_hash(v, options.merge({class: 'table table-hover', th: (options.key?(:sub_th) ? options[:sub_th] : options[:th])}))
              elsif v.kind_of?(Array)
                '[' + v.join(', ') + ']'
              else
                v.to_s
              end
            end
          end
        end.join('').html_safe
      end
    end.html_safe
  else
    hash.to_s.html_safe
  end
end