Skip to content

Commit

Permalink
Merge pull request #378 from marlena-b/be-validation-for-supply-product
Browse files Browse the repository at this point in the history
BE validation for supply product
  • Loading branch information
marlena-b authored Aug 19, 2024
2 parents b432b09 + 2053208 commit 360e274
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
27 changes: 26 additions & 1 deletion rails_application/app/controllers/supplies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
class SuppliesController < ApplicationController
class SupplyForm
include ActiveModel::Model
include ActiveModel::Validations

attr_reader :product_id, :quantity

def initialize(params)
@product_id = params[:product_id]
@quantity = params[:quantity]
end

validates :product_id, presence: true
validates :quantity, presence: true, numericality: { only_integer: true, greater_than: 0, allow_blank: true }
end

def new
@product_id = params[:product_id]
end

def create
supply(params[:product_id], params[:quantity])
supply_form = SupplyForm.new(supply_params)

unless supply_form.valid?
return render "new", locals: { errors: supply_form.errors }, status: :unprocessable_entity
end

supply(supply_form.product_id, supply_form.quantity.to_i)
redirect_to products_path, notice: "Stock level changed"
end

Expand All @@ -15,4 +36,8 @@ def supply(product_id, quantity)
Inventory::Supply.new(product_id: product_id, quantity: quantity)
)
end

def supply_params
params.permit(:product_id, :quantity)
end
end
12 changes: 11 additions & 1 deletion rails_application/app/views/supplies/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,15 @@
<label for="quantity" class="block font-bold">
Quantity
</label>
<%= number_field_tag :quantity, nil, min: 1, step: 1, id: "quantity", class: "mt-1 focus:ring-blue-500 focus:border-blue-500 block shadow-sm sm:text-sm border-gray-300 rounded-md" %>
<%= number_field_tag :quantity, nil, min: 1, step: 1, id: "quantity",
class: "mt-1 focus:ring-blue-500 focus:border-blue-500 block shadow-sm sm:text-sm border-gray-300 rounded-md",
data: { turbo_permanent: true } %>

<% if defined?(errors) %>
<% errors.each do |error| %>
<div class="mt-2 text-red-600">
<span><%= error.full_message %></span>
</div>
<% end %>
<% end %>
<% end %>
67 changes: 67 additions & 0 deletions rails_application/test/integration/supplies_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "test_helper"

class SuppliesTest < InMemoryRESIntegrationTestCase
def test_happy_path
product_id = register_product("Async Remote", 100, 10)

get "/products/#{product_id}/supplies/new"
assert_select "h1", "Supply"
post "/products/#{product_id}/supplies",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"quantity" => "1"
}
follow_redirect!

assert_select "#notice", "Stock level changed"
end

def test_renders_validation_error_when_quantity_is_not_present
product_id = register_product("Async Remote", 100, 10)

get "/products/#{product_id}/supplies/new"
assert_select "h1", "Supply"
post "/products/#{product_id}/supplies",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"quantity" => ""
}

assert_response :unprocessable_entity
assert_select "span", "Quantity can't be blank"
end

def test_renders_validation_error_when_quantity_is_not_a_number
product_id = register_product("Async Remote", 100, 10)

get "/products/#{product_id}/supplies/new"
assert_select "h1", "Supply"
post "/products/#{product_id}/supplies",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"quantity" => "not a number"
}

assert_response :unprocessable_entity
assert_select "span", "Quantity is not a number"
end

def test_renders_validation_error_when_quantity_is_zero
product_id = register_product("Async Remote", 100, 10)

get "/products/#{product_id}/supplies/new"
assert_select "h1", "Supply"
post "/products/#{product_id}/supplies",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"quantity" => "0"
}

assert_response :unprocessable_entity
assert_select "span", "Quantity must be greater than 0"
end
end

0 comments on commit 360e274

Please sign in to comment.