Module: Errors2Html

Defined in:
lib/rails_errors2html.rb

Defined Under Namespace

Modules: Mixin

Constant Summary collapse

VERSION =
'1.4.0'

Class Method Summary collapse

Class Method Details

.dependenciesObject



8
9
10
11
12
13
14
# File 'lib/rails_errors2html.rb', line 8

def Errors2Html.dependencies
  {
    'fattr'      => [ 'fattr'         , ' >= 2.2.1' ],
    'map'        => [ 'map'           , ' >= 6.2.0' ],
    'rails_view' => [ 'rails_view'    , ' >= 1.0.1' ]
  }
end

.flatten(hashlike) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rails_errors2html.rb', line 74

def Errors2Html.flatten(hashlike)
  case hashlike
    when Map
      hash = Hash.new
      hashlike.depth_first_each do |key, value|
        index = key.pop if key.last.is_a?(Integer)
        (hash[key] ||= []).push(value)
      end
      hash
    else
      hashlike.respond_to?(:to_hash) ? hashlike.to_hash : hashlike
  end
end

.to_html(*args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails_errors2html.rb', line 27

def Errors2Html.to_html(*args)
  if args.size == 1
    case args.first
      when Array, String, Symbol
        messages = Array(args.first)
        args = [{:base => messages}]
    end
  end

  args.flatten!
  args.compact!

  at_least_one_error = false

  errors = Map.new
  errors[:global] = []
  errors[:fields] = {} 

  args.each do |e|
    flatten(e).each do |key, messages|
      Array(messages).each do |message|
        at_least_one_error = true

        if Array(key).join =~ /\A(?:[*]|base)\Z/iomx
          errors.global.push(message.to_s).uniq!
        else
          (errors.fields[key] ||= []).push(message.to_s).uniq!
        end
      end
    end
  end

  return "" unless at_least_one_error

  locals = {
    :errors => errors,
    :global_errors => errors.global,
    :fields_errors => errors.fields
  }

  if template
    View.render(:template => template, :locals => locals)
  else
    View.render(:inline => inline, :locals => locals)
  end
end

.versionObject



4
5
6
# File 'lib/rails_errors2html.rb', line 4

def Errors2Html.version
  Errors2Html::VERSION
end