Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed Solution #3

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions app/controllers/baskets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ def build_goods(basket)
good = basket.goods.build(name: name, price: price, quantity: quantity, basic_tax: 0, import_tax: 0)
good.save
end
basket
end
end
14 changes: 14 additions & 0 deletions app/models/good.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
class Good < ApplicationRecord
belongs_to :basket

before_save :calulate_tax_update_price

private

def calulate_tax_update_price
exampt = %w[chocolate book pills]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, because those values are going to be constant and might be needed elsewhere in the application, a good Ruby practice is to extract it as a constant and place it at the top of the class:

EXEMPT_FROM_TAX = %w[chocolate book pills]

self.basic_tax = (price.to_f.round(2) * 0.1).round(2) unless exampt.any? do |exeption|
name.include?(exeption)
end
self.import_tax = (price.to_f.round(2) * 0.05).round(2) if name.include?('imported')
fprice = quantity.to_f * (price.to_f.round(2) + basic_tax.to_f.round(2) + import_tax.to_f.round(2))
self.final_price = fprice.round(2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, in just one method, you are setting basic_tax, import_tax, and final_price. You could extract 3 individual methods for each so it's easier and clearer to unit test each one.

end
end
6 changes: 3 additions & 3 deletions app/views/baskets/_baskets.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<ul>
<% baskets.each do |basket|%>
<a href= <%= basket_path(basket.id)%> > basket <%= basket.id%> </a>
<li><a href= <%= basket_path(basket.id)%> > basket <%= basket.id%> </a></li>
<%end%>
</div>
</ul>
8 changes: 6 additions & 2 deletions app/views/baskets/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
<h1>Baskets#new</h1>
<p>Find me in app/views/baskets/new.html.erb</p>
<div>
<h5> Upload basket </h5>
<%= form_tag({:action => :create}, :multipart => true) do %>
<%= file_field_tag 'file'%>
<%= submit_tag 'submit'%>
<% end %>
4 changes: 2 additions & 2 deletions app/views/baskets/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1>Baskets#show</h1>
<p>Find me in app/views/baskets/show.html.erb</p>
<h1>Basket <%[email protected]%></h1>
<%= @basket.goods.map { |entry| entry} %>