Module: Brainstem::Concerns::ControllerDSL

Extended by:
ActiveSupport::Concern
Includes:
InheritableConfiguration
Included in:
Brainstem::ControllerMethods
Defined in:
lib/brainstem/concerns/controller_dsl.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_BRAINSTEM_PARAMS_CONTEXT =
:_default

Instance Method Summary collapse

Methods included from InheritableConfiguration

#configuration

Instance Method Details

#brainstem_valid_params(requested_context = action_name.to_sym, root_param_name = brainstem_model_name) ⇒ Hash{String => String, Hash] a hash of pairs of param names and Also known as: brainstem_valid_params_for

Lists all valid parameters for the current action. Falls back to the valid parameters for the default context.

descriptions or sub-hashes.

Returns:

  • (Hash{String => String, Hash] a hash of pairs of param names and)

    Hash{String => String, Hash] a hash of pairs of param names and



378
379
380
# File 'lib/brainstem/concerns/controller_dsl.rb', line 378

def brainstem_valid_params(requested_context = action_name.to_sym, root_param_name = brainstem_model_name)
  valid_params_tree(requested_context)[root_param_name.to_s]
end

#transforms(requested_context = action_name.to_sym) ⇒ Hash{Symbol => Symbol} Also known as: transforms_for

Lists all incoming param keys that will be rewritten to use a different name for internal usage for the current action.

Rewrites all params to be symbols for backwards compatibility.

Returns:

  • (Hash{Symbol => Symbol})

    a map of incoming => internal param names.



395
396
397
398
399
400
401
# File 'lib/brainstem/concerns/controller_dsl.rb', line 395

def transforms(requested_context = action_name.to_sym)
  tforms = contextual_key(requested_context, :transforms).to_h
  tforms.inject({}) do |memo, (k, v)|
    memo[k.to_sym] = v.to_sym
    memo
  end
end

#valid_params_tree(requested_context = action_name.to_sym) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/brainstem/concerns/controller_dsl.rb', line 347

def valid_params_tree(requested_context = action_name.to_sym)
  contextual_key(requested_context, :valid_params)
    .to_h
    .inject(ActiveSupport::HashWithIndifferentAccess.new) do |hsh, (field_name_proc, field_config)|

    field_name = field_name_proc.call(self.class)
    if field_config.has_key?(:ancestors)
      ancestors = field_config[:ancestors].map { |ancestor_key| ancestor_key.call(self.class) }
      parent = ancestors.inject(hsh) do |traversed_hash, ancestor_name|
        traversed_hash[ancestor_name] ||= {}
        traversed_hash[ancestor_name]
      end

      parent[field_name] = { :_config => field_config.except(:root, :ancestors) }
    else
      hsh[field_name] = { :_config => field_config }
    end

    hsh
  end
end