OSDN Git Service

Merge branch 'master' into stable
[wvm/gitlab.git] / app / helpers / application_helper.rb
1 require 'digest/md5'
2
3 module ApplicationHelper
4
5   # Check if a particular controller is the current one
6   #
7   # args - One or more controller names to check
8   #
9   # Examples
10   #
11   #   # On TreeController
12   #   current_controller?(:tree)           # => true
13   #   current_controller?(:commits)        # => false
14   #   current_controller?(:commits, :tree) # => true
15   def current_controller?(*args)
16     args.any? { |v| v.to_s.downcase == controller.controller_name }
17   end
18
19   # Check if a partcular action is the current one
20   #
21   # args - One or more action names to check
22   #
23   # Examples
24   #
25   #   # On Projects#new
26   #   current_action?(:new)           # => true
27   #   current_action?(:create)        # => false
28   #   current_action?(:new, :create)  # => true
29   def current_action?(*args)
30     args.any? { |v| v.to_s.downcase == action_name }
31   end
32
33   def gravatar_icon(user_email = '', size = 40)
34     if Gitlab.config.disable_gravatar? || user_email.blank?
35       'no_avatar.png'
36     else
37       gravatar_prefix = request.ssl? ? "https://secure" : "http://www"
38       user_email.strip!
39       "#{gravatar_prefix}.gravatar.com/avatar/#{Digest::MD5.hexdigest(user_email.downcase)}?s=#{size}&d=mm"
40     end
41   end
42
43   def request_protocol
44     request.ssl? ? "https" : "http"
45   end
46
47   def web_app_url
48     "#{request_protocol}://#{Gitlab.config.web_host}/"
49   end
50
51   def last_commit(project)
52     if project.repo_exists?
53       time_ago_in_words(project.commit.committed_date) + " ago"
54     else
55       "Never"
56     end
57   rescue
58     "Never"
59   end
60
61   def grouped_options_refs(destination = :tree)
62     options = [
63       ["Branch", @project.branch_names ],
64       [ "Tag", @project.tag_names ]
65     ]
66
67     # If reference is commit id -
68     # we should add it to branch/tag selectbox
69     if(@ref && !options.flatten.include?(@ref) &&
70        @ref =~ /^[0-9a-zA-Z]{6,52}$/)
71       options << ["Commit", [@ref]]
72     end
73
74     grouped_options_for_select(options, @ref || @project.default_branch)
75   end
76
77   def search_autocomplete_source
78     projects = current_user.projects.map{ |p| { label: p.name, url: project_path(p) } }
79
80     default_nav = [
81       { label: "My Profile", url: profile_path },
82       { label: "My SSH Keys", url: keys_path },
83       { label: "My Dashboard", url: root_path },
84       { label: "Admin Section", url: admin_root_path },
85     ]
86
87     help_nav = [
88       { label: "Workflow Help", url: help_workflow_path },
89       { label: "Permissions Help", url: help_permissions_path },
90       { label: "Web Hooks Help", url: help_web_hooks_path },
91       { label: "System Hooks Help", url: help_system_hooks_path },
92       { label: "API Help", url: help_api_path },
93       { label: "Markdown Help", url: help_markdown_path },
94       { label: "SSH Keys Help", url: help_ssh_path },
95     ]
96
97     project_nav = []
98     if @project && !@project.new_record?
99       project_nav = [
100         { label: "#{@project.name} Issues",   url: project_issues_path(@project) },
101         { label: "#{@project.name} Commits",  url: project_commits_path(@project, @ref || @project.root_ref) },
102         { label: "#{@project.name} Merge Requests", url: project_merge_requests_path(@project) },
103         { label: "#{@project.name} Milestones", url: project_milestones_path(@project) },
104         { label: "#{@project.name} Snippets", url: project_snippets_path(@project) },
105         { label: "#{@project.name} Team",     url: project_team_index_path(@project) },
106         { label: "#{@project.name} Tree",     url: project_tree_path(@project, @ref || @project.root_ref) },
107         { label: "#{@project.name} Wall",     url: wall_project_path(@project) },
108         { label: "#{@project.name} Wiki",     url: project_wikis_path(@project) },
109       ]
110     end
111
112     [projects, default_nav, project_nav, help_nav].flatten.to_json
113   end
114
115   def emoji_autocomplete_source
116     # should be an array of strings
117     # so to_s can be called, because it is sufficient and to_json is too slow
118     Emoji.names.to_s
119   end
120
121   def ldap_enable?
122     Devise.omniauth_providers.include?(:ldap)
123   end
124
125   def app_theme
126     Gitlab::Theme.css_class_by_id(current_user.try(:theme_id))
127   end
128
129   def show_last_push_widget?(event)
130     event &&
131       event.last_push_to_non_root? &&
132       !event.rm_ref? &&
133       event.project &&
134       event.project.merge_requests_enabled
135   end
136
137   def hexdigest(string)
138     Digest::SHA1.hexdigest string
139   end
140
141   def project_last_activity project
142     activity = project.last_activity
143     if activity && activity.created_at
144       time_ago_in_words(activity.created_at) + " ago"
145     else
146       "Never"
147     end
148   end
149
150   def authbutton(provider, size = 64)
151     file_name = "#{provider.to_s.split('_').first}_#{size}.png"
152     image_tag("authbuttons/#{file_name}",
153               alt: "Sign in with #{provider.to_s.titleize}")
154   end
155 end