Sha256: 3f19f2e80de7b435e176e1007c3ff708f35e2c6b8a4774ae260960dae8280742

Contents?: true

Size: 1.56 KB

Versions: 10

Compression:

Stored size: 1.56 KB

Contents

# author: Vadim Shaveiko <@vshaveyko>
# frozen_string_literal: true
module RailsApiDoc::Controller::StrongParams

  def resource_params
    # accepted_params for permit
    accepted_params = [{}]
    loop_params(params, permitted_params, accepted_params)
    params.permit(accepted_params)
  end

  private

  # loop through current level of params and add to permit level if
  # all requirements met
  # requirements are: 1) if required is set - param must be present and not empty
  #                   2) if enum is set - param must equal predefined value
  #                   3) if config.check_params_type is set - param must be of required type
  # @accepted_params = [{}] - array with last member hash for nesting
  # @level_params - current nesting level params
  # @level_permitted_params - data for params permission
  def loop_params(nested_controller_params, level_permitted_params, accepted_params)
    level_permitted_params.each do |param_name, api_param_data|
      controller_param = nested_controller_params[param_name]

      next unless RailsApiDoc::Config::Validator.valid_param?(controller_param, api_param_data)

      if api_param_data.nested?
        level_accepted_params = accepted_params.last[param_name] = [{}]
        next loop_params(controller_param, api_param_data, level_accepted_params)
      else
        accepted_params.unshift(param_name)
      end
    end
  end

  def permitted_params
    Parameter::Repository[self]
  end

  def check_required_ok?(param_data, param_config)
    return true unless param_config.required?
    !param_data.blank?
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rails_api_documentation-0.1.9 lib/rails_api_doc/controller/strong_params.rb
rails_api_documentation-0.1.8 lib/rails_api_doc/controller/strong_params.rb
rails_api_documentation-0.1.7 lib/rails_api_doc/controller/strong_params.rb
rails_api_documentation-0.1.6 lib/rails_api_doc/controller/strong_params.rb
rails_api_documentation-0.1.5 lib/rails_api_doc/controller/strong_params.rb
rails_api_documentation-0.1.4 lib/rails_api_doc/controller/strong_params.rb
rails_api_documentation-0.1.3 lib/rails_api_doc/controller/strong_params.rb
rails_api_documentation-0.1.2 lib/rails_api_doc/controller/strong_params.rb
rails_api_documentation-0.1.1 lib/rails_api_doc/controller/strong_params.rb
rails_api_documentation-0.1.0 lib/rails_api_doc/controller/strong_params.rb