Module: Core::Helpers::Fields

Included in:
Controllers::Base
Defined in:
lib/core/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.



21
22
23
24
25
# File 'lib/core/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.



10
11
12
13
14
# File 'lib/core/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



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

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