Commit 4bf4efe712489090fca4ce14be9cbe7637f4d511

Authored by Dmitriy Zaporozhets
1 parent 6721ef01

decorators & tree model

@@ -21,6 +21,7 @@ gem "git" @@ -21,6 +21,7 @@ gem "git"
21 gem "acts_as_list" 21 gem "acts_as_list"
22 gem 'rdiscount' 22 gem 'rdiscount'
23 gem 'acts-as-taggable-on', '~> 2.1.0' 23 gem 'acts-as-taggable-on', '~> 2.1.0'
  24 +gem 'drapper'
24 25
25 group :assets do 26 group :assets do
26 gem 'sass-rails', "~> 3.1.0" 27 gem 'sass-rails', "~> 3.1.0"
@@ -86,6 +86,7 @@ GEM @@ -86,6 +86,7 @@ GEM
86 orm_adapter (~> 0.0.3) 86 orm_adapter (~> 0.0.3)
87 warden (~> 1.1) 87 warden (~> 1.1)
88 diff-lcs (1.1.3) 88 diff-lcs (1.1.3)
  89 + drapper (0.8.4)
89 erubis (2.7.0) 90 erubis (2.7.0)
90 eventmachine (0.12.10) 91 eventmachine (0.12.10)
91 execjs (1.2.9) 92 execjs (1.2.9)
@@ -254,6 +255,7 @@ DEPENDENCIES @@ -254,6 +255,7 @@ DEPENDENCIES
254 coffee-rails (~> 3.1.0) 255 coffee-rails (~> 3.1.0)
255 database_cleaner 256 database_cleaner
256 devise (= 1.5.0) 257 devise (= 1.5.0)
  258 + drapper
