OSDN Git Service

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