Sha256: 43f3bec93de905d4f3ccab6db612c5b4914106cc1a973e7448d2e8231951144f

Contents?: true

Size: 1.37 KB

Versions: 23

Compression:

Stored size: 1.37 KB

Contents

module ParagraphHelper

  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

  # required for truncating html
  require 'rexml/parsers/pullparser'
  
  # Return truncating in html raw format
  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
  
  private

    def attrs_to_s(attrs)
      if attrs.empty?
        ''
      else
        ' ' + attrs.to_a.map { |attr| %{#{attr[0]}="#{attr[1]}"} }.join(' ')
      end
    end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
rich_table_component-0.0.30 app/helpers/paragraph_helper.rb
rich_table_component-0.0.29 app/helpers/paragraph_helper.rb
rich_table_component-0.0.28 app/helpers/paragraph_helper.rb
rich_table_component-0.0.27 app/helpers/paragraph_helper.rb
rich_table_component-0.0.26 app/helpers/paragraph_helper.rb
rich_table_component-0.0.24 app/helpers/paragraph_helper.rb
rich_table_component-0.0.22 app/helpers/paragraph_helper.rb
rich_table_component-0.0.21 app/helpers/paragraph_helper.rb
rich_table_component-0.0.18 app/helpers/paragraph_helper.rb
rich_table_component-0.0.17 app/helpers/paragraph_helper.rb
rich_table_component-0.0.16 app/helpers/paragraph_helper.rb
rich_table_component-0.0.15 app/helpers/paragraph_helper.rb
rich_table_component-0.0.14 app/helpers/paragraph_helper.rb
rich_table_component-0.0.13 app/helpers/paragraph_helper.rb
rich_table_component-0.0.10 app/helpers/paragraph_helper.rb
rich_table_component-0.0.9 app/helpers/paragraph_helper.rb
rich_table_component-0.0.8 app/helpers/paragraph_helper.rb
rich_table_component-0.0.7 app/helpers/paragraph_helper.rb
rich_table_component-0.0.6 app/helpers/paragraph_helper.rb
rich_table_component-0.0.5 app/helpers/paragraph_helper.rb