Module: MiniDefender::ValidationHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/mini_defender/validation_helpers.rb

Instance Method Summary collapse

Instance Method Details

#field_errors(field) ⇒ Object



38
39
40
41
42
43
# File 'lib/mini_defender/validation_helpers.rb', line 38

def field_errors(field)
  # Transform Rails foo[bar] convention to foo.bar
  field = field.gsub('[', '.').gsub(']', '')

  validation_errors[field] || []
end

#has_validation_errors?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/mini_defender/validation_helpers.rb', line 12

def has_validation_errors?
  validation_errors.length > 0
end

#old_value(field, default = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mini_defender/validation_helpers.rb', line 20

def old_value(field, default = nil)
  # Transform Rails foo[bar] convention to foo.bar
  field = field.gsub('[', '.').gsub(']', '')
  field = field.split('.')

  result = old_values.deep_stringify_keys

  while (key = field.shift)
    key = key.to_i if Array === result

    if Hash === result[key] || Array === result[key] || field.empty?
      result = result[key]
    end
  end

  result || default
end

#old_valuesObject



16
17
18
# File 'lib/mini_defender/validation_helpers.rb', line 16

def old_values
  @old_values ||= flash[:old_values] || {}
end

#render_field_errors(field) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/mini_defender/validation_helpers.rb', line 45

def render_field_errors(field)
  unless block_given?
    raise ArgumentError, 'Expected a block.'
  end

  field_errors(field).each { |e| yield(e) }
end

#validation_errorsObject



8
9
10
# File 'lib/mini_defender/validation_helpers.rb', line 8

def validation_errors
  @validation_errors ||= flash[:validation_errors] || {}
end