application_helper.rb 3.3 KB
Newer Older
G
gitlabhq 已提交
1 2
require 'digest/md5'
module ApplicationHelper
3

4 5
  # Check if a particular controller is the current one
  #
6 7
  # args - One or more controller names to check
  #
8 9 10
  # Examples
  #
  #   # On TreeController
11 12 13 14 15
  #   current_controller?(:tree)           # => true
  #   current_controller?(:commits)        # => false
  #   current_controller?(:commits, :tree) # => true
  def current_controller?(*args)
    args.any? { |v| v.to_s.downcase == controller.controller_name }
16 17
  end

18
  def gravatar_icon(user_email = '', size = 40)
19 20 21 22 23 24 25
    if Gitlab.config.disable_gravatar? || user_email.blank?
      'no_avatar.png'
    else
      gravatar_prefix = request.ssl? ? "https://secure" : "http://www"
      user_email.strip!
      "#{gravatar_prefix}.gravatar.com/avatar/#{Digest::MD5.hexdigest(user_email.downcase)}?s=#{size}&d=identicon"
    end
G
gitlabhq 已提交
26 27
  end

28 29 30 31 32
  def request_protocol
    request.ssl? ? "https" : "http"
  end

  def web_app_url
33
    "#{request_protocol}://#{Gitlab.config.web_host}/"
34 35
  end

G
gitlabhq 已提交
36
  def last_commit(project)
N
Nihad Abbasov 已提交
37
    if project.repo_exists?
G
gitlabhq 已提交
38
      time_ago_in_words(project.commit.committed_date) + " ago"
N
Nihad Abbasov 已提交
39
    else
G
gitlabhq 已提交
40 41
      "Never"
    end
N
Nihad Abbasov 已提交
42
  rescue
G
gitlabhq 已提交
43
    "Never"
G
gitlabhq 已提交
44 45
  end

G
gitlabhq 已提交
46
  def grouped_options_refs(destination = :tree)
G
gitlabhq 已提交
47
    options = [
D
Dmitriy Zaporozhets 已提交
48
      ["Branch", @project.repo.heads.map(&:name) ],
G
gitlabhq 已提交
49 50 51
      [ "Tag", @project.tags ]
    ]

A
Andrey Vakarev 已提交
52
    # If reference is commit id -
53
    # we should add it to branch/tag selectbox
54
    if(@ref && !options.flatten.include?(@ref) &&
55 56 57 58
       @ref =~ /^[0-9a-zA-Z]{6,52}$/)
      options << ["Commit", [@ref]]
    end

D
Dmitriy Zaporozhets 已提交
59
    grouped_options_for_select(options, @ref || @project.default_branch)
G
gitlabhq 已提交
60 61
  end

G
gitlabhq 已提交
62
  def search_autocomplete_source
63
    projects = current_user.projects.map{ |p| { label: p.name, url: project_path(p) } }
G
gitlabhq 已提交
64
    default_nav = [
65 66 67 68
      { label: "Profile", url: profile_path },
      { label: "Keys", url: keys_path },
      { label: "Dashboard", url: root_path },
      { label: "Admin", url: admin_root_path }
G
gitlabhq 已提交
69 70 71 72 73 74
    ]

    project_nav = []

    if @project && !@project.new_record?
      project_nav = [
75 76 77 78 79
        { label: "#{@project.name} / Issues",  url: project_issues_path(@project) },
        { label: "#{@project.name} / Wall",    url: wall_project_path(@project) },
        { label: "#{@project.name} / Tree",    url: project_tree_path(@project, @ref || @project.root_ref) },
        { label: "#{@project.name} / Commits", url: project_commits_path(@project, @ref || @project.root_ref) },
        { label: "#{@project.name} / Team",    url: project_team_index_path(@project) }
G
gitlabhq 已提交
80 81 82 83 84 85
      ]
    end

    [projects, default_nav, project_nav].flatten.to_json
  end

V
vsizov 已提交
86 87 88
  def ldap_enable?
    Devise.omniauth_providers.include?(:ldap)
  end
89

90
  def app_theme
91
    Gitlab::Theme.css_class_by_id(current_user.try(:theme_id))
92
  end
R
randx 已提交
93 94

  def show_last_push_widget?(event)
95
    event &&
R
randx 已提交
96 97
      event.last_push_to_non_root? &&
      !event.rm_ref? &&
98
      event.project &&
R
randx 已提交
99
      event.project.merge_requests_enabled
R
randx 已提交
100
  end
D
Dmitriy Zaporozhets 已提交
101

102 103 104
  def hexdigest(string)
    Digest::SHA1.hexdigest string
  end
105 106 107 108 109 110 111 112 113

  def project_last_activity project
    activity = project.last_activity
    if activity && activity.created_at
      time_ago_in_words(activity.created_at) + " ago"
    else
      "Never"
    end
  end
114

F
Florian Unglaub 已提交
115
  def authbutton(provider, size = 64)
D
Dmitriy Zaporozhets 已提交
116 117 118
    file_name = "#{provider.to_s.split('_').first}_#{size}.png"
    image_tag("authbuttons/#{file_name}",
              alt: "Sign in with #{provider.to_s.titleize}")
F
Florian Unglaub 已提交
119
  end
G
gitlabhq 已提交
120
end