Sha256: 30df3f576eaaa229d81c970add03997fb59fa9d893acb2ccc0bb36de8f4078c5

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module AbstractController
  class Base
    if defined? ActionController::StrongParameters
      def send_action(method_name, *args)
        return send method_name, *args unless args.empty?

        target_model_name = self.class.name.sub(/Controller$/, '').singularize.underscore.to_sym
        permitted_attributes = self.class.instance_variable_get '@permitted_attributes'
        values = method(method_name).parameters.reject {|type, _| type == :block }.map do |type, key|
          params.require key if type == :req
          if (key == target_model_name) && permitted_attributes
            params[key].try :permit, *permitted_attributes
          else
            params[key]
          end
        end
        send method_name, *values
      end

      # You can configure StrongParameters' `permit` attributes using this DSL method.
      # The `permit` call will be invoked only against parameters having the resource
      # model name inferred from the controller class name.
      #
      #   class UsersController < ApplicationController
      #     permits :name, :age
      #
      #     def create(user)
      #       @user = User.new(user)
      #     end
      #   end
      #
      def self.permits(*attributes)
        @permitted_attributes = attributes
      end
    # no StrongParameters
    else
      def send_action(method_name, *args)
        return send method_name, *args unless args.empty?

        values = method(method_name).parameters.reject {|type, _| type == :block }.map {|_, key| params[key]}
        send method_name, *values
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
action_args-1.0.4 lib/action_args/abstract_controller.rb
action_args-1.0.3 lib/action_args/abstract_controller.rb