Class: VenusMediaLibrary::AcceptedTypes
- Inherits:
-
Object
- Object
- VenusMediaLibrary::AcceptedTypes
- Defined in:
- app/models/venus_media_library/accepted_types.rb
Overview
Resolves the file types a picker field accepts and matches content types against them, so the same rule drives the browser's native , the greying-out of non-matching library assets, and the server-side upload check. A token may be:
- an exact mime type ("image/png")
- a type wildcard ("image/*")
- a file extension (".png")
Extensions are mapped to a mime type via Marcel so server-side matching stays consistent with what the browser lets a user pick from disk.
This is a plain value object (not Active Record); it lives under app/models only so Zeitwerk autoloads it alongside the engine's other domain types.
Instance Attribute Summary collapse
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
- #any? ⇒ Boolean
-
#initialize(tokens) ⇒ AcceptedTypes
constructor
A new instance of AcceptedTypes.
-
#matches?(content_type) ⇒ Boolean
True when
content_typesatisfies at least one accepted token. -
#to_input_accept ⇒ Object
The value handed to a native — the raw tokens joined.
Constructor Details
#initialize(tokens) ⇒ AcceptedTypes
Returns a new instance of AcceptedTypes.
22 23 24 |
# File 'app/models/venus_media_library/accepted_types.rb', line 22 def initialize(tokens) @tokens = Array(tokens).uniq end |
Instance Attribute Details
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
20 21 22 |
# File 'app/models/venus_media_library/accepted_types.rb', line 20 def tokens @tokens end |
Class Method Details
.parse(raw) ⇒ Object
15 16 17 18 |
# File 'app/models/venus_media_library/accepted_types.rb', line 15 def self.parse(raw) tokens = Array(raw).flat_map { |value| value.to_s.split(",") }.map(&:strip).reject(&:blank?) new(tokens) end |
Instance Method Details
#any? ⇒ Boolean
26 27 28 |
# File 'app/models/venus_media_library/accepted_types.rb', line 26 def any? @tokens.any? end |
#matches?(content_type) ⇒ Boolean
True when content_type satisfies at least one accepted token.
36 37 38 39 40 |
# File 'app/models/venus_media_library/accepted_types.rb', line 36 def matches?(content_type) return false if content_type.blank? @tokens.any? { |token| token_matches?(token, content_type) } end |
#to_input_accept ⇒ Object
The value handed to a native — the raw tokens joined.
31 32 33 |
# File 'app/models/venus_media_library/accepted_types.rb', line 31 def to_input_accept @tokens.join(",") end |