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

4 5 6 7 8 9 10 11 12 13 14
  # Check if a particular controller is the current one
  #
  # Examples
  #
  #   # On TreeController
  #   current_controller?(:tree)    # => true
  #   current_controller?(:commits) # => false
  def current_controller?(name)
    controller.controller_name == name.to_s.downcase
  end

15
  def gravatar_icon(user_email = '', size = 40)
16 17 18 19 20 21 22
    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 已提交
23 24
  end

25 26 27 28 29
  def request_protocol
    request.ssl? ? "https" : "http"
  end

  def web_app_url
30
    "#{request_protocol}://#{Gitlab.config.web_host}/"
31 32
  end

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

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

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

D
Dmitriy Zaporozhets 已提交
56
    grouped_options_for_select(options, @ref || @project.default_branch)
G
gitlabhq 已提交
57 58
  end

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

    project_nav = []

    if @project && !@project.new_record?
      project_nav = [
72 73 74 75 76
        { 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 已提交
77 78 79 80 81 82
      ]
    end

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

V
vsizov 已提交
83 84 85
  def ldap_enable?
    Devise.omniauth_providers.include?(:ldap)
  end
86

87
  def app_theme
88
    Gitlab::Theme.css_class_by_id(current_user.try(:theme_id))
89
  end
R
randx 已提交
90 91

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

  def tab_class(tab_key)
    active = case tab_key
101

D
Dmitriy Zaporozhets 已提交
102 103 104 105
             # Project Area
             when :wall; wall_tab?
             when :wiki; controller.controller_name == "wikis"
             when :issues; issues_tab?
106
             when :network; current_page?(controller: "projects", action: "graph", id: @project)
D
Dmitriy Zaporozhets 已提交
107 108 109 110 111 112 113 114 115 116
             when :merge_requests; controller.controller_name == "merge_requests"

             # Dashboard Area
             when :help; controller.controller_name == "help"
             when :search; current_page?(search_path)
             when :dash_issues; current_page?(dashboard_issues_path)
             when :dash_mr; current_page?(dashboard_merge_requests_path)
             when :root; current_page?(dashboard_path) || current_page?(root_path)

             # Profile Area
117
             when :profile;  current_page?(controller: "profile", action: :show)
D
Dmitriy Zaporozhets 已提交
118 119
             when :history;  current_page?(controller: "profile", action: :history)
             when :account;  current_page?(controller: "profile", action: :account)
120 121
             when :token;    current_page?(controller: "profile", action: :token)
             when :design;   current_page?(controller: "profile", action: :design)
D
Dmitriy Zaporozhets 已提交
122 123 124 125 126 127
             when :ssh_keys; controller.controller_name == "keys"

             # Admin Area
             when :admin_root;     controller.controller_name == "dashboard"
             when :admin_users;    controller.controller_name == 'users'
             when :admin_projects; controller.controller_name == "projects"
128
             when :admin_hooks;    controller.controller_name == 'hooks'
D
Dmitriy Zaporozhets 已提交
129
             when :admin_resque;   controller.controller_name == 'resque'
130
             when :admin_logs;   controller.controller_name == 'logs'
D
Dmitriy Zaporozhets 已提交
131 132 133 134 135 136

             else
               false
             end
    active ? "current" : nil
  end
137 138 139 140

  def hexdigest(string)
    Digest::SHA1.hexdigest string
  end
141 142 143 144 145 146 147 148 149

  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
150

F
Florian Unglaub 已提交
151
  def authbutton(provider, size = 64)
D
Dmitriy Zaporozhets 已提交
152 153 154
    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 已提交
155
  end
G
gitlabhq 已提交
156
end