Module: Redacted::ActionView::Helpers

Defined in:
lib/redacted/action_view/helpers.rb

Instance Method Summary collapse

Instance Method Details

#ascii_redact(str) ⇒ Object



4
5
6
7
# File 'lib/redacted/action_view/helpers.rb', line 4

def ascii_redact str
  length = str.length
  "\u2588" * length unless length.zero?
end

#redact(str) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/redacted/action_view/helpers.rb', line 9

def redact str
  if str.include?("\n")
    redact_paragraphs(str).join('').html_safe
  else
    redact_text(str).html_safe
  end
end

#redact_html(html_str) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/redacted/action_view/helpers.rb', line 28

def redact_html html_str
  doc = Nokogiri::HTML::DocumentFragment.parse(html_str)
  doc.traverse do |ele|
    ele.replace(redact_text(ele.content.chomp)) if ele.text?
  end
  doc.to_html
end

#redact_paragraphs(str) ⇒ Object



17
18
19
20
21
# File 'lib/redacted/action_view/helpers.rb', line 17

def redact_paragraphs str
  str.lines.map do |l|
    "<p>#{redact_text(l.chomp)}</p>" if l.chomp.present?
  end.reject{|l|l.nil?}
end

#redact_text(str) ⇒ Object



23
24
25
26
# File 'lib/redacted/action_view/helpers.rb', line 23

def redact_text str
  return '' unless str
  "<span class=\"redacted-text\">#{generate_fake_text(str)}</span>"
end