summaryrefslogtreecommitdiff
path: root/app/models/blob.rb
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2016-02-18 18:19:43 -0500
committerRobert Speicher <rspeicher@gmail.com>2016-02-18 22:45:30 -0500
commit8c454b362425228ab14bb4ed5320f6ba2f505679 (patch)
tree764f96718fd1efef5ccc3eba458db63b6f1aa8d7 /app/models/blob.rb
parentea4d2741a2b574407b0bd387ccd6a8202c014fc5 (diff)
downloadgitlab-ce-8c454b362425228ab14bb4ed5320f6ba2f505679.tar.gz
Add a `Blob` model that wraps `Gitlab::Git::Blob`rs-blob
This allows us to take advantage of Rails' `to_partial_path` to render the correct partial based on the Blob type, rather than cluttering the view with conditionals. It also allows (and will allow in the future) better encapsulation for Blob-related logic which makes sense for our Rails app but might not make as much sense for the core `gitlab_git` library, such as detecting if the blob is an SVG.
Diffstat (limited to 'app/models/blob.rb')
-rw-r--r--app/models/blob.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/app/models/blob.rb b/app/models/blob.rb
new file mode 100644
index 00000000000..8ee9f3006b2
--- /dev/null
+++ b/app/models/blob.rb
@@ -0,0 +1,34 @@
+# Blob is a Rails-specific wrapper around Gitlab::Git::Blob objects
+class Blob < SimpleDelegator
+ # Wrap a Gitlab::Git::Blob object, or return nil when given nil
+ #
+ # This method prevents the decorated object from evaluating to "truthy" when
+ # given a nil value. For example:
+ #
+ # blob = Blob.new(nil)
+ # puts "truthy" if blob # => "truthy"
+ #
+ # blob = Blob.decorate(nil)
+ # puts "truthy" if blob # No output
+ def self.decorate(blob)
+ return if blob.nil?
+
+ new(blob)
+ end
+
+ def svg?
+ text? && language && language.name == 'SVG'
+ end
+
+ def to_partial_path
+ if lfs_pointer?
+ 'download'
+ elsif image? || svg?
+ 'image'
+ elsif text?
+ 'text'
+ else
+ 'download'
+ end
+ end
+end