<commit_before>module ActiveModel
  module MassAssignmentSecurity

    protected

    def mass_assignment_authorizer(roles)
      active_authorizer = self.class.active_authorizer

      roles_array = if roles.is_a? Array
        roles
      elsif roles.is_a?(String) or roles.is_a?(Symbol)
        [roles]
      elsif roles.respond_to? :roles
        roles.send :roles
      end

      if roles_array
        roles_array = roles_array.compact.map(&:to_sym)
        mixed_role_name = roles_array.sort.join('_').to_sym
      end

      if roles_array and active_authorizer.has_key?(mixed_role_name)
        active_authorizer[mixed_role_name]

      elsif roles_array and has_attr_configs_for?(roles_array)
        build_new_attrs_config roles_array, mixed_role_name
        self.class.active_authorizer[mixed_role_name]

      else
        active_authorizer[:default]
      end
    end

    private

    def has_attr_configs_for?(roles_array)
      roles_array.all? { |role_name| self.class.active_authorizer.has_key? role_name }
    end

    def build_new_attrs_config(roles_array, new_role_name)
      fields = roles_array.inject([]) do |result, role_name|
        result += self.class._accessible_attributes[role_name].to_a
      end

      self.class.attr_accessible(*fields, :as => new_role_name)
    end
  end
end<commit_msg>refactoring<commit_after>module ActiveModel
  module MassAssignmentSecurity

    protected

    def mass_assignment_authorizer(roles)
      active_authorizer = self.class.active_authorizer

      composite_role_name, roles_array = compile_role_name roles, active_authorizer

      if !composite_role_name
        active_authorizer[:default]

      elsif active_authorizer.has_key? composite_role_name
        active_authorizer[composite_role_name]

      else
        build_new_attrs_config roles_array, composite_role_name
        self.class.active_authorizer[composite_role_name]
      end
    end

    private

    def compile_role_name(roles, active_authorizer)
      roles_array = if roles.is_a? Array
        roles
      elsif roles.respond_to? :roles
        [roles.send(:roles)].flatten
      else
        [roles]
      end

      roles_array = roles_array.find_all { |role| active_authorizer.has_key? role }

      if roles_array.any?
        [ roles_array.map(&:to_s).join('_').to_sym, roles_array ]
      else
        [ nil, roles_array ]
      end
    end

    def build_new_attrs_config(roles_array, new_role_name)
      fields = roles_array.inject([]) do |result, role_name|
        result += self.class.active_authorizer[role_name].to_a
      end

      method_name = if self.class.active_authorizer[ roles_array[0] ].is_a? ActiveModel::MassAssignmentSecurity::WhiteList
        :attr_accessible
      else
        :attr_protected
      end

      self.class.send method_name, *fields, :as => new_role_name
    end
  end
end
{'fraction_non_alphanumeric': 0.07322175732217573, 'fraction_numerical': 0.0003486750348675035, 'mean_word_length': 3.018207282913165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1565378', 'n_tokens_mistral': 930, 'n_tokens_neox': 914, 'n_words': 160}