Module: ModelErrorMessages::PrivateHelpers

Defined in:
lib/model_error_messages/helpers.rb

Class Method Summary collapse

Class Method Details

.errors_list(model, config) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/model_error_messages/helpers.rb', line 36

def self.errors_list(model, config)
  messages = model.errors.full_messages

  if messages.count == 1 && config.single_error_in_paragraph
    return tag(:p, messages.first)
  end

  ul_string = messages.map do |message|
    tag(:li, message)
  end.join.html_safe

  tag(:ul, ul_string)
end

.errors_wrapper(model, config) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/model_error_messages/helpers.rb', line 16

def self.errors_wrapper(model, config)
  class_attr = wrapper_class_attr(model, config)

  div_string = [
    config.prepend_html,
    errors_list(model, config),
    config.append_html
  ].join.html_safe

  tag(:div, div_string, class: class_attr)
end

.local_config(options) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/model_error_messages/helpers.rb', line 50

def self.local_config(options)
  config = ModelErrorMessages.configuration.clone
  options.each_pair do |k, v|
    config.send(k.to_s + '=', v)
  end
  config
end

.tag(name, value, attributes = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/model_error_messages/helpers.rb', line 58

def self.tag(name, value, attributes = {})
  string_attributes = attributes.inject('') do |attrs, pair|
    unless pair.last.nil?
      attrs << %( #{pair.first}="#{CGI.escapeHTML(pair.last.to_s)}")
    end
    attrs
  end
  "<#{name}#{string_attributes}>#{value}</#{name}>"
end

.wrapper_class_attr(model, config) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/model_error_messages/helpers.rb', line 28

def self.wrapper_class_attr(model, config)
  if config.classes.is_a?(Proc)
    config.classes.call(model)
  else
    config.classes
  end
end