Add BlobViewer::Base#binary? method
This commit is contained in:
parent
ec19703a44
commit
a2f4650fc7
2 changed files with 30 additions and 22 deletions
|
@ -70,7 +70,11 @@ class Blob < SimpleDelegator
|
|||
|
||||
def raw_binary?
|
||||
if valid_lfs_pointer?
|
||||
!rich_viewer&.text_based?
|
||||
if rich_viewer
|
||||
rich_viewer.binary?
|
||||
else
|
||||
true
|
||||
end
|
||||
else
|
||||
binary?
|
||||
end
|
||||
|
@ -96,22 +100,6 @@ class Blob < SimpleDelegator
|
|||
lfs_pointer? && !project.lfs_enabled?
|
||||
end
|
||||
|
||||
def simple_viewer_class
|
||||
if empty?
|
||||
BlobViewer::Empty
|
||||
elsif raw_binary?
|
||||
BlobViewer::Download
|
||||
else # text
|
||||
BlobViewer::Text
|
||||
end
|
||||
end
|
||||
|
||||
def rich_viewer_class
|
||||
return if invalid_lfs_pointer? || empty?
|
||||
|
||||
rich_viewers_classes.find { |viewer_class| viewer_class.can_render?(self) }
|
||||
end
|
||||
|
||||
def simple_viewer
|
||||
@simple_viewer ||= simple_viewer_class.new(self)
|
||||
end
|
||||
|
@ -123,7 +111,7 @@ class Blob < SimpleDelegator
|
|||
end
|
||||
|
||||
def rendered_as_text?(ignore_errors: true)
|
||||
simple_viewer.is_a?(BlobViewer::Text) && (ignore_errors || simple_viewer.render_error.nil?)
|
||||
simple_viewer.text? && (ignore_errors || simple_viewer.render_error.nil?)
|
||||
end
|
||||
|
||||
def show_viewer_switcher?
|
||||
|
@ -137,13 +125,29 @@ class Blob < SimpleDelegator
|
|||
|
||||
private
|
||||
|
||||
def simple_viewer_class
|
||||
if empty?
|
||||
BlobViewer::Empty
|
||||
elsif raw_binary?
|
||||
BlobViewer::Download
|
||||
else # text
|
||||
BlobViewer::Text
|
||||
end
|
||||
end
|
||||
|
||||
def rich_viewers_classes
|
||||
if valid_lfs_pointer?
|
||||
RICH_VIEWERS
|
||||
elsif binary?
|
||||
RICH_VIEWERS.reject(&:text_based?)
|
||||
RICH_VIEWERS.select(&:binary?)
|
||||
else # text
|
||||
RICH_VIEWERS.select(&:text_based?)
|
||||
RICH_VIEWERS.select(&:text?)
|
||||
end
|
||||
end
|
||||
|
||||
def rich_viewer_class
|
||||
return if invalid_lfs_pointer? || empty?
|
||||
|
||||
rich_viewers_classes.find { |viewer_class| viewer_class.can_render?(self) }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ module BlobViewer
|
|||
class Base
|
||||
class_attribute :partial_name, :type, :extensions, :client_side, :text_based, :switcher_icon, :switcher_title, :max_size, :absolute_max_size
|
||||
|
||||
delegate :partial_path, :rich?, :simple?, :client_side?, :server_side?, :text_based?, to: :class
|
||||
delegate :partial_path, :rich?, :simple?, :client_side?, :server_side?, :text?, :binary?, to: :class
|
||||
|
||||
attr_reader :blob
|
||||
attr_accessor :override_max_size
|
||||
|
@ -31,10 +31,14 @@ module BlobViewer
|
|||
!client_side?
|
||||
end
|
||||
|
||||
def self.text_based?
|
||||
def self.text?
|
||||
text_based
|
||||
end
|
||||
|
||||
def self.binary?
|
||||
!text?
|
||||
end
|
||||
|
||||
def self.can_render?(blob)
|
||||
!extensions || extensions.include?(blob.extension)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue