Skip to content

Commit

Permalink
Added constant allowed images type and use turbo_frame_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
olexandervanzuriak committed Jan 17, 2025
1 parent f5aa673 commit da0aad9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
2 changes: 2 additions & 0 deletions app/models/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
class Formula < ApplicationRecord
include Translatable

ALLOWED_IMAGE_TYPES = "image/jpeg, image/png"

belongs_to :calculator

has_one_attached :formula_image
Expand Down
3 changes: 1 addition & 2 deletions app/validators/formula_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def acceptable_image(record)
record.errors.add(:formula_image, :image_size_invalid)
end

acceptable_types = ["image/jpeg", "image/png"]
if acceptable_types.exclude?(record.formula_image.content_type)
if Formula::ALLOWED_IMAGE_TYPES.exclude?(record.formula_image.content_type)
record.errors.add(:formula_image, :image_type_invalid)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<%= f.input :en_unit, label: "Unit Label:" %>

<%= f.label :formula_image, "Upload logo:", class: "form-label d-block" %>
<%= f.file_field :formula_image, accept: "image/jpeg, image/png", class: "file-input mb-3 flex" %>
<%= f.file_field :formula_image, accept: Formula::ALLOWED_IMAGE_TYPES, class: "file-input mb-3 flex" %>

<%= link_to_remove_association "- Remove Formula", f, class: "text-red-500 underline" %>
</div>
Expand Down
10 changes: 5 additions & 5 deletions app/views/calculators/partials/_calculation_results.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<%# TODO: Delete this and use user provided value%>
<%# TODO: Delete this and use user provided value %>
<% formula_image = "money_to_spent_2.png" %>

<div class="calculation-results rounded jumbotron jumbotron-fluid position-relative">
<h2 class="pt-6 text-2xl font-semibold text-center dynamic-text-color">Calculation Results</h2>
<div class="jumbotron calculation-results result main-result-container result-container">
<% results.each_with_index do |result, index| %>
<div id="result-<%= index %>">
<div>
<%= image_tag result[:formula_image], class: "formula-image", alt: "icon" %>
<turbo-frame id="calc-result-<%= index %>">
<%= render partial: "calculators/partials/show/calculation_result",
<%= turbo_frame_tag "calc-result-#{index}" do %>
<%= render partial: "calculators/partials/show/calculation_result",
locals: { result: result } %>
</turbo-frame>
<% end %>
</div>
<% end %>
</div>
Expand Down

0 comments on commit da0aad9

Please sign in to comment.