From 3e106dc7f5fbd6961c67f41aea66033c27b5142f Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 18 Mar 2023 12:15:43 -0600 Subject: AƱade spec de products_controller#show MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/products_controller.rb | 16 +++++++++------- app/models/product.rb | 8 -------- 2 files changed, 9 insertions(+), 15 deletions(-) (limited to 'app') diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index ec82af6..0c3a30b 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -4,9 +4,7 @@ module Api # ProductsController class ProductsController < MasterController def show - @product = Product.find_by(public_id: params[:id]) - - render json: not_found_error_message, status: :not_found and return if @product.nil? + render json: not_found_error_message, status: :not_found and return if product.nil? render json: serialized_object.serializable_hash, status: :ok end @@ -48,11 +46,15 @@ module Api private + def product + @product ||= Product.joins(:company) + .select('products.*', 'companies.name as company_name', + 'companies.short_name as company_short_name') + .find_by(public_id: params[:id]) + end + def serialized_object - Serializers::ProductSerializer.new( - @product.joins(:company) - .select('products.*', 'companies.name as company_name','companies.short_name as company_short_name') - ) + Serializers::ProductSerializer.new(product) end def serialized_collection diff --git a/app/models/product.rb b/app/models/product.rb index 6e76f75..e6f3720 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -21,17 +21,9 @@ class Product < ApplicationRecord has_one_attached :picture - # around_create :generate_public_id - serialize :categories, Array def to_param public_id end - - #private - - #def generate_public_id - # self.public_id = SecureRandom.hex(12) - #end end -- cgit v1.2.3