Skip to content

Commit

Permalink
Breaking the law breaking the law
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszreszke committed Sep 23, 2024
1 parent 1629faa commit 00564fc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
17 changes: 3 additions & 14 deletions rails_application/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,9 @@ def create
def update
@product = Product.find(params[:id])

if params["future_price"].present?
@product.future_price = params["future_price"]["price"]
@product.future_price_start_time = params["future_price"]["start_time"]
@product.save!
else
ApplicationRecord.transaction do
@product_with_new_price = @product.dup
@product_with_new_price.price = product_params[:price]
@product_with_new_price.latest = true
@product.latest = false
@product_with_new_price.save!
@product.save!
end
end
@product.change_name(params[:product][:name])
@product.change_price!(product_params)

redirect_to products_path, notice: "Product was successfully updated"
end

Expand Down
24 changes: 24 additions & 0 deletions rails_application/app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,28 @@ def validate_vat_rate
errors.add(:vat_rate, "is not a number")
end
end

def change_name(new_name)
return if new_name == self.name || new_name.blank?
self.name = new_name
self.save!
end

def change_price!(params)
if params["future_price"].present?

self.future_price = params["future_price"]["price"]
self.future_price_start_time = params["future_price"]["start_time"]
self.save!
else
ApplicationRecord.transaction do
product_with_new_price = self.dup
product_with_new_price.price = params[:price]
product_with_new_price.latest = true
self.latest = false
product_with_new_price.save!
self.save!
end
end
end
end
6 changes: 6 additions & 0 deletions rails_application/app/views/products/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
</label>
<%= form.text_field :name, required: true, 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 } %>
</div>
<div>
<label for="name" class="block font-bold">
SKU
</label>
<%= form.text_field :sku, required: true, 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 } %>
</div>
<div class="mt-2">
<label for="price" class="block font-bold">
Price
Expand Down

0 comments on commit 00564fc

Please sign in to comment.