Skip to content

Commit

Permalink
Create SitesHelper with delivery_map_accepted_mime_types method
Browse files Browse the repository at this point in the history
  • Loading branch information
B-Rass committed Oct 29, 2024
1 parent 60c5242 commit a81af49
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/helpers/sites_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module SitesHelper
def delivery_map_accepted_mime_types
Site.validators_on(:delivery_map).find { |v| v.is_a?(ActiveStorageValidations::ContentTypeValidator) }
.options[:in].map { |f| Mime[f].to_s }
.uniq
.join(", ")
end
end
2 changes: 1 addition & 1 deletion app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Site < ApplicationRecord

has_one_attached :delivery_map

validates :delivery_map, content_type: %i[png jpg jpeg pdf gif]
validates :delivery_map, content_type: %i[png jpg jpeg gif pdf]

geocoded_by :address
after_validation :geocode
Expand Down
2 changes: 1 addition & 1 deletion app/views/sites/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<%= f.label :delivery_map, class: "form-label" %>
<%= f.file_field :delivery_map,
class: "form-control",
accept: "image/png, image/jpeg, image/gif, application/pdf" %>
accept: delivery_map_accepted_mime_types %>
<% if @site.delivery_map.attached? && @site.persisted? %>
<%= image_tag @site.delivery_map.representation(resize_to_limit: [200, 200]), class: "ms-0 mt-2" %>
<% end %>
Expand Down
9 changes: 9 additions & 0 deletions spec/helpers/sites_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe SitesHelper do
describe "delivery_map_accepted_mime_types" do
it { expect(helper.delivery_map_accepted_mime_types).to eq("image/png, image/jpeg, image/gif, application/pdf") }
end
end

0 comments on commit a81af49

Please sign in to comment.