<commit_before>//= require select2
//= require_self

$(function() {
  setupSelect2(document);

  $(document).on('has_many_add:after', function(event, container){
    setupSelect2(container);
  });

  function setupSelect2(container) {
    var INVALID_PARENT_ID = -1;

    $('.select2-tags', container).each(function(i, el) {
      var model = $(el).data('model'),
          method = $(el).data('method'),
          selectOptions = {
            width: '80%',
            tags: $(el).data('collection')
          };

      if(!!model) {
        selectOptions.createSearchChoice = function() { return null; };
        var prefix = model + '_' + method;
        $(el).on('select2-selecting', onItemAdded);
        $(el).on('select2-removed', onItemRemoved);
      }

      $(el).select2(selectOptions);

      function onItemRemoved(event) {
        var itemId = '#' + prefix +  '_' + event.val;
        $(itemId).remove();
      }

      function onItemAdded(event) {
        var selectedItemsContainer = $('#' + prefix +  '_selected_values'),
            itemName = model + '[' + method + '][]',
            itemId = prefix + '_' + event.val;
<commit_msg>Fix select2 in case of namespaced AR model<commit_after>//= require select2
//= require_self

$(function() {
  setupSelect2(document);

  $(document).on('has_many_add:after', function(event, container){
    setupSelect2(container);
  });

  function setupSelect2(container) {
    var INVALID_PARENT_ID = -1;

    $('.select2-tags', container).each(function(i, el) {
      var model = $(el).data('model'),
          method = $(el).data('method'),
          selectOptions = {
            width: '80%',
            tags: $(el).data('collection')
          };

      if(!!model) {
        selectOptions.createSearchChoice = function() { return null; };
        var prefix = model + '_' + method;
        $(el).on('select2-selecting', onItemAdded);
        $(el).on('select2-removed', onItemRemoved);
      }

      $(el).select2(selectOptions);

      function onItemRemoved(event) {
        var itemId = '#' + prefix +  '_' + event.val;
        $(itemId).remove();
      }

      function onItemAdded(event) {
          var selectedItemsContainer = $("[id='" + prefix + "_selected_values']"),
            itemName = model + '[' + method + '][]',
            itemId = prefix + '_' + event.val;

{'fraction_non_alphanumeric': 0.17235494880546076, 'fraction_numerical': 0.009812286689419795, 'mean_word_length': 2.49478390461997, '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': '7654690', 'n_tokens_mistral': 720, 'n_tokens_neox': 703, 'n_words': 130}