Module: Arkaan::Concerns::MimeTypable::ClassMethods
- Defined in:
- lib/arkaan/concerns/mime_typable.rb
Overview
Submodule holding all the static methods add to the current subclass.
Instance Method Summary collapse
-
#mime_type(values) ⇒ Object
Defines the MIME type attribute with the given possible MIME types.
- #mime_type_check(values) ⇒ Object
- #parse_values(values) ⇒ Object
Instance Method Details
#mime_type(values) ⇒ Object
Defines the MIME type attribute with the given possible MIME types.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/arkaan/concerns/mime_typable.rb', line 15 def mime_type(values) # @!attribute [rw] mime_type # @return [String] the MIME type of the file, obtained from the uploaded file. field :mime_type, type: String validates :mime_type, presence: { message: 'required' } validate :mime_type_validity, if: :mime_type? mime_type_check(values) end |
#mime_type_check(values) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/arkaan/concerns/mime_typable.rb', line 27 def mime_type_check(values) # Validates the validity of the MIME type by checking if it respects any of the given mime types. # If it does not respect any MIME types possible, it adds an error to the mime_type field and invalidates. define_method :mime_type_validity do checked_types = parse_values(values) return true if checked_types.empty? checked_types.each do |type| type_regex = ::Regexp.new("^#{type.sub(/\*/, '(.+)')}$") return true unless type_regex.match(mime_type).nil? end errors.add(:mime_type, 'pattern') end end |
#parse_values(values) ⇒ Object
42 43 44 45 46 |
# File 'lib/arkaan/concerns/mime_typable.rb', line 42 def parse_values(values) values.is_a?(Symbol) ? send(values) : values rescue StandardError [] end |