<commit_before>angular.module("ofn.admin").factory "BulkProducts", (PagedFetcher, dataFetcher) ->
  new class BulkProducts
    products: []

    fetch: (filters, onComplete) ->
      queryString = filters.reduce (qs,f) ->
        return qs + "q[#{f.property.db_column}_#{f.predicate.predicate}]=#{f.value};"
      , ""

      url = "/api/products/bulk_products?page=::page::;per_page=20;#{queryString}"
      PagedFetcher.fetch url, (data) => @addProducts data.products

    cloneProduct: (product) ->
      dataFetcher("/admin/products/" + product.permalink_live + "/clone.json").then (data) =>
        # Ideally we would use Spree's built in respond_override helper here to redirect the
        # user after a successful clone with .json in the accept headers
        # However, at the time of writing there appears to be an issue which causes the
        # respond_with block in the destroy action of Spree::Admin::Product to break
        # when a respond_overrride for the clone action is used.
        id = data.product.id
        dataFetcher("/api/products/" + id + "?template=bulk_show").then (newProduct) =>
          @addProducts [newProduct]

    updateVariantLists: (serverProducts, productsWithUnsavedVariants) ->
      for product in productsWithUnsavedVariants
        server_product = @findProductInList(product.id, serverProducts)
        product.variants = server_product.variants
        @loadVariantUnitValues product

    find: (id) ->
      @findProductInList id, @products

    findProductInList: (id, product_list) ->
      products = (product for product in product_list when product.id == id)
      if products.length == 0 then null else products[0]

    addProducts: (products) ->
      for product in products
        @unpackProduct product
        @products.push product

    unpackProduct: (product) -><commit_msg>insert clone after cloned product<commit_after>angular.module("ofn.admin").factory "BulkProducts", (PagedFetcher, dataFetcher) ->
  new class BulkProducts
    products: []

    fetch: (filters, onComplete) ->
      queryString = filters.reduce (qs,f) ->
        return qs + "q[#{f.property.db_column}_#{f.predicate.predicate}]=#{f.value};"
      , ""

      url = "/api/products/bulk_products?page=::page::;per_page=20;#{queryString}"
      PagedFetcher.fetch url, (data) => @addProducts data.products

    cloneProduct: (product) ->
      dataFetcher("/admin/products/" + product.permalink_live + "/clone.json").then (data) =>
        # Ideally we would use Spree's built in respond_override helper here to redirect the
        # user after a successful clone with .json in the accept headers
        # However, at the time of writing there appears to be an issue which causes the
        # respond_with block in the destroy action of Spree::Admin::Product to break
        # when a respond_overrride for the clone action is used.
        id = data.product.id
        dataFetcher("/api/products/" + id + "?template=bulk_show").then (newProduct) =>
          @insertProductAfter(product, newProduct)

    updateVariantLists: (serverProducts, productsWithUnsavedVariants) ->
      for product in productsWithUnsavedVariants
        server_product = @findProductInList(product.id, serverProducts)
        product.variants = server_product.variants
        @loadVariantUnitValues product

    find: (id) ->
      @findProductInList id, @products

    findProductInList: (id, product_list) ->
      products = (product for product in product_list when product.id == id)
      if products.length == 0 then null else products[0]

    addProducts: (products) ->
      for product in products
        @unpackProduct product
        @products.push product

    insertProductAfter: (product, newProduct) ->
      index = @products.indexOf(product)
      @products.splice(index + 1, 0, newProduct)

    unpackProduct: (product) ->
{'fraction_non_alphanumeric': 0.10794719130209682, 'fraction_numerical': 0.0025886616619207868, 'mean_word_length': 3.2322015334063527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, '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': '13740', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1084, 'n_words': 305}