forked from nanego/my-dcim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move format validator check to application helper
- Loading branch information
Showing
5 changed files
with
33 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.