<commit_before>require "actioncontroller-parameter_filter/version"

# When a controller has ParameterFilter included, it will by default remove
# everything from params. The way to receive parameters is to specifically
# allow them with accept_fields.

module ActionController

  # accept_fields user: [ :email, :password ]
  # accept_fields { user: { company: [ :name, :address ] }, on: [ :update, :create ]

  module ParameterFilter

    module ClassMethods

      def accept_fields_parser fields
        table = {}

        [fields].flatten.compact.uniq.each do |field|
          case field

          when Symbol, String
            table[field.to_s] = {}

          when Hash
            field.each do |key, value|
              table[key.to_s] = accept_fields_parser value
            end

          end
        end

        table
      end

      def accepts options = {}
        @_accepted_fields ||= { nil: { controller: {}, action: {}, id: {} } }
        fields = options[:fields] || options[:field] || {}

        case options[:on]
        when Array
          options[:on].each do |k|
            @_accepted_fields[k.to_s] = accept_fields_parser fields
          end

        when Symbol, String
          @_accepted_fields[options[:on].to_s] = accept_fields_parser fields

        else
          @_accepted_fields[nil] ||= {}
          @_accepted_fields[nil].merge! accept_fields_parser fields

        end
      end

    end

    module InstanceMethods

      def remove_filtered_parameters accepted_fields = nil, parameters = nil
        if !accepted_fields && !parameters
          accepted_fields = self.class.instance_variable_get("@_accepted_fields")
          raise [ :af, accepted_fields ].inspect
          fields = (accepted_fields[nil] || {}).merge(accepted_fields[self.action_name] || {})
          remove_filtered_parameters fields, self.params<commit_msg>Remove debug code before releasing. Duh.<commit_after>require "actioncontroller-parameter_filter/version"

# When a controller has ParameterFilter included, it will by default remove
# everything from params. The way to receive parameters is to specifically
# allow them with accept_fields.

module ActionController

  # accept_fields user: [ :email, :password ]
  # accept_fields { user: { company: [ :name, :address ] }, on: [ :update, :create ]

  module ParameterFilter

    module ClassMethods

      def accept_fields_parser fields
        table = {}

        [fields].flatten.compact.uniq.each do |field|
          case field

          when Symbol, String
            table[field.to_s] = {}

          when Hash
            field.each do |key, value|
              table[key.to_s] = accept_fields_parser value
            end

          end
        end

        table
      end

      def accepts options = {}
        @_accepted_fields ||= { nil: { controller: {}, action: {}, id: {} } }
        fields = options[:fields] || options[:field] || {}

        case options[:on]
        when Array
          options[:on].each do |k|
            @_accepted_fields[k.to_s] = accept_fields_parser fields
          end

        when Symbol, String
          @_accepted_fields[options[:on].to_s] = accept_fields_parser fields

        else
          @_accepted_fields[nil] ||= {}
          @_accepted_fields[nil].merge! accept_fields_parser fields

        end
      end

    end

    module InstanceMethods

      def remove_filtered_parameters accepted_fields = nil, parameters = nil
        if !accepted_fields && !parameters
          accepted_fields = self.class.instance_variable_get("@_accepted_fields")
          fields = (accepted_fields[nil] || {}).merge(accepted_fields[self.action_name] || {})
          remove_filtered_parameters fields, self.params
{'fraction_non_alphanumeric': 0.09380010689470872, 'fraction_numerical': 0.0, 'mean_word_length': 2.4089253187613844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3856593', 'n_tokens_mistral': 1032, 'n_tokens_neox': 997, 'n_words': 264}