Class: BlobViewer::Base

Inherits:
Object
  • Object
show all
Defined in:
app/models/blob_viewer/base.rb

Constant Summary collapse

PARTIAL_PATH_PREFIX =
'projects/blob/viewers'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blob) ⇒ Base

Returns a new instance of Base.



17
18
19
20
# File 'app/models/blob_viewer/base.rb', line 17

def initialize(blob)
  @blob = blob
  @initially_binary = blob.binary_in_repo?
end

Instance Attribute Details

#blobObject (readonly)

Returns the value of attribute blob.



13
14
15
# File 'app/models/blob_viewer/base.rb', line 13

def blob
  @blob
end

Class Method Details

.auxiliary?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/blob_viewer/base.rb', line 38

def self.auxiliary?
  type == :auxiliary
end

.binary?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/blob_viewer/base.rb', line 46

def self.binary?
  binary
end

.can_render?(blob, verify_binary: true) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'app/models/blob_viewer/base.rb', line 54

def self.can_render?(blob, verify_binary: true)
  return false if verify_binary && binary? != blob.binary_in_repo?
  return true if extensions&.include?(blob.extension)
  return true if file_types&.include?(blob.file_type)

  false
end

.load_async?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/blob_viewer/base.rb', line 42

def self.load_async?
  load_async
end

.loading_partial_pathObject



26
27
28
# File 'app/models/blob_viewer/base.rb', line 26

def self.loading_partial_path
  File.join(PARTIAL_PATH_PREFIX, loading_partial_name)
end

.partial_pathObject



22
23
24
# File 'app/models/blob_viewer/base.rb', line 22

def self.partial_path
  File.join(PARTIAL_PATH_PREFIX, partial_name)
end

.rich?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/models/blob_viewer/base.rb', line 30

def self.rich?
  type == :rich
end

.simple?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/blob_viewer/base.rb', line 34

def self.simple?
  type == :simple
end

.text?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/blob_viewer/base.rb', line 50

def self.text?
  !binary?
end

Instance Method Details

#binary_detected_after_load?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/blob_viewer/base.rb', line 74

def binary_detected_after_load?
  !@initially_binary && blob.binary_in_repo?
end

#collapsed?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'app/models/blob_viewer/base.rb', line 62

def collapsed?
  return @collapsed if defined?(@collapsed)

  @collapsed = !blob.expanded? && collapse_limit && blob.raw_size > collapse_limit
end

#prepare!Object



97
98
99
# File 'app/models/blob_viewer/base.rb', line 97

def prepare!
  # To be overridden by subclasses
end

#render_errorObject

This method is used on the server side to check whether we can attempt to render the blob at all. Human-readable error messages are found in the ‘BlobHelper#blob_render_error_reason` helper.

This method does not and should not load the entire blob contents into memory, and should not be overridden to do so in order to validate the format of the blob.

Prefer to implement a client-side viewer, where the JS component loads the binary from ‘blob_raw_path` and does its own format validation and error rendering, especially for potentially large binary formats.



89
90
91
92
93
94
95
# File 'app/models/blob_viewer/base.rb', line 89

def render_error
  if too_large?
    :too_large
  elsif collapsed?
    :collapsed
  end
end

#too_large?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'app/models/blob_viewer/base.rb', line 68

def too_large?
  return @too_large if defined?(@too_large)

  @too_large = size_limit && blob.raw_size > size_limit
end