Module: ParagraphHelper

Defined in:
app/helpers/paragraph_helper.rb

Instance Method Summary collapse

Instance Method Details

#prettify_notification(str = '') ⇒ Object



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

def prettify_notification str=''
  str.gsub!(/\B(##)\w*[a-zA-Z0-9_]+\w*/) do |x| 
    obj = (x.split('_')[0][2..100]).constantize.find_all_by_id(x.split('_')[1]).last
    if can? :read, obj
      link_to obj.to_s, obj
    else
      obj.to_s
    end
  end
  str.html_safe
end

#truncate_html(text, len = 30, at_end = ' ...') ⇒ Object

Return truncating in html raw format



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/paragraph_helper.rb', line 19

def truncate_html(text, len = 30, at_end = ' ...')
  p = REXML::Parsers::PullParser.new((text.presence || '<p> </p>').gsub(/[\r\n\t]/, ''))
  tags = []
  new_len = len
  results = ''
  while p.has_next? && new_len > 0
    p_e = p.pull
    case p_e.event_type
    when :start_element
      tags.push p_e[0]
      results << "<#{tags.last}#{attrs_to_s(p_e[1])}>"
    when :end_element
      results << "</#{tags.pop}>"
    when :text
      results << p_e[0][0..new_len]
      new_len -= p_e[0].length
    else
      results << "<!-- #{p_e.inspect} -->"
    end
  end
  if at_end && (sanitize(text).try(:length).to_i) > len
    results << at_end
  end
  tags.reverse.each do |tag|
    results << "</#{tag}>"
  end
  raw(results)
end