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



523
524
525
# File 'lib/brainstem/concerns/controller_dsl.rb', line 523

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.



539
540
541
542
543
544
545
# File 'lib/brainstem/concerns/controller_dsl.rb', line 539

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



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/brainstem/concerns/controller_dsl.rb', line 492

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