diff options
author | HombreLaser <sebastian-440@live.com> | 2023-03-16 21:53:23 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-03-16 21:53:23 -0600 |
commit | 69153b46eee0fec67efa66f9b5f0499730a98829 (patch) | |
tree | f07d8de984911fcf6d684b06ae69516cc9aa2b43 | |
parent | ec60f7fb4be7213174f150318e418cdd8832a39b (diff) |
Mejorado código de ProductsController
-rw-r--r-- | app/controllers/api/companies_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/api/products_controller.rb | 11 | ||||
-rw-r--r-- | app/controllers/serializers/product_serializer.rb | 11 | ||||
-rw-r--r-- | app/models/product.rb | 10 | ||||
-rw-r--r-- | spec/controllers/products_controller_spec.rb | 13 | ||||
-rw-r--r-- | spec/factories/product.rb | 18 |
6 files changed, 49 insertions, 16 deletions
diff --git a/app/controllers/api/companies_controller.rb b/app/controllers/api/companies_controller.rb index 63d30c8..086ac5b 100644 --- a/app/controllers/api/companies_controller.rb +++ b/app/controllers/api/companies_controller.rb @@ -52,7 +52,7 @@ module Api end def not_found_error_message - { error_message: "No se encontró la compañía #{params[:short_name]}"} + { error_message: "No se encontró la compañía #{params[:short_name]}" } end end end diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index febb207..213fa9f 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -18,7 +18,7 @@ module Api end def create - @product = Product.new(permitted_params) + @product = Product.new(object_params) if @product.save render json: serialized_object.serializable_hash, status: :ok @@ -30,7 +30,7 @@ module Api def update @product = Product.find_by(public_id: params[:id]) - if @product.update(permitted_params) + if @product.update(object_params) render json: serialized_object.serializable_hash, status: :ok else render json: @product.errors.full_messages, status: :unprocessable_entity @@ -49,7 +49,7 @@ module Api private def serialized_object - Serializers::ProductSerializer(@product).new + Serializers::ProductSerializer.new(@product) end def serialized_collection @@ -59,5 +59,10 @@ module Api def permitted_params params.permit(:name, :unitary_price, :bulk_price, :picture, :available_quantity, :categories, :company_id) end + + def object_params + categories = permitted_params[:categories].split(',') + permitted_params.merge(categories:, public_id: SecureRandom.hex(12)) + end end end diff --git a/app/controllers/serializers/product_serializer.rb b/app/controllers/serializers/product_serializer.rb index 4a2158d..09310a0 100644 --- a/app/controllers/serializers/product_serializer.rb +++ b/app/controllers/serializers/product_serializer.rb @@ -5,17 +5,14 @@ module Serializers class ProductSerializer < BaseSerializer extend ActionView::RoutingUrlFor - attributes :name, :unitary_price, :bulk_price, :available_quantity, :categories + attributes :name, :unitary_price, :bulk_price, :available_quantity, :categories, :company attribute :picture do |object| object.picture.url end - belongs_to :company, links: { - self: :url, - related: lambda(object) { - company_path(object.company) - } - } + attribute :company do |object| + { name: object.company.name, short_name: object.company.short_name } + end end end
\ No newline at end of file diff --git a/app/models/product.rb b/app/models/product.rb index 87a7a71..6e76f75 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -21,7 +21,7 @@ class Product < ApplicationRecord has_one_attached :picture - around_create :generate_public_id + # around_create :generate_public_id serialize :categories, Array @@ -29,9 +29,9 @@ class Product < ApplicationRecord public_id end - private + #private - def generate_public_id - self.public_id = SecureRandom.hex(12) - end + #def generate_public_id + # self.public_id = SecureRandom.hex(12) + #end end diff --git a/spec/controllers/products_controller_spec.rb b/spec/controllers/products_controller_spec.rb new file mode 100644 index 0000000..d4f1373 --- /dev/null +++ b/spec/controllers/products_controller_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Api::ProductsController, type: :controller do + let(:product) { create(:product) } + + it { should route(:post, '/api/products').to(action: :create) } + it { should route(:put, "/api/products#{product.public_id}").to(action: :update, id: product.public_id) } + it { should route(:get, '/api/products').to(action: :index) } + it { should route(:get, "/api/products/#{product.public_id}").to(action: :show, id: product.public_id) } + it { should route(:delete, "/api/products/#{product.public_id}").to(action: :destroy, id: product.public_id) } +end diff --git a/spec/factories/product.rb b/spec/factories/product.rb new file mode 100644 index 0000000..ab65cd0 --- /dev/null +++ b/spec/factories/product.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :company, class: 'Company' do + name { Faker::Commerce.product_name } + unitary_price { rand(1000.0) } + bulk_price { rand(1000.0) } + available_quantity { rand(1000) } + company { create(:company) } + categories do + c = [] + (0..rand(5)).each do + c.push(Faker::Commerce.department(max: 1)) + end + c + end + end +end |