module Util module_function def days_between(time, other_time) raise ArgumentError if time.blank? || other_time.blank? Date.parse(time.to_s) - Date.parse(other_time.to_s) end def convert_base64_image(str, **opts) return if str.blank? Base64ImageConverter.new(**opts).convert(str) end def write_file(io, path) dir = File.dirname(path) FileUtils.mkdir_p(dir) unless File.directory?(dir) Rails.logger.info("### save file #{path}, size: #{io.size} ~") File.open(path, 'wb') do |file| if io.respond_to?(:read) io.rewind while buffer = io.read(8192) file.write(buffer) end else file.write(io) end end end def logger_error(exception) Rails.logger.error(exception.message) exception.backtrace.each { |message| Rails.logger.error(message) } end def map_or_pluck(relation, name) relation.is_a?(Array) || relation.loaded? ? relation.map(&name.to_sym) : relation.pluck(name) end def extract_content(str) return '' if str.blank? str.gsub(/<\/?.*?>/, '').gsub(/[\n\t\r]/, '').gsub(/ /, '') end end