module Brainstem::Concerns::ControllerDSL

Constants

DEFAULT_BRAINSTEM_PARAMS_CONTEXT
DYNAMIC_KEY

Public Instance Methods

brainstem_valid_params(requested_context = action_name.to_sym, root_param_name = brainstem_model_name) click to toggle source

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

@params [Symbol] requested_context the context which to look up.

@return [Hash{String => String, Hash] a hash of pairs of param names and descriptions or sub-hashes.

# File lib/brainstem/concerns/controller_dsl.rb, line 593
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
Also aliased as: brainstem_valid_params_for
brainstem_valid_params_for(requested_context = action_name.to_sym, root_param_name = brainstem_model_name)
transforms(requested_context = action_name.to_sym) click to toggle source

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.

@params [Symbol] requested_context the context which to look up.

@return [Hash{Symbol => Symbol}] a map of incoming => internal

param names.
# File lib/brainstem/concerns/controller_dsl.rb, line 609
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
Also aliased as: transforms_for
transforms_for(requested_context = action_name.to_sym)
Alias for: transforms
valid_params_tree(requested_context = action_name.to_sym) click to toggle source
# File lib/brainstem/concerns/controller_dsl.rb, line 562
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

Private Instance Methods

contextual_key(context, key) click to toggle source

Retrieves a specific key in a given context, or if that doesn't exist, falls back to the parent context.

@private

@params [Symbol] context the context in which to first look for the key @params [Symbol] key the key name to look for

# File lib/brainstem/concerns/controller_dsl.rb, line 627
def contextual_key(context, key)
  if configuration.has_key?(context.to_sym)
    configuration[context.to_sym][key.to_sym]
  else
    configuration[DEFAULT_BRAINSTEM_PARAMS_CONTEXT][key.to_sym]
  end
end