Module: Mack::ViewHelpers::ActiveRecordHelpers

Defined in:
lib/mack-active_record/helpers/orm_helpers.rb

Constant Summary collapse

DEFAULT_PARTIAL =
%{
<div class="errorExplanation" id="errorExplanation">
  <h2><%= pluralize_word(errors.size, "error") %> occured.</h2>
  <ul>
    <% for error in errors %>
      <li><%= error %></li>
    <% end %>  
  </ul>
</div>}.strip

Instance Method Summary collapse

Instance Method Details

#error_messages_for(object_names = [], view_partial = nil) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mack-active_record/helpers/orm_helpers.rb', line 20

def error_messages_for(object_names = [], view_partial = nil)
  object_names = [object_names]
  object_names.flatten!
  app_errors = []
  object_names.each do |name|
    object = instance_variable_get("@#{name}")
    if object
      object.errors.each do |key, value|
        key = key.to_s
        if value.is_a?(Array)
          value.each do |v|
            if v.match(/^\^/)
              app_errors << v[1..v.length]
            else
              if key.class == String and key == "base"
                app_errors << "#{v}"
              else
                app_errors << "#{object.business_display_name} #{key.underscore.split('_').join(' ')} #{v}"
              end
            end
          end
        else
          if value.match(/^\^/)
            app_errors << value[1..value.length]
          else
            if key.class == String and key == "base"
              app_errors << "#{value}"
            else
              app_errors << "#{object.business_display_name} #{key.underscore.split('_').join(' ')} #{value}"
            end
          end
        end
      end
    end
  end
  unless app_errors.empty?
    app_errors.uniq!
    if view_partial.nil?
      if File.exist?(Mack::Paths.views("application", "_error_messages.html.erb"))
        render(:partial, "application/error_messages", :locals => {:errors => app_errors})
      else
        render(:inline, DEFAULT_PARTIAL, :locals => {:errors => app_errors})
      end
    else        
      render(:partial, view_partial, :locals => {:errors => app_errors})
    end
  else
    ""
  end
end