<commit_before>function Textbook(attributes){
  this.title = attributes.title
  this.id = attributes.id
  this.price = attributes.price
  this.inventory = attributes.inventory
  this.courses = attributes.courses
  this.adoptions = attributes.adoptions
}

Textbook.prototype.renderShow = function (){
  return Textbook.template(this)
}

function Course(attributes){
  this.name = attributes.name
  this.id = attributes.id
  this.enrollment = attributes.enrollment
}

Course.prototype.renderShow = function (){
  return Course.template(this)
}

function Adoption(attributes){
  this.id = attributes.id
  this.course_id = attributes.course_id
  this.textbook_id = attributes.textbook_id
  this.ordered = attributes.ordered
}

Adoption.prototype.renderButton = function (){
  return Adoption.template(this)
}

$(function() {

  //New Textbook Form ajax call
  $("form#new_textbook").on('submit', function(e){
    e.preventDefault()

    let $form = $(this)
    let action = $form.attr('action')
    let params = $form.serialize()
    $.ajax({
      url: action,
      data: params,
      dataType: 'JSON',
      method: 'POST',
      success: function (json) {
        let $div = $('div#main')
        let textbook = new Textbook(json)
        let textbookShow = textbook.renderShow()
        
        $div.empty()
        $div.append(textbookShow)
        textbook.courses.forEach(element => {
          let course = new Course(element)
          let courseShow = course.renderShow()

          $('ul#textbook-courses').append(courseShow)
        })

        textbook.adoptions.forEach(element => {
          let adoption = new Adoption(element)
          let adoptionButton = adoption.renderButton()
          // debugger

          $(`#course_id_${adoption.course_id}`).after(adoptionButton)
        })
      },
      error: function (xhr) {
        let errors = $.parseJSON(xhr.responseText).errors
        alert(errors.join(', '))
        $('#new_textbook input:submit').prop('disabled', false)
      }
    }) 
  })

  //Welcome Page Textbook Index ajax call
  $('div#main').on('click', 'a.textbooks', function (event) {
    event.preventDefault()
    $.getJSON(this.href).success(function(data){
      let $div = $('div#textbooks')
      $div.empty()

      data.forEach(textbook => {
        $div.append(`<h3><a href='/textbooks/${textbook.id}'>${textbook.title}</a></h3>`)
      })
    })
  })

  //Welcome Page Course Index ajax call
  $('div#main').on('click', 'a.courses', function (event) {
    event.preventDefault()
    $.getJSON(this.href).success(function(data){
      let $div = $('div#courses')<commit_msg>refactors bettertext.js for clarity<commit_after>function Textbook(attributes){
  this.title = attributes.title
  this.id = attributes.id
  this.price = attributes.price
  this.inventory = attributes.inventory
  this.courses = attributes.courses
  this.adoptions = attributes.adoptions
}

Textbook.prototype.renderShow = function (){
  return Textbook.template(this)
}

function Course(attributes){
  this.name = attributes.name
  this.id = attributes.id
  this.enrollment = attributes.enrollment
}

Course.prototype.renderShow = function (){
  return Course.template(this)
}

function Adoption(attributes){
  this.id = attributes.id
  this.course_id = attributes.course_id
  this.textbook_id = attributes.textbook_id
  this.ordered = attributes.ordered
}

Adoption.prototype.renderButton = function (){
  return Adoption.template(this)
}

function createCoursePartial(element){
  let course = new Course(element)
  let courseShow = course.renderShow()

  $('ul#textbook-courses').append(courseShow)
}

function createAdoptionButton(element){
  let adoption = new Adoption(element)
  let adoptionButton = adoption.renderButton()

  $(`#course_id_${adoption.course_id}`).after(adoptionButton)

}

$(function() {

  //New Textbook Form ajax call
  $("form#new_textbook").on('submit', function(e){
    e.preventDefault()

    let $form = $(this)
    let action = $form.attr('action')
    let params = $form.serialize()
    $.ajax({
      url: action,
      data: params,
      dataType: 'JSON',
      method: 'POST',
      success: function (json) {
        let $div = $('div#main')
        let textbook = new Textbook(json)
        let textbookShow = textbook.renderShow()
        
        $div.empty()
        $div.append(textbookShow)
        textbook.courses.forEach(element => {
          createCoursePartial(element)
        })

        textbook.adoptions.forEach(element => {
          createAdoptionButton(element)
        })
      },
      error: function (xhr) {
        let errors = $.parseJSON(xhr.responseText).errors
        alert(errors.join(', '))
        $('#new_textbook input:submit').prop('disabled', false)
      }
    }) 
  })

  //Welcome Page Textbook Index ajax call
  $('div#main').on('click', 'a.textbooks', function (event) {
    event.preventDefault()
    $.getJSON(this.href).success(function(data){
      let $div = $('div#textbooks')
      $div.empty()

      data.forEach(textbook => {
        $div.append(`<h3><a href='/textbooks/${textbook.id}'>${textbook.title}</a></h3>`)
      })
    })
  })

  //Welcome Page Course Index ajax call
  $('div#main').on('click', 'a.courses', function (event) {
    event.preventDefault()
    $.getJSON(this.href).success(function(data){
      let $div = $('div#courses')
{'fraction_non_alphanumeric': 0.1469105842822475, 'fraction_numerical': 0.0007466865783087549, 'mean_word_length': 3.1215384615384614, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '3294083', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1639, 'n_words': 305}