OSDN Git Service

Move some decorator logic to helpers
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Sun, 31 Mar 2013 20:46:54 +0000 (23:46 +0300)
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Sun, 31 Mar 2013 20:46:54 +0000 (23:46 +0300)
app/helpers/commits_helper.rb
app/helpers/tree_helper.rb

index acdd48e..da209e0 100644 (file)
@@ -1,4 +1,20 @@
 module CommitsHelper
+  # Returns a link to the commit author. If the author has a matching user and
+  # is a member of the current @project it will link to the team member page.
+  # Otherwise it will link to the author email as specified in the commit.
+  #
+  # options:
+  #  avatar: true will prepend the avatar image
+  #  size:   size of the avatar image in px
+  def commit_author_link(commit, options = {})
+    commit_person_link(commit, options.merge(source: :author))
+  end
+
+  # Just like #author_link but for the committer.
+  def commit_committer_link(commit, options = {})
+    commit_person_link(commit, options.merge(source: :committer))
+  end
+
   def identification_type(line)
     if line[0] == "+"
       "new"
@@ -105,4 +121,62 @@ module CommitsHelper
       line
     end
   end
+
+  # Breadcrumb links for a Project and, if applicable, a tree path
+  def commits_breadcrumbs
+    return unless @project && @ref
+
+    # Add the root project link and the arrow icon
+    crumbs = content_tag(:li) do
+      content_tag(:span, nil, class: 'arrow') +
+      link_to(@project.name, project_commits_path(@project, @ref))
+    end
+
+    if @path
+      parts = @path.split('/')
+
+      parts.each_with_index do |part, i|
+        crumbs += content_tag(:span, '/', class: 'divider')
+        crumbs += content_tag(:li) do
+          # The text is just the individual part, but the link needs all the parts before it
+          link_to part, project_commits_path(@project, tree_join(@ref, parts[0..i].join('/')))
+        end
+      end
+    end
+
+    crumbs.html_safe
+  end
+
+  protected
+
+  def no_commit_message
+    "--no commit message"
+  end
+
+  # Private: Returns a link to a person. If the person has a matching user and
+  # is a member of the current @project it will link to the team member page.
+  # Otherwise it will link to the person email as specified in the commit.
+  #
+  # options:
+  #  source: one of :author or :committer
+  #  avatar: true will prepend the avatar image
+  #  size:   size of the avatar image in px
+  def commit_person_link(commit, options = {})
+    source_name = commit.send "#{options[:source]}_name".to_sym
+    source_email = commit.send "#{options[:source]}_email".to_sym
+    text = if options[:avatar]
+            avatar = image_tag(gravatar_icon(source_email, options[:size]), class: "avatar #{"s#{options[:size]}" if options[:size]}", width: options[:size], alt: "")
+            %Q{#{avatar} <span class="commit-#{options[:source]}-name">#{source_name}</span>}
+          else
+            source_name
+          end
+
+    user = User.where('name like ? or email like ?', source_name, source_email).first
+
+    if user.nil?
+      mail_to(source_email, text.html_safe, class: "commit-#{options[:source]}-link")
+    else
+      link_to(text.html_safe, user_path(user), class: "commit-#{options[:source]}-link")
+    end
+  end
 end
index fab0085..1cba947 100644 (file)
@@ -70,28 +70,26 @@ module TreeHelper
     end
   end
 
-  # Breadcrumb links for a Project and, if applicable, a tree path
-  def breadcrumbs
-    return unless @project && @ref
-
-    # Add the root project link and the arrow icon
-    crumbs = content_tag(:li) do
-      content_tag(:span, nil, class: 'arrow') +
-      link_to(@project.name, project_commits_path(@project, @ref))
-    end
+  def tree_breadcrumbs(tree, max_links = 2)
+    if tree.path
+      part_path = ""
+      parts = tree.path.split("\/")
+
+      yield('..', nil) if parts.count > max_links
 
-    if @path
-      parts = @path.split('/')
+      parts.each do |part|
+        part_path = File.join(part_path, part) unless part_path.empty?
+        part_path = part if part_path.empty?
 
-      parts.each_with_index do |part, i|
-        crumbs += content_tag(:span, '/', class: 'divider')
-        crumbs += content_tag(:li) do
-          # The text is just the individual part, but the link needs all the parts before it
-          link_to part, project_commits_path(@project, tree_join(@ref, parts[0..i].join('/')))
-        end
+        next unless parts.last(2).include?(part) if parts.count > max_links
+        yield(part, tree_join(tree.ref, part_path))
       end
     end
+  end
 
-    crumbs.html_safe
+  def up_dir_path tree
+    file = File.join(tree.path, "..")
+    tree_join(tree.ref, file)
   end
+
 end