-
-
Notifications
You must be signed in to change notification settings - Fork 148
feat: include content_types in the file-field accept attribute #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pardeyke
wants to merge
5
commits into
igorkasyanchuk:master
Choose a base branch
from
pardeyke:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,39 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ActiveStorageValidations | ||
| module FormBuilder | ||
| def file_field(method, options = {}) | ||
| if options[:accept].blank? | ||
| accept = inferred_accept_types(method) | ||
| options[:accept] = accept.join(",") if accept.any? | ||
| end | ||
|
|
||
| super(method, options) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def inferred_accept_types(method) | ||
| return [] unless @object.class.respond_to?(:validators_on) | ||
|
|
||
| content_type_validators = @object.class.validators_on(method).select do |v| | ||
| v.is_a?(ActiveStorageValidations::ContentTypeValidator) | ||
| end | ||
|
|
||
| content_type_validators.flat_map do |validator| | ||
| types = Array(validator.options[:with]) + Array(validator.options[:in]) | ||
| types.filter_map do |type| | ||
| case type | ||
| when String | ||
| type.include?("/") ? type : Marcel::MimeType.for(declared_type: type, extension: type) | ||
| when Symbol | ||
| Marcel::MimeType.for(declared_type: type.to_s, extension: type.to_s) | ||
| when Regexp | ||
| match = type.source.match(%r{\A\\A([a-z]+)/\.\*\\z\z}) | ||
| "#{match[1]}/*" if match | ||
| end | ||
| end | ||
| end.uniq | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module FormBuilder | ||
| def self.table_name_prefix | ||
| "form_builder_" | ||
| end | ||
| end |
This file contains hidden or 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,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # == Schema Information | ||
| # | ||
| # Table name: form_builder_checks | ||
| # | ||
| # id :integer not null, primary key | ||
| # created_at :datetime not null | ||
| # updated_at :datetime not null | ||
| # | ||
|
|
||
| class FormBuilder::Check < ApplicationRecord | ||
| has_one_attached :with_symbol | ||
| validates :with_symbol, content_type: :png | ||
|
|
||
| has_one_attached :in_array | ||
| validates :in_array, content_type: [ :png, :gif ] | ||
|
|
||
| has_one_attached :with_string_mime | ||
| validates :with_string_mime, content_type: "image/png" | ||
|
|
||
| has_one_attached :with_proc | ||
| validates :with_proc, content_type: ->(record) { :png } | ||
|
|
||
| has_one_attached :with_regex | ||
| validates :with_regex, content_type: /\Aimage\/.*\z/ | ||
|
|
||
| has_one_attached :with_non_matching_regex | ||
| validates :with_non_matching_regex, content_type: /\Aimage\/(png|gif)\z/ | ||
|
|
||
| has_one_attached :no_content_type_validator | ||
| validates :no_content_type_validator, attached: true | ||
|
|
||
| has_one_attached :no_validator | ||
| end |
This file contains hidden or 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 hidden or 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,57 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| describe ActiveStorageValidations::FormBuilder do | ||
| let(:model) { FormBuilder::Check.new } | ||
| let(:builder) { ActionView::Helpers::FormBuilder.new(:check, model, ActionView::Base.empty, {}) } | ||
|
|
||
| describe "file_field with content_type validator" do | ||
| it "auto-sets accept from :with symbol option" do | ||
| html = builder.file_field(:with_symbol) | ||
| assert_includes html, 'accept="image/png"' | ||
| end | ||
|
|
||
| it "auto-sets accept from :in array option" do | ||
| html = builder.file_field(:in_array) | ||
| assert_includes html, "image/png" | ||
| assert_includes html, "image/gif" | ||
| end | ||
|
|
||
| it "auto-sets accept from string MIME type" do | ||
| html = builder.file_field(:with_string_mime) | ||
| assert_includes html, 'accept="image/png"' | ||
| end | ||
|
|
||
| it "does not override explicit accept option" do | ||
| html = builder.file_field(:with_symbol, accept: "image/jpeg") | ||
| assert_includes html, 'accept="image/jpeg"' | ||
| refute_includes html, "image/png" | ||
| end | ||
|
|
||
| it "handles models without content_type validators" do | ||
| html = builder.file_field(:no_content_type_validator) | ||
| refute_includes html, "accept=" | ||
| end | ||
|
|
||
| it "handles attachments with no validators at all" do | ||
| html = builder.file_field(:no_validator) | ||
| refute_includes html, "accept=" | ||
| end | ||
|
|
||
| it "skips Proc options gracefully" do | ||
| html = builder.file_field(:with_proc) | ||
| refute_includes html, "accept=" | ||
| end | ||
|
|
||
| it "auto-sets accept from Regexp content type" do | ||
| html = builder.file_field(:with_regex) | ||
| assert_includes html, 'accept="image/*"' | ||
| end | ||
|
|
||
| it "skips non-matching Regexp options gracefully" do | ||
| html = builder.file_field(:with_non_matching_regex) | ||
| refute_includes html, "accept=" | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For
Regexwe could make it a bit more practical.Let's say the dev has set up exactly
content_type: /\Aimage\/.*\z/, we could translate it toaccept="image/*". We could do this for all content_type categories (image,video,application, ...). Other regexes would be ignored as previously committed.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pardeyke thanks for the addition, could you update the tests accordingly? If so we can merge it soon! :)