Module: RailsCom::FormatHelper

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

Instance Method Summary collapse

Instance Method Details

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/rails_com/format_helper.rb', line 18

def simple_format(text, html_options = {}, options = {})
  if text.is_a?(Hash)
    return simple_format_hash(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