Class: DiffViewer::Base

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

Constant Summary collapse

PARTIAL_PATH_PREFIX =
'projects/diffs/viewers'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diff_file) ⇒ Base

Returns a new instance of Base.


19
20
21
22
# File 'app/models/diff_viewer/base.rb', line 19

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

Instance Attribute Details

#diff_fileObject (readonly)

Returns the value of attribute diff_file


15
16
17
# File 'app/models/diff_viewer/base.rb', line 15

def diff_file
  @diff_file
end

Class Method Details

.binary?Boolean

Returns:

  • (Boolean)

36
37
38
# File 'app/models/diff_viewer/base.rb', line 36

def self.binary?
  binary
end

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

Returns:

  • (Boolean)

44
45
46
47
# File 'app/models/diff_viewer/base.rb', line 44

def self.can_render?(diff_file, verify_binary: true)
  can_render_blob?(diff_file.old_blob, verify_binary: verify_binary) &&
    can_render_blob?(diff_file.new_blob, verify_binary: verify_binary)
end

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

Returns:

  • (Boolean)

49
50
51
52
53
54
55
# File 'app/models/diff_viewer/base.rb', line 49

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

  false
end

.partial_pathObject


24
25
26
# File 'app/models/diff_viewer/base.rb', line 24

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

.rich?Boolean

Returns:

  • (Boolean)

28
29
30
# File 'app/models/diff_viewer/base.rb', line 28

def self.rich?
  type == :rich
end

.simple?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'app/models/diff_viewer/base.rb', line 32

def self.simple?
  type == :simple
end

.text?Boolean

Returns:

  • (Boolean)

40
41
42
# File 'app/models/diff_viewer/base.rb', line 40

def self.text?
  !binary?
end

Instance Method Details

#binary_detected_after_load?Boolean

Returns:

  • (Boolean)

71
72
73
# File 'app/models/diff_viewer/base.rb', line 71

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

#collapsed?Boolean

Returns:

  • (Boolean)

57
58
59
60
61
62
# File 'app/models/diff_viewer/base.rb', line 57

def collapsed?
  return @collapsed if defined?(@collapsed)
  return @collapsed = true if diff_file.collapsed?

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

#prepare!Object


95
96
97
# File 'app/models/diff_viewer/base.rb', line 95

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 diff_file at all. The human-readable error message can be retrieved by #render_error_message.


78
79
80
81
82
# File 'app/models/diff_viewer/base.rb', line 78

def render_error
  if too_large?
    :too_large
  end
end

#render_error_messageObject


84
85
86
87
88
89
90
91
92
93
# File 'app/models/diff_viewer/base.rb', line 84

def render_error_message
  return unless render_error

  _("This %{viewer} could not be displayed because %{reason}. You can %{options} instead.") %
    {
      viewer: switcher_title,
      reason: render_error_reason,
      options: Gitlab::Utils.to_exclusive_sentence(render_error_options)
    }
end

#too_large?Boolean

Returns:

  • (Boolean)

64
65
66
67
68
69
# File 'app/models/diff_viewer/base.rb', line 64

def too_large?
  return @too_large if defined?(@too_large)
  return @too_large = true if diff_file.too_large?

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