summaryrefslogtreecommitdiff
path: root/app/models/tree.rb
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-03-17 10:29:06 +0100
committerDouwe Maan <douwe@gitlab.com>2015-03-18 14:08:26 +0100
commit31e484085a22ccc012b825670e5bbff25684933c (patch)
tree79862e3297d1f44cc5e49d7f5a93e95168fbbbe9 /app/models/tree.rb
parent88f9ed3d196776295294d540556216b95d0bcd60 (diff)
downloadgitlab-ce-31e484085a22ccc012b825670e5bbff25684933c.tar.gz
Find tree readme lazily.
Diffstat (limited to 'app/models/tree.rb')
-rw-r--r--app/models/tree.rb39
1 files changed, 19 insertions, 20 deletions
diff --git a/app/models/tree.rb b/app/models/tree.rb
index 9095fb134ff..d3f9176c79c 100644
--- a/app/models/tree.rb
+++ b/app/models/tree.rb
@@ -1,33 +1,32 @@
class Tree
include Gitlab::MarkdownHelper
- attr_accessor :entries, :readme
+ attr_accessor :entries
def initialize(repository, sha, path = '/')
path = '/' if path.blank?
git_repo = repository.raw_repository
@entries = Gitlab::Git::Tree.where(git_repo, sha, path)
+ end
+
+ def readme
+ return @readme if defined?(@readme)
+
+ available_readmes = @blobs.select(&:readme?)
- available_readmes = @entries.select(&:readme?)
-
- if available_readmes.count > 0
- # If there is more than 1 readme in tree, find readme which is supported
- # by markup renderer.
- if available_readmes.length > 1
- supported_readmes = available_readmes.select do |readme|
- previewable?(readme.name)
- end
-
- # Take the first supported readme, or the first available readme, if we
- # don't support any of them
- readme_tree = supported_readmes.first || available_readmes.first
- else
- readme_tree = available_readmes.first
- end
-
- readme_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)
- @readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
+ if available_readmes.count == 0
+ return @readme = nil
end
+
+ # Take the first previewable readme, or the first available readme, if we
+ # can't preview any of them
+ readme_tree = available_readmes.find do |readme|
+ previewable?(readme.name)
+ end || available_readmes.first
+
+ readme_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)
+
+ @readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
end
def trees