Browse Source

Removed some duplicated code (variant search)

Jacqueline Maldonado 7 years ago
parent
commit
499d324fa9
1 changed files with 20 additions and 56 deletions
  1. 20 56
      app/controllers/application_controller.rb

+ 20 - 56
app/controllers/application_controller.rb

@@ -86,7 +86,7 @@ class ApplicationController < ActionController::Base
         query_array.shift # delete the name of the product from the array to iterate the attributes
         attrs_query_string = ''
         query_array.each do |attribute|
-          next unless attribute.present?
+          next if attribute.nil?
           attr_type =
             case attribute[0]
             when 'c'
@@ -123,75 +123,39 @@ class ApplicationController < ActionController::Base
       else
         Pointsale.find(current_user.pointsale_id).products
       end
-
-    if query.include? ':' # search with attributes
-      query_array = query.split(':')
-      product_name = query_array[0]
-      query_array.shift # delete the name of the product from the array to iterate the attributes
-      attrs_query_string = ''
-      query_array.each do |attribute|
-        next if attribute.nil?
-        attr_type =
-          case attribute[0]
-          when 'c'
-            'colors'
-          when 't'
-            'sizes'
-          when 'e'
-            'styles'
-          end
-        attribute[0] = "" # delete the attribute type character
-        attrs_query_string.concat(" AND attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
+    attributes = query_for_variants(params[:query])
+    consult =
+      if query.include? ':'
+        location.name_sku_barcode_attribute_like(@product_name, attributes)
+      else
+        location.name_sku_barcode_like(params[:query])
       end
-      consult = location.name_sku_barcode_attribute_like(product_name, attrs_query_string)
-    else
-      product_name = query
-      consult = location.name_sku_barcode_like(params[:query])
-    end
 
     render json: consult.where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
   end
 
-  # rubocop:disable Metrics/BlockNesting
   def find_from_stock_by_pointsale
     if params[:pointsale_id].present?
       id = params[:pointsale_id][2, params[:pointsale_id].length]
+      attributes = query_for_variants(params[:query])
       query = params[:query]
-      if query.include? ':' # search with attributes
-        query_array = query.split(':')
-        product_name = query_array[0]
-        query_array.shift # delete the name of the product from the array to iterate the attributes
-        attrs_query_string = ''
-        query_array.each do |attribute|
-          if attribute.present?
-            attr_type = case attribute[0]
-                        when 'c'
-                          'colors'
-                        when 't'
-                          'sizes'
-                        when 'e'
-                          'styles'
-                        end
-            attribute[0] = "" # delete the attribute type character
-            attrs_query_string.concat(" AND attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
-          else
-            next
-          end
+      location =
+        if params[:pointsale_id].first == "P"
+          Pointsale.find(id).products
+        else
+          Warehouse.find(id).products
         end
-      else
-        product_name = query
-      end
-
-      if params[:pointsale_id].first == 'P'
-        render json: query.include?(":") ? Pointsale.find(id).products.name_sku_barcode_attribute_like(product_name, attrs_query_string).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes]) : Pointsale.find(id).products.name_sku_barcode_like(params[:query]).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
-      else
-        render json: query.include?(":") ? Warehouse.find(id).products.name_sku_barcode_attribute_like(product_name, attrs_query_string).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes]) : Warehouse.find(id).products.name_sku_barcode_like(params[:query]).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
-      end
+      consult =
+        if query.include?(":")
+          location.name_sku_barcode_attribute_like(@product_name, attributes)
+        else
+          location.name_sku_barcode_like(params[:query])
+        end
+      render json: consult.where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
     else
       render json: {}
     end
   end
-  # rubocop:enable Metrics/BlockNesting
 
   def get_subcategories
     render json: params[:category_id] != '0' ? Category.activos.where("parent_id = ?", params[:category_id]) : Category.activos.where('parent_id != 0')