Module: Virtuatable::Helpers::Fields

Included in:
Controllers::Base
Defined in:
lib/virtuatable/helpers/fields.rb

Overview

Helpers for the parameters of a request.

Author:

Instance Method Summary collapse

Instance Method Details

#check_either_presence(*fields, key:) ⇒ Object

Checks the presence of either fields given in parameters. It halts with an error only if ALL parameters are not given.

Parameters:

  • fields (Array<String>)

    an array of fields names to search in the parameters

  • key (String)

    the key to search in the errors configuration file.



21
22
23
24
25
# File 'lib/virtuatable/helpers/fields.rb', line 21

def check_either_presence(*fields, key:)
  api_bad_request "#{key}.required" if fields.none? do |field|
    field_defined?(field)
  end
end

#check_presence(*fields) ⇒ Object

Checks the presence of several fields given as parameters and halts the execution if it’s not present.

Parameters:

  • fields (Array<String>)

    an array of fields names to search in the parameters



10
11
12
13
14
# File 'lib/virtuatable/helpers/fields.rb', line 10

def check_presence(*fields)
  fields.each do |field|
    api_bad_request "#{field}.required" unless field_defined?(field)
  end
end

#field_defined?(field) ⇒ Boolean

Checks if a given field is defined in the params

Parameters:

  • field (String)

    the name of the field to check in the params

Returns:

  • (Boolean)

    TRUE if the field exists, FALSE otherwise.



30
31
32
# File 'lib/virtuatable/helpers/fields.rb', line 30

def field_defined?(field)
  !params.nil? && params.key?(field) && params[field] != ''
end