Module: ActiveInteraction::Extras::StrongParams

Extended by:
ActiveSupport::Concern
Included in:
All
Defined in:
lib/active_interaction/extras/strong_params.rb

Instance Method Summary collapse

Instance Method Details

#initialize(inputs = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_interaction/extras/strong_params.rb', line 4

def initialize(inputs = {})
  # TODO: whitelist :params and :form_params, so they could not be used as filters
  return super if self.class.filters.key?(:params) || self.class.filters.key?(:form_params)

  return super if %i[fetch key? merge].any? { |m| !inputs.respond_to?(m) }

  if inputs.key?(:params) && inputs.key?(:form_params)
    raise ArgumentError, 'Both options :params and :form_params are given. ' \
    'One or none are accepted.'
  end

  form_params = inputs.fetch(:form_params) do
    params = inputs[:params]
    if params
      key = to_model&.model_name&.param_key
      params.require(key) if params.respond_to?(:require) && params.key?(key)
    end
  end

  if form_params
    inputs = inputs.merge(form_params.permit(self.class.permitted_params))
  end

  super(inputs)
end