Class: Reform::Contract::Result::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/reform/errors.rb

Overview

Provides the old API for Rails and friends. Note that this might become an optional “deprecation” gem in Reform 3.

Constant Summary collapse

DottedErrors =

PROTOTYPING. THIS WILL GO TO A SEPARATE GEM IN REFORM 2.4/3.0.

->(form, prefix, hash) do
  result = form.to_result
  result.messages.collect { |k, v| hash[[*prefix, k].join(".").to_sym] = v }

  form.schema.each(twin: true) do |dfn|
    Disposable::Twin::PropertyProcessor.new(dfn, form).() do |frm, i|
      form_obj = i ? form.send(dfn[:name])[i] : form.send(dfn[:name])
      DottedErrors.(form_obj, [*prefix, dfn[:name]], hash)
    end
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(result, form) ⇒ Errors

Returns a new instance of Errors.



4
5
6
7
8
9
10
# File 'lib/reform/errors.rb', line 4

def initialize(result, form)
  @result        = result # DISCUSS: we don't use this ATM?
  @form          = form
  @dotted_errors = {} # Reform does not endorse this style of error msgs.

  DottedErrors.(@form, [], @dotted_errors)
end

Instance Method Details

#[](name) ⇒ Object



36
37
38
# File 'lib/reform/errors.rb', line 36

def [](name)
	@dotted_errors[name] || []
end

#add(key, error_test) ⇒ Object

we need to delegate adding error to result because every time we call form.errors a new instance of this class is created so we need to update the @results array to be able to add custom errors here. This method will actually work only AFTER a validate call has been made



53
54
55
# File 'lib/reform/errors.rb', line 53

def add(key, error_test)
  @result.add_error(key, error_test)
end

#empty?Boolean

needed for rails form helpers

Returns:

  • (Boolean)


45
46
47
# File 'lib/reform/errors.rb', line 45

def empty?
  messages.empty?
end

#full_messagesObject



29
30
31
32
33
34
# File 'lib/reform/errors.rb', line 29

def full_messages
 @dotted_errors.collect { |path, errors|
  human_field = path.to_s.gsub(/([\.\_])+/, " ").gsub(/(\b\w)+/) { |s| s.capitalize }
	 errors.collect { |message| "#{human_field} #{message}" }
}.flatten
end

#messages(*args) ⇒ Object



25
26
27
# File 'lib/reform/errors.rb', line 25

def messages(*args)
  @dotted_errors
end

#sizeObject



40
41
42
# File 'lib/reform/errors.rb', line 40

def size
  messages.size
end