OSDN Git Service

Remove Projects#team action
[wvm/gitlab.git] / app / helpers / application_helper.rb
1 require 'digest/md5'
2 module ApplicationHelper
3
4   def gravatar_icon(user_email = '', size = 40)
5     if Gitlab.config.disable_gravatar? || user_email.blank?
6       'no_avatar.png'
7     else
8       gravatar_prefix = request.ssl? ? "https://secure" : "http://www"
9       user_email.strip!
10       "#{gravatar_prefix}.gravatar.com/avatar/#{Digest::MD5.hexdigest(user_email.downcase)}?s=#{size}&d=identicon"
11     end
12   end
13
14   def request_protocol
15     request.ssl? ? "https" : "http"
16   end
17
18   def web_app_url
19     "#{request_protocol}://#{Gitlab.config.web_host}/"
20   end
21
22   def last_commit(project)
23     if project.repo_exists?
24       time_ago_in_words(project.commit.committed_date) + " ago"
25     else
26       "Never"
27     end
28   rescue
29     "Never"
30   end
31
32   def grouped_options_refs(destination = :tree)
33     options = [
34       ["Branch", @project.repo.heads.map(&:name) ],
35       [ "Tag", @project.tags ]
36     ]
37
38     # If reference is commit id -
39     # we should add it to branch/tag selectbox
40     if(@ref && !options.flatten.include?(@ref) &&
41        @ref =~ /^[0-9a-zA-Z]{6,52}$/)
42       options << ["Commit", [@ref]]
43     end
44
45     grouped_options_for_select(options, @ref || @project.default_branch)
46   end
47
48   def search_autocomplete_source
49     projects = current_user.projects.map{ |p| { label: p.name, url: project_path(p) } }
50     default_nav = [
51       { label: "Profile", url: profile_path },
52       { label: "Keys", url: keys_path },
53       { label: "Dashboard", url: root_path },
54       { label: "Admin", url: admin_root_path }
55     ]
56
57     project_nav = []
58
59     if @project && !@project.new_record?
60       project_nav = [
61         { label: "#{@project.name} / Issues", url: project_issues_path(@project) },
62         { label: "#{@project.name} / Wall", url: wall_project_path(@project) },
63         { label: "#{@project.name} / Tree", url: tree_project_ref_path(@project, @project.root_ref) },
64         { label: "#{@project.name} / Commits", url: project_commits_path(@project) },
65         { label: "#{@project.name} / Team", url: project_team_index_path(@project) }
66       ]
67     end
68
69     [projects, default_nav, project_nav].flatten.to_json
70   end
71
72   def ldap_enable?
73     Devise.omniauth_providers.include?(:ldap)
74   end
75
76   def app_theme
77     Gitlab::Theme.css_class_by_id(current_user.try(:theme_id))
78   end
79
80   def show_last_push_widget?(event)
81     event &&
82       event.last_push_to_non_root? &&
83       !event.rm_ref? &&
84       event.project &&
85       event.project.merge_requests_enabled
86   end
87
88   def tab_class(tab_key)
89     active = case tab_key
90
91              # Project Area
92              when :wall; wall_tab?
93              when :wiki; controller.controller_name == "wikis"
94              when :issues; issues_tab?
95              when :network; current_page?(controller: "projects", action: "graph", id: @project)
96              when :merge_requests; controller.controller_name == "merge_requests"
97
98              # Dashboard Area
99              when :help; controller.controller_name == "help"
100              when :search; current_page?(search_path)
101              when :dash_issues; current_page?(dashboard_issues_path)
102              when :dash_mr; current_page?(dashboard_merge_requests_path)
103              when :root; current_page?(dashboard_path) || current_page?(root_path)
104
105              # Profile Area
106              when :profile;  current_page?(controller: "profile", action: :show)
107              when :history;  current_page?(controller: "profile", action: :history)
108              when :account;  current_page?(controller: "profile", action: :account)
109              when :token;    current_page?(controller: "profile", action: :token)
110              when :design;   current_page?(controller: "profile", action: :design)
111              when :ssh_keys; controller.controller_name == "keys"
112
113              # Admin Area
114              when :admin_root;     controller.controller_name == "dashboard"
115              when :admin_users;    controller.controller_name == 'users'
116              when :admin_projects; controller.controller_name == "projects"
117              when :admin_hooks;    controller.controller_name == 'hooks'
118              when :admin_resque;   controller.controller_name == 'resque'
119              when :admin_logs;   controller.controller_name == 'logs'
120
121              else
122                false
123              end
124     active ? "current" : nil
125   end
126
127   def hexdigest(string)
128     Digest::SHA1.hexdigest string
129   end
130
131   def project_last_activity project
132     activity = project.last_activity
133     if activity && activity.created_at
134       time_ago_in_words(activity.created_at) + " ago"
135     else
136       "Never"
137     end
138   end
139
140   def authbutton(provider, size = 64)
141     file_name = "#{provider.to_s.split('_').first}_#{size}.png"
142     image_tag("authbuttons/#{file_name}",
143               alt: "Sign in with #{provider.to_s.titleize}")
144   end
145 end