Module: RailsCom::FormatHelper

Defined in:
app/helpers/rails_com/format_helper.rb

Instance Method Summary collapse

Instance Method Details

#ex_simple_format(text, html_options = {}, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/rails_com/format_helper.rb', line 18

def ex_simple_format(text, html_options={}, options={})
  text = '' if text.nil?
  text = text.dup
  start_tag = tag('p', html_options, true)
  text = sanitize(text) unless options[:sanitize] == false
  text = text.to_str
  text.gsub!(/\n?/, "</p>\n#{start_tag}")
  text.insert 0, start_tag
  text.html_safe.safe_concat("</p>")
end

#simple_format(text, html_options = {}, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/rails_com/format_helper.rb', line 29

def simple_format(text, html_options = {}, options = {})
  if text.is_a?(Hash)
    return simple_format_hash(text, html_options, options)
    #elsif text.is_a?(String)
    #return ex_simple_format(text, html_options, options)
  end

  if text.is_a?(Array)
    begin
      hash_text = text.to_h
      return simple_format_hash(hash_text, html_options, options)
    rescue TypeError
      return text.map { |t| (:p, t, html_options) }.join("\n").html_safe
    end
  end

  text = text.to_s

  super
end

#simple_format_hash(hash_text, html_options = {}, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/helpers/rails_com/format_helper.rb', line 4

def simple_format_hash(hash_text, html_options = {}, options = {})
  wrapper_tag = options.fetch(:wrapper_tag, :p)

  hash_text.map do |k, v|
    if k.to_s.rstrip.end_with?(':')
      text = k.to_s + ' ' + v.to_s
    else
      text = k.to_s + ': ' + v.to_s
    end

    (wrapper_tag, text, html_options)
  end.join("\n\n").html_safe
end