s

Ruby code posted by s
created at 18 Jun 13:10

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class RepositoriesController < ApplicationController
  layout 'v2'
  before_filter :require_login

  def index
    @repositories = @user.account.repositories + @user.account.git_repositories
  end
  

  def show
    path = params[:path].join('/')
    if params[:repo_type].to_s == "svn"
      expected_class = Repository
       repo = @overview_repositories.find {|r| 
            r.short_name.nil? == false && 
            r.short_name.downcase == params[:id].to_s.downcase &&
            r.is_a?(expected_class) rescue nil  }
    else
      expected_class = GitRepository
       repo = @overview_repositories.find {|r| 
            r.url.nil? == false && 
            r.url.downcase == params[:id].to_s.downcase &&
            r.is_a?(expected_class) rescue nil  }
    end

   
    if repo.nil?
      throw "ACCESS DENIED"
    end
    
    if params[:repo_type].to_s == "svn"
      entries = repo.scm(@user.user_name, @user.user_password, 0).entries(path)
    else
      entries = repo.scm().entries(path, params[:branch])
    end
    
    # Generate content for a file or a directory
    if requesting_a_file?(entries)
      if params[:repo_type].to_s == "svn"
        @content = render_to_string(:partial => "file", :locals => {
            :file => h(repo.scm(@user.user_name, @user.user_password, 0).cat(path, rev = nil)),
            :repo => get_repo_navigation,
            :filetype => ::CodeRay::FileType.fetch(params[:path].last, :text, true)
          }
        )
       else
          @content = render_to_string(:partial => "file", :locals => {
              :file => h(repo.scm().cat(path, params[:branch])),
              :repo => get_repo_navigation,
              :filetype => ::CodeRay::FileType.fetch(params[:path].last, :text, true)
            }
          )
        end
    else    
      @content = render_to_string(:partial => "fs", :locals => {
          :entries => entries, 
          :repo => get_repo_navigation
        }
      )
    end
    
    render :text => @content if params["_pjax"]
  end
  

  private
  def get_repo_navigation
    config = {}
    
    arrPath = params[:path]
    
    if params[:repo_type].to_s == "svn"
      config[:root_path] = "/svn_repositories/#{params[:id]}"
    else
      config[:root_path] = "/git_repositories/#{params[:id]}/#{params[:branch]}"
    end
    
    config[:previous_path] = config[:root_path] + "/" + arrPath[0...-1].join("/") 
    
    arrBreadcrumb = [{:title => "~", :href => config[:root_path]}]

    arrPath.each_with_index do |part, i|
      next if part.blank? # prevents adding an empty breadcrumb; I don't know
                          # why it happens, but it does... so here's my fix.
                          
      arrBreadcrumb << {:title => part, :href => arrBreadcrumb[arrBreadcrumb.length-1][:href] + "/" + part}
    end
    config[:breadcrumb] = arrBreadcrumb

    config
  end
  
  # Determines if the request is attempting to load a file
  def requesting_a_file?(entries)
    begin
    # If there's more than one entry, it's not a file
    if entries.count > 1
      return false
    end
    
    # If the only entry isn't a file, it's not a file
    if "file" != entries[0].kind
      return false;
    end
    
    # If the last to parts of the path aren't the same, it's not a file
    # ex. TaskList/doc/README_FOR_APP/README_FOR_APP 
    arrPath = entries[0].path.split("/")
    if arrPath[arrPath.count-2] != arrPath[arrPath.count-1]
      return false
    end
    
    return true
  rescue
    true
  end
  end
end
3.56 KB in 8 ms with coderay