257 faker 259 faker
258 git 260 git
259 grit! 261 grit!
app/assets/javascripts/projects.js
@@ -6,7 +6,8 @@ $(document).ready(function(){ @@ -6,7 +6,8 @@ $(document).ready(function(){
6 $("#tree-slider tr.tree-item").live('click', function(e){ 6 $("#tree-slider tr.tree-item").live('click', function(e){
7 if(e.target.nodeName != "A") { 7 if(e.target.nodeName != "A") {
8 e.stopPropagation(); 8 e.stopPropagation();
9 - $(this).find("td.tree-item-file-name a").click(); 9 + link = $(this).find("td.tree-item-file-name a")
  10 + link.click();
10 return false; 11 return false;
11 } 12 }
12 }); 13 });
app/assets/stylesheets/projects.css.scss
@@ -378,5 +378,3 @@ body.dashboard.project-page .news-feed .project-updates a.project-update span.up @@ -378,5 +378,3 @@ body.dashboard.project-page .news-feed .project-updates a.project-update span.up
378 body.dashboard.project-page .news-feed .project-updates a.project-update span.update-author{color: #999; font-weight: normal; font-style: italic;} 378 body.dashboard.project-page .news-feed .project-updates a.project-update span.update-author{color: #999; font-weight: normal; font-style: italic;}
379 body.dashboard.project-page .news-feed .project-updates a.project-update span.update-author strong{font-weight: bold; font-style: normal;} 379 body.dashboard.project-page .news-feed .project-updates a.project-update span.update-author strong{font-weight: bold; font-style: normal;}
380 /* eo Dashboard Page */ 380 /* eo Dashboard Page */
381 -  
382 -  
app/controllers/application_controller.rb
@@ -84,4 +84,10 @@ class ApplicationController < ActionController::Base @@ -84,4 +84,10 @@ class ApplicationController < ActionController::Base
84 nil 84 nil
85 end 85 end
86 end 86 end
  87 +
  88 + def no_cache_headers
  89 + response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
  90 + response.headers["Pragma"] = "no-cache"
  91 + response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
  92 + end
87 end 93 end
app/controllers/refs_controller.rb
@@ -25,16 +25,14 @@ class RefsController < ApplicationController @@ -25,16 +25,14 @@ class RefsController < ApplicationController
25 @repo = project.repo 25 @repo = project.repo
26 26
27 @commit = @repo.commits(@ref).first 27 @commit = @repo.commits(@ref).first
28 - @tree = @commit.tree  
29 - @tree = @tree / params[:path] if params[:path] 28 + @tree = Tree.new(@commit.tree, project, @ref, params[:path])
  29 + @tree = TreeDecorator.new(@tree)
30 30
31 respond_to do |format| 31 respond_to do |format|
32 - format.html # show.html.erb 32 + format.html
33 format.js do 33 format.js do
34 - # diasbale cache to allow back button works  
35 - response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"  
36 - response.headers["Pragma"] = "no-cache"  
37 - response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT" 34 + # disable cache to allow back button works
  35 + no_cache_headers
38 end 36 end
39 end 37 end
40 rescue 38 rescue
app/decorators/application_decorator.rb 0 → 100644
@@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
  1 +class ApplicationDecorator < Drapper::Base
  2 + # Lazy Helpers
  3 + # PRO: Call Rails helpers without the h. proxy
  4 + # ex: number_to_currency(model.price)
  5 + # CON: Add a bazillion methods into your decorator's namespace
  6 + # and probably sacrifice performance/memory
  7 + #
  8 + # Enable them by uncommenting this line:
  9 + # lazy_helpers
  10 +
  11 + # Shared Decorations
  12 + # Consider defining shared methods common to all your models.
  13 + #
  14 + # Example: standardize the formatting of timestamps
  15 + #
  16 + # def formatted_timestamp(time)
  17 + # h.content_tag :span, time.strftime("%a %m/%d/%y"),
  18 + # :class => 'timestamp'
  19 + # end
  20 + #
  21 + # def created_at
  22 + # formatted_timestamp(model.created_at)
  23 + # end
  24 + #
  25 + # def updated_at
  26 + # formatted_timestamp(model.updated_at)
  27 + # end
  28 +end
app/decorators/commit_decorator.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +class CommitDecorator < ApplicationDecorator
  2 + decorates :commit
  3 +
  4 + def breadcrumbs
  5 +
  6 + end
  7 +end
app/decorators/tree_decorator.rb 0 → 100644
@@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
  1 +class TreeDecorator < ApplicationDecorator
  2 + decorates :tree
  3 +
  4 + def breadcrumbs(max_links = 2)
  5 + if path
  6 + part_path = ""
  7 + parts = path.split("\/")
  8 +
  9 + yield(h.link_to("..", "#", :remote => :true)) if parts.count > max_links
  10 +
  11 + parts.each do |part|
  12 + part_path = File.join(part_path, part) unless part_path.empty?
  13 + part_path = part if part_path.empty?
  14 +
  15 + next unless parts.last(2).include?(part) if parts.count > max_links
  16 + yield(h.link_to(h.truncate(part, :length => 40), h.tree_file_project_ref_path(project, ref, :path => part_path), :remote => :true))
  17 + end
  18 + end
  19 + end
  20 +
  21 + def up_dir?
  22 + !!path
  23 + end
  24 +
  25 + def up_dir_path
  26 + file = File.join(path, "..")
  27 + h.tree_file_project_ref_path(project, ref, file)
  28 + end
  29 +
  30 + def history_path
  31 + h.project_commits_path(project, :path => path, :ref => ref)
  32 + end
  33 +end
app/models/commit.rb 0 → 100644
@@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
  1 +class Commit
  2 +end
app/models/tree.rb 0 → 100644
@@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
  1 +class Tree
  2 + attr_accessor :path, :tree, :project, :ref
  3 +
  4 + delegate :contents,
  5 + :basename,
  6 + :name,
  7 + :data,
  8 + :text?,
  9 + :colorize,
  10 + :to => :tree
  11 +
  12 + def initialize(raw_tree, project, ref = nil, path = nil)
  13 + @project, @ref, @path = project, ref, path,
  14 + @tree = if path
  15 + raw_tree / path
  16 + else
  17 + raw_tree
  18 + end
  19 + end
  20 +
  21 + def is_blob?
  22 + tree.is_a?(Grit::Blob)
  23 + end
  24 +end
app/views/refs/_tree.html.haml
1 --#%a.right.button{:href => "#"} Download  
2 --#-if can? current_user, :admin_project, @project  
3 - %a.right.button.blue{:href => "#"} EDIT  
4 #tree-breadcrumbs 1 #tree-breadcrumbs
5 %h2.icon 2 %h2.icon
6 %span 3 %span
7 %d 4 %d
8 = link_to tree_project_ref_path(@project, @ref, :path => nil), :remote => true do 5 = link_to tree_project_ref_path(@project, @ref, :path => nil), :remote => true do
9 = @project.name 6 = @project.name
10 - - if params[:path]  
11 - - part_path = ""  
12 - - params[:path].split("\/").each do |part|  
13 - - part_path = File.join(part_path, part) unless part_path.empty?  
14 - - if part_path.empty?  
15 - - part_path = part  
16 - \/  
17 - = link_to truncate(part, :length => 40), tree_file_project_ref_path(@project, @ref, :path => part_path), :remote => :true  
18 - &nbsp; 7 + - tree.breadcrumbs(2) do |link|
  8 + \/
  9 + = link
  10 + &nbsp;
19 .right= render :partial => "projects/refs", :locals => { :destination => :tree } 11 .right= render :partial => "projects/refs", :locals => { :destination => :tree }
20 .clear 12 .clear
21 -  
22 #tree-content-holder 13 #tree-content-holder
23 - - if tree.is_a?(Grit::Blob) 14 + - if tree.is_blob?
24 = render :partial => "refs/tree_file", :locals => { :name => tree.name, :content => tree.data, :file => tree } 15 = render :partial => "refs/tree_file", :locals => { :name => tree.name, :content => tree.data, :file => tree }
25 - else 16 - else
26 - contents = tree.contents 17 - contents = tree.contents
@@ -30,13 +21,13 @@ @@ -30,13 +21,13 @@
30 %th Last Update 21 %th Last Update
31 %th 22 %th
32 Last commit 23 Last commit
33 - = link_to "history", project_commits_path(@project, :path => params[:path], :ref => @ref), :class => "right"  
34 - - if params[:path]  
35 - - file = File.join(params[:path], "..")  
36 - %tr{ :class => "tree-item", :url => tree_file_project_ref_path(@project, @ref, file) } 24 + = link_to "history", tree.history_path, :class => "right"
  25 +
  26 + - if tree.up_dir?
  27 + %tr{ :class => "tree-item", :url => tree.up_dir_path }
37 %td.tree-item-file-name 28 %td.tree-item-file-name
38 = image_tag "dir.png" 29 = image_tag "dir.png"
39 - = link_to "..", tree_file_project_ref_path(@project, @ref, file), :remote => :true 30 + = link_to "..", tree.up_dir_path, :remote => :true
40 %td 31 %td
41 %td 32 %td
42 33