Module: SimpleModel::ErrorHelpers

Included in:
Base
Defined in:
lib/simple_model/error_helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errors_countObject

Returns the value of attribute errors_count.



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

def errors_count
  @errors_count
end

Instance Method Details

#create_error_list(key, value) ⇒ Object

Allow for nested errors like one might see in nested assets when using accepts_nested_attributes and nested forms



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
# File 'lib/simple_model/error_helpers.rb', line 35

def create_error_list(key,value)
  error_items = ""
  if value.is_a?(Array)
    value.uniq!
    if value.length == 1
      self.errors_count = (self.errors_count.to_i + 1)
      error_items << "<li>#{key.to_s.titleize} #{value[0]}</li>"
    else
      error_items << "<li><ul>#{key.to_s.titleize} errors:"
      value.each do |item|
        if item.is_a?(Hash)
          new_value = item.to_a[0]
          error_items << create_error_list(new_value[0],new_value[1])
        else
          self.errors_count = (self.errors_count.to_i + 1)
          error_items << "<li>#{key.to_s.titleize} #{item}</li>"
        end
      end
      error_items << "</ul></li>"
    end
  elsif value.is_a?(Hash)
    error_items << "<li><ul>#{key.to_s.titleize} error:"
    error_items << create_error_list(value[0],value[1])
    error_items << "</ul></li>"
  else
    self.errors_count = (self.errors_count.to_i + 1)
    error_items << "<li>#{key.to_s.titleize} #{value}</li>"
  end
  error_items
end

#errors?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/simple_model/error_helpers.rb', line 6

def errors?
  !self.errors.blank?
end

#errors_for_flash(options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simple_model/error_helpers.rb', line 11

def errors_for_flash(options={})
  #set defaults and overwrite
  options = {:failed_action => "saving",
             :id => 'errorExplanation',
             :classes =>  ''}.merge!(options)

  error_list  = ""

  # The active_model errors object is not a normal hash and the each method
  # for the active_mode errors object does not perform as expected
  # so make it a plain hash
  {}.merge!(self.errors).each do |error,message|
    error_list << create_error_list(error,message)
  end
  error_string = "<div id='#{options[:id]}' class='#{options[:classes]}'><h2>#{self.errors_count}"
  error_string << " #{puralize_errors_string(self.errors)}"
  error_string << " prevented #{options[:failed_action]}.</h2><ul>"
  error_string << error_list
  error_string << "</ul></div>"
  error_string
end

#errors_to_sObject



73
74
75
76
77
78
79
# File 'lib/simple_model/error_helpers.rb', line 73

def errors_to_s
  error_string = ""
  self.errors.full_messages.each do |m|
    error_string << "#{m} "
  end
  error_string
end

#puralize_errors_string(array) ⇒ Object



66
67
68
69
70
71
# File 'lib/simple_model/error_helpers.rb', line 66

def puralize_errors_string(array)
  array = array.to_a if array.is_a?(ActiveModel::Errors)
  s = "error"
  s << "s" if array.length > 1
  s
end