<commit_before>$(function() {
  $(document).on('turbolinks:render, turbolinks:load', function() {
    const getFieldsLabel = (field) => ($('label[for="'+field.id+'"]'));

    const showFieldWithLabel = (field) => {
      $(field).show();
      getFieldsLabel(field).show();
    }

    const hideFieldWithLabel = (field) => {
      $(field).hide();
      getFieldsLabel(field).hide();
    }

    const getFormGroupInputs = ({subject, model}) => (
      $('.form-group.' + model + '_' + subject + ' input[type="checkbox"],.form-group.' + model + '_' + subject + ' input[type="radio"]')
    );

    const getInputCollection = ({model, subject}) => (
      subject.map((formGroup) => (
        getFormGroupInputs({model, subject: formGroup})[0]
      ))
    );

    const reduceInputCollectionChecked = (inputs) => (
      inputs.reduce((first, second) => (
        ( ((typeof first === 'boolean') ? first : first.checked) || second.checked )
      ))
    );

    const handleInputGroupChange = ({inputs, field}) => {
      inputs.forEach((input) => {
        if(input.checked) {
          showFieldWithLabel(field);
        }
        $(input).bind('change', () => {
          if(reduceInputCollectionChecked(inputs)) {
            showFieldWithLabel(field);
          } else {
            hideFieldWithLabel(field);
          }
        });
      });
    }

    const showForCheckbox = ({field, subject, model}) => {
      getFormGroupInputs({subject, model}).each((key, input) => {
        if(input.checked) {
          showFieldWithLabel(field);
        }
        $(input).bind('change', ({target: {checked}}) => {
          if(checked) {
            showFieldWithLabel(field)
          } else {
            hideFieldWithLabel(field)
          }
        });
      });
    }

    const showForRadio = ({field, subject, model, value}) => {
      $('#' + model + '_' + subject + '_' + value).each((key, input) => {
        if(input.checked) {
          showFieldWithLabel(field);
        }
        $(input).bind('change', () => {
          if(input.checked) {
            showFieldWithLabel(field);
            $(input).off('change');
            getFormGroupInputs({subject, model}).bind('change', ({rem_target}) => {
              hideFieldWithLabel(field);
              $(rem_target).off('change');
              showForRadio({field, data: {subject, model, value}});
            });
          }
        });
      });
    }

    $('.conditional-field').each((key, field) => {
      const {subject, value, model} = $(field).data();
      if(value) {
        return showForRadio({field, subject, value, model});
      }
      showForCheckbox({field, subject, model});
    });

    $('input.conditional-group[type="checkbox"]').each((key, field) => {
      const {value, model, subject} = $(field).data();
      const inputs = getInputCollection({model, subject});
      handleInputGroupChange({inputs, field});
    });
  });
});
<commit_msg>use template strings in conditional field js selectors<commit_after>$(function() {
  $(document).on('turbolinks:render, turbolinks:load', function() {
    const getFieldsLabel = (field) => ($(`label[for="${field.id}"]`));

    const showFieldWithLabel = (field) => {
      $(field).show();
      getFieldsLabel(field).show();
    }

    const hideFieldWithLabel = (field) => {
      $(field).hide();
      getFieldsLabel(field).hide();
    }

    const getFormGroupInputs = ({subject, model}) => (
      $(`.form-group.${model}_${subject} input[type="checkbox"],.form-group.${model}_${subject} input[type="radio"]`)
    );

    const getInputCollection = ({model, subject}) => (
      subject.map((formGroup) => (
        getFormGroupInputs({model, subject: formGroup})[0]
      ))
    );

    const reduceInputCollectionChecked = (inputs) => (
      inputs.reduce((first, second) => (
        ( ((typeof first === 'boolean') ? first : first.checked) || second.checked )
      ))
    );

    const handleInputGroupChange = ({inputs, field}) => {
      inputs.forEach((input) => {
        if(input.checked) {
          showFieldWithLabel(field);
        }
        $(input).bind('change', () => {
          if(reduceInputCollectionChecked(inputs)) {
            showFieldWithLabel(field);
          } else {
            hideFieldWithLabel(field);
          }
        });
      });
    }

    const showForCheckbox = ({field, subject, model}) => {
      getFormGroupInputs({subject, model}).each((key, input) => {
        if(input.checked) {
          showFieldWithLabel(field);
        }
        $(input).bind('change', ({target: {checked}}) => {
          if(checked) {
            showFieldWithLabel(field)
          } else {
            hideFieldWithLabel(field)
          }
        });
      });
    }

    const showForRadio = ({field, subject, model, value}) => {
      $('#' + model + '_' + subject + '_' + value).each((key, input) => {
        if(input.checked) {
          showFieldWithLabel(field);
        }
        $(input).bind('change', () => {
          if(input.checked) {
            showFieldWithLabel(field);
            $(input).off('change');
            getFormGroupInputs({subject, model}).bind('change', ({rem_target}) => {
              hideFieldWithLabel(field);
              $(rem_target).off('change');
              showForRadio({field, data: {subject, model, value}});
            });
          }
        });
      });
    }

    $('.conditional-field').each((key, field) => {
      const {subject, value, model} = $(field).data();
      if(value) {
        return showForRadio({field, subject, value, model});
      }
      showForCheckbox({field, subject, model});
    });

    $('input.conditional-group[type="checkbox"]').each((key, field) => {
      const {value, model, subject} = $(field).data();
      const inputs = getInputCollection({model, subject});
      handleInputGroupChange({inputs, field});
    });
  });
});

{'fraction_non_alphanumeric': 0.1844248388191381, 'fraction_numerical': 0.00033932813030200206, 'mean_word_length': 2.3380520951302377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '7310481', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1677, 'n_words': 267}