Added method to link file path to blob
[gitorious:fonsecas-clone.git] / app / helpers / application_helper.rb
1 # Methods added to this helper will be available to all templates in the application.
2 module ApplicationHelper
3   include TagsHelper
4   
5   def default_css_tag_sizes
6     %w(tag_size_1 tag_size_2 tag_size_3 tag_size_4)
7   end
8   
9   def linked_tag_list_as_sentence(tags)
10     tags.map do |tag|
11       link_to(h(tag.name), search_path(:q => "category:#{h(tag.name)}"))
12     end.to_sentence
13   end
14   
15   def build_notice_for(object)
16     out =  %Q{<div class="being_constructed">}
17     out << %Q{  <p>This #{object.class.name.humanize.downcase} is being created,<br />}
18     out << %Q{  it will be ready pretty soon&hellip;</p>}
19     out << %Q{</div>}
20     out
21   end
22   
23   def render_if_ready(object, &blk)
24     if object.respond_to?(:ready?) && object.ready?
25       yield
26     else
27       concat(build_notice_for(object), blk.binding)
28     end
29   end  
30   
31   def selected_if_current_page(url_options, slack = false)
32     if slack
33       if controller.request.request_uri.index(CGI.escapeHTML(url_for(url_options))) == 0
34         "selected"
35       end
36     else
37       "selected" if current_page?(url_options)
38     end
39   end
40   
41   def submenu_selected_class_if_current?(section)
42     case section
43     when :overview
44      if %w[projects].include?(controller.controller_name )
45        return "selected"
46      end
47     when :repositories
48       if %w[repositories trees logs commits comitters comments merge_requests 
49             blobs committers].include?(controller.controller_name )
50         return "selected"
51       end
52     end
53   end
54   
55   def link_to_with_selected(name, options = {}, html_options = nil)
56     html_options = current_page?(options) ? {:class => "selected"} : nil
57     link_to(name, options = {}, html_options)
58   end
59   
60   def syntax_themes_css
61     out = []
62     if @load_syntax_themes
63       # %w[ active4d all_hallows_eve amy blackboard brilliance_black brilliance_dull 
64       #     cobalt dawn eiffel espresso_libre idle iplastic lazy mac_classic 
65       #     magicwb_amiga pastels_on_dark slush_poppies spacecadet sunburst 
66       #     twilight zenburnesque 
67       # ].each do |syntax|
68       #   out << stylesheet_link_tag("syntax_themes/#{syntax}")
69       # end
70       return stylesheet_link_tag("syntax_themes/idle")
71     end
72     out.join("\n")
73   end
74   
75   def base_url(full_url)
76     URI.parse(full_url).host
77   end
78   
79   def gravatar_url_for(email, options = {})
80     "http://www.gravatar.com/avatar.php?gravatar_id=" << 
81     Digest::MD5.hexdigest(email) << 
82     "&amp;default=" <<
83     u("http://#{request.host}:#{request.port}/images/default_face.gif") <<
84     options.map { |k,v| "&amp;#{k}=#{v}" }.join
85   end
86   
87   def gravatar(email, options = {})
88     size = options[:size]
89     image_options = { :alt => "avatar" }
90     if size
91       image_options.merge!(:width => size, :height => size)
92     end
93     image_tag(gravatar_url_for(email, options), image_options)
94   end
95   
96   def gravatar_frame(email, options = {})
97     extra_css_class = options[:style] ? " gravatar_#{options[:style]}" : ""
98     %{<div class="gravatar#{extra_css_class}">#{gravatar(email, options)}</div>}
99   end
100   
101   def flashes
102     flash.map { |type, content| content_tag(:div, content_tag(:p, content), :class => "flash_message #{type}")}
103   end
104   
105   def commit_graph_tag(repository, ref = "master")
106     filename = Gitorious::Graphs::CommitsBuilder.filename(repository, ref)
107     if File.exist?(File.join(Gitorious::Graphs::Builder.graph_dir, filename))
108       image_tag("graphs/#{filename}")
109     end
110   end
111   
112   def commit_graph_by_author_tag(repository, ref = "master")    
113     filename = Gitorious::Graphs::CommitsByAuthorBuilder.filename(repository, ref)
114     if File.exist?(File.join(Gitorious::Graphs::Builder.graph_dir, filename))
115       image_tag("graphs/#{filename}")
116     end
117   end
118   
119   def action_and_body_for_event(event)
120     target = event.target
121     action = ""
122     body = ""
123     category = ""
124     case event.action
125       when Action::CREATE_PROJECT
126         action = "<strong>created project</strong> #{link_to h(target.title), project_path(target)}"
127         body = truncate(target.stripped_description, 100)
128         category = "project"
129       when Action::DELETE_PROJECT
130         action = "<strong>deleted project</strong> #{h(event.data)}"
131         category = "project"
132       when Action::UPDATE_PROJECT
133         action = "<strong>updated project</strong> #{link_to h(target.title), project_path(target)}"
134         category = "project"
135       when Action::CLONE_REPOSITORY
136         original_repo = Repository.find_by_id(event.data.to_i)
137         next if original_repo.nil?
138         
139         project = target.project
140         
141         action = "<strong>forked</strong> #{link_to h(project.title), project_path(project)}/#{link_to h(original_repo.name), project_repository_url(project, original_repo)} in #{link_to h(target.name), project_repository_url(project, target)}"
142         category = "repository"
143       when Action::DELETE_REPOSITORY
144         action = "<strong>deleted repository</strong> #{link_to h(target.title), project_path(target)}/#{event.data}"
145         category = "project"
146       when Action::COMMIT
147         project = target.project
148         action = "<strong>committed</strong> #{link_to event.data[0,8], project_repository_commit_path(project, target, event.data)} to #{link_to h(project.slug), project_path(project)}/#{link_to h(target.name), project_repository_url(project, target)}"
149         body = "<code>#{truncate(event.body, 150)}</code>"
150         category = "commit"
151       when Action::CREATE_BRANCH
152         project = target.project
153         if event.data == "master"
154           action = "<strong>started development</strong> of #{link_to h(project.title), project_path(project)}/#{link_to h(target.name), project_repository_url(project, target)}"
155           body = event.body
156         else
157           action = "<strong>created branch</strong> #{link_to h(event.data), project_repository_tree_path(project, target, event.data)} on #{link_to h(project.title), project_path(project)}/#{link_to h(target.name), project_repository_url(project, target)}"
158         end
159         category = "commit"
160       when Action::DELETE_BRANCH
161         project = target.project
162         action = "<strong>deleted branch</strong> #{event.data} on #{link_to h(project.title), project_path(project)}/#{link_to h(target.name), project_repository_url(project, target)}"
163         category = "commit"
164       when Action::CREATE_TAG
165         project = target.project
166         action = "<strong>tagged</strong> #{link_to h(project.title), project_path(project)}/#{link_to h(target.name), project_repository_url(project, target)}"
167         body = "#{link_to event.data, project_repository_commit_path(project, target, event.data)}<br/>#{event.body}"
168         category = "commit"
169       when Action::DELETE_TAG
170         project = target.project
171         action = "<strong>deleted tag</strong> #{event.data} on #{link_to h(project.title), project_path(project)}/#{link_to h(target.name), project_repository_url(project, target)}"
172         category = "commit"
173       when Action::ADD_COMMITTER
174         user = target.user
175         repo = target.repository
176         action = "<strong>added committer</strong> #{link_to user.login, user_path(user)} to #{link_to h(repo.project.title), project_path(repo.project)}/#{link_to h(repo.name), project_repository_url(repo.project, repo)}"
177         category = "repository"
178       when Action::REMOVE_COMMITTER
179         user = User.find_by_id(event.data.to_i)
180         next unless user
181         
182         project = target.project
183         action = "<strong>removed committer</strong> #{link_to user.login, user_path(user)} from #{link_to h(project.title), project_path(project)}/#{link_to h(target.name), project_repository_url(project, target)}"
184         category = "repository"
185       when Action::COMMENT
186         project = target.project
187         repo = target.repository
188         
189         action = "<strong>commented</strong> on #{link_to h(project.title), project_path(project)}/#{link_to h(repo.name), project_repository_url(project, repo)}"
190         body = truncate(h(target.body), 150)
191         category = "comment"
192       when Action::REQUEST_MERGE
193         source_repository = target.source_repository
194         project = source_repository.project
195         target_repository = target.target_repository
196         
197         action = "<strong>requested merge</strong> #{link_to h(project.title), project_path(project)}/#{link_to h(source_repository.name), project_repository_url(project, source_repository)} to #{link_to h(project.title), project_path(project)}/#{link_to h(target_repository.name)}"
198         body = "#{link_to "review", [project, target_repository, target]}<br/>#{truncate(h(target.proposal), 100)}"
199         category = "merge request"
200       when Action::RESOLVE_MERGE_REQUEST
201         source_repository = target.source_repository
202         project = source_repository.project
203         target_repository = target.target_repository
204         
205         action = "<strong>resolved merge request </strong>to [#{target.status_string}] from #{link_to h(project.title), project_path(project)}/#{link_to h(source_repository.name), project_repository_url(project, source_repository)}"
206         category = "merge_request"
207       when Action::UPDATE_MERGE_REQUEST
208         source_repository = target.source_repository
209         project = source_repository.project
210         target_repository = target.target_repository
211         
212         action = "<strong>updated merge request</strong> from #{link_to h(project.title), project_path(project)}/#{link_to h(source_repository.name), project_repository_url(project, source_repository)}"
213         category = "merge_request"
214       when Action::DELETE_MERGE_REQUEST
215         project = target.project
216         
217         action = "<strong>deleted merge request</strong> from #{link_to h(project.title), project_path(project)}/#{link_to h(target.name), project_repository_url(project, target)}"
218         category = "merge_request"
219     end
220       
221     [action, body, category]
222   end
223   
224   def sidebar_content?
225     !@content_for_sidebar.blank?
226   end
227   
228   def render_readme(repository)
229     possibilities = []
230     repository.git.git.ls_tree({:name_only => true}, "master").each do |line|
231       possibilities << line[0, line.length-1] if line =~ /README.*/
232     end
233     
234     return "" if possibilities.empty?
235     text = repository.git.git.show({}, "master:#{possibilities.first}")
236     markdown(text) rescue text.gsub("\n", "<br/>")
237   end
238   
239   def file_path(repository, filename, head = "master")
240     project_repository_blob_path(repository.project, repository, head, filename)
241   end
242 end