Commit da5b0c91dc79136be4315cf61e5520692e19be8a
1 parent
685681e2
Exists in
master
and in
4 other branches
Move some decorator logic to helpers
Showing
2 changed files
with
90 additions
and
18 deletions
Show diff stats
app/helpers/commits_helper.rb
1 | module CommitsHelper | 1 | module CommitsHelper |
2 | + # Returns a link to the commit author. If the author has a matching user and | ||
3 | + # is a member of the current @project it will link to the team member page. | ||
4 | + # Otherwise it will link to the author email as specified in the commit. | ||
5 | + # | ||
6 | + # options: | ||
7 | + # avatar: true will prepend the avatar image | ||
8 | + # size: size of the avatar image in px | ||
9 | + def commit_author_link(commit, options = {}) | ||
10 | + commit_person_link(commit, options.merge(source: :author)) | ||
11 | + end | ||
12 | + | ||
13 | + # Just like #author_link but for the committer. | ||
14 | + def commit_committer_link(commit, options = {}) | ||
15 | + commit_person_link(commit, options.merge(source: :committer)) | ||
16 | + end | ||
17 | + | ||
2 | def identification_type(line) | 18 | def identification_type(line) |
3 | if line[0] == "+" | 19 | if line[0] == "+" |
4 | "new" | 20 | "new" |
@@ -105,4 +121,62 @@ module CommitsHelper | @@ -105,4 +121,62 @@ module CommitsHelper | ||
105 | line | 121 | line |
106 | end | 122 | end |
107 | end | 123 | end |
124 | + | ||
125 | + # Breadcrumb links for a Project and, if applicable, a tree path | ||
126 | + def commits_breadcrumbs | ||
127 | + return unless @project && @ref | ||
128 | + | ||
129 | + # Add the root project link and the arrow icon | ||
130 | + crumbs = content_tag(:li) do | ||
131 | + content_tag(:span, nil, class: 'arrow') + | ||
132 | + link_to(@project.name, project_commits_path(@project, @ref)) | ||
133 | + end | ||
134 | + | ||
135 | + if @path | ||
136 | + parts = @path.split('/') | ||
137 | + | ||
138 | + parts.each_with_index do |part, i| | ||
139 | + crumbs += content_tag(:span, '/', class: 'divider') | ||
140 | + crumbs += content_tag(:li) do | ||
141 | + # The text is just the individual part, but the link needs all the parts before it | ||
142 | + link_to part, project_commits_path(@project, tree_join(@ref, parts[0..i].join('/'))) | ||
143 | + end | ||
144 | + end | ||
145 | + end | ||
146 | + | ||
147 | + crumbs.html_safe | ||
148 | + end | ||
149 | + | ||
150 | + protected | ||
151 | + | ||
152 | + def no_commit_message | ||
153 | + "--no commit message" | ||
154 | + end | ||
155 | + | ||
156 | + # Private: Returns a link to a person. If the person has a matching user and | ||
157 | + # is a member of the current @project it will link to the team member page. | ||
158 | + # Otherwise it will link to the person email as specified in the commit. | ||
159 | + # | ||
160 | + # options: | ||
161 | + # source: one of :author or :committer | ||
162 | + # avatar: true will prepend the avatar image | ||
163 | + # size: size of the avatar image in px | ||
164 | + def commit_person_link(commit, options = {}) | ||
165 | + source_name = commit.send "#{options[:source]}_name".to_sym | ||
166 | + source_email = commit.send "#{options[:source]}_email".to_sym | ||
167 | + text = if options[:avatar] | ||
168 | + avatar = image_tag(gravatar_icon(source_email, options[:size]), class: "avatar #{"s#{options[:size]}" if options[:size]}", width: options[:size], alt: "") | ||
169 | + %Q{#{avatar} <span class="commit-#{options[:source]}-name">#{source_name}</span>} | ||
170 | + else | ||
171 | + source_name | ||
172 | + end | ||
173 | + | ||
174 | + user = User.where('name like ? or email like ?', source_name, source_email).first | ||
175 | + | ||
176 | + if user.nil? | ||
177 | + mail_to(source_email, text.html_safe, class: "commit-#{options[:source]}-link") | ||
178 | + else | ||
179 | + link_to(text.html_safe, user_path(user), class: "commit-#{options[:source]}-link") | ||
180 | + end | ||
181 | + end | ||
108 | end | 182 | end |
app/helpers/tree_helper.rb
@@ -70,28 +70,26 @@ module TreeHelper | @@ -70,28 +70,26 @@ module TreeHelper | ||
70 | end | 70 | end |
71 | end | 71 | end |
72 | 72 | ||
73 | - # Breadcrumb links for a Project and, if applicable, a tree path | ||
74 | - def breadcrumbs | ||
75 | - return unless @project && @ref | ||
76 | - | ||
77 | - # Add the root project link and the arrow icon | ||
78 | - crumbs = content_tag(:li) do | ||
79 | - content_tag(:span, nil, class: 'arrow') + | ||
80 | - link_to(@project.name, project_commits_path(@project, @ref)) | ||
81 | - end | 73 | + def tree_breadcrumbs(tree, max_links = 2) |
74 | + if tree.path | ||
75 | + part_path = "" | ||
76 | + parts = tree.path.split("\/") | ||
77 | + | ||
78 | + yield('..', nil) if parts.count > max_links | ||
82 | 79 | ||
83 | - if @path | ||
84 | - parts = @path.split('/') | 80 | + parts.each do |part| |
81 | + part_path = File.join(part_path, part) unless part_path.empty? | ||
82 | + part_path = part if part_path.empty? | ||
85 | 83 | ||
86 | - parts.each_with_index do |part, i| | ||
87 | - crumbs += content_tag(:span, '/', class: 'divider') | ||
88 | - crumbs += content_tag(:li) do | ||
89 | - # The text is just the individual part, but the link needs all the parts before it | ||
90 | - link_to part, project_commits_path(@project, tree_join(@ref, parts[0..i].join('/'))) | ||
91 | - end | 84 | + next unless parts.last(2).include?(part) if parts.count > max_links |
85 | + yield(part, tree_join(tree.ref, part_path)) | ||
92 | end | 86 | end |
93 | end | 87 | end |
88 | + end | ||
94 | 89 | ||
95 | - crumbs.html_safe | 90 | + def up_dir_path tree |
91 | + file = File.join(tree.path, "..") | ||
92 | + tree_join(tree.ref, file) | ||
96 | end | 93 | end |
94 | + | ||
97 | end | 95 | end |