From 9453d21351fad2763c01fea067f8a093fe1d87fb Mon Sep 17 00:00:00 2001 From: B_Rass Date: Tue, 29 Oct 2024 12:30:14 +0100 Subject: [PATCH] Move format validator check to application helper --- app/helpers/application_helper.rb | 9 +++++++++ app/helpers/sites_helper.rb | 10 ---------- app/views/sites/_form.html.erb | 2 +- spec/helpers/application_helper_spec.rb | 23 +++++++++++++++++++++++ spec/helpers/sites_helper_spec.rb | 9 --------- 5 files changed, 33 insertions(+), 20 deletions(-) delete mode 100644 app/helpers/sites_helper.rb create mode 100644 spec/helpers/application_helper_spec.rb delete mode 100644 spec/helpers/sites_helper_spec.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 15b06f0f6..97a38e4a8 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,13 @@ # frozen_string_literal: true module ApplicationHelper + def accepted_format_for_attachment(model_klass, attribute_name) + validator = model_klass.validators_on(attribute_name.to_sym).find do |v| + v.is_a?(ActiveStorageValidations::ContentTypeValidator) + end + + return unless validator + + validator.options[:in].map { |f| Mime[f].to_s }.uniq.join(", ") + end end diff --git a/app/helpers/sites_helper.rb b/app/helpers/sites_helper.rb deleted file mode 100644 index a66a8c25e..000000000 --- a/app/helpers/sites_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -# 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 diff --git a/app/views/sites/_form.html.erb b/app/views/sites/_form.html.erb index 35aac48f6..7ed77ab33 100644 --- a/app/views/sites/_form.html.erb +++ b/app/views/sites/_form.html.erb @@ -71,7 +71,7 @@ <%= f.label :delivery_map, class: "form-label" %> <%= f.file_field :delivery_map, class: "form-control", - accept: delivery_map_accepted_mime_types %> + accept: accepted_format_for_attachment(@site.class, "delivery_map") %> <% if @site.delivery_map.attached? && @site.persisted? %> <%= image_tag @site.delivery_map.representation(resize_to_limit: [200, 200]), class: "ms-0 mt-2" %> <% end %> diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb new file mode 100644 index 000000000..7edaa3f54 --- /dev/null +++ b/spec/helpers/application_helper_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe ApplicationHelper do + describe "accepted_format_for_attachment" do + context "with a model and attribute using ContentTypeValidator" do + it do + expect(helper.accepted_format_for_attachment(Site, "delivery_map")).to eq( + "image/png, image/jpeg, image/gif, application/pdf" + ) + end + end + + context "with a model using ContentTypeValidator (but not the attribute)" do + it { expect(helper.accepted_format_for_attachment(Site, "name")).to be_nil } + end + + context "with a model not using ContentTypeValidator" do + it { expect(helper.accepted_format_for_attachment(Room, "name")).to be_nil } + end + end +end diff --git a/spec/helpers/sites_helper_spec.rb b/spec/helpers/sites_helper_spec.rb deleted file mode 100644 index c9983bf60..000000000 --- a/spec/helpers/sites_helper_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -# 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