Commit 4bf4efe712489090fca4ce14be9cbe7637f4d511

Authored by Dmitriy Zaporozhets
1 parent 6721ef01

decorators & tree model

Gemfile
... ... @@ -21,6 +21,7 @@ gem "git"
21 21 gem "acts_as_list"
22 22 gem 'rdiscount'
23 23 gem 'acts-as-taggable-on', '~> 2.1.0'
  24 +gem 'drapper'
24 25  
25 26 group :assets do
26 27 gem 'sass-rails', "~> 3.1.0"
... ...
Gemfile.lock
... ... @@ -86,6 +86,7 @@ GEM
86 86 orm_adapter (~> 0.0.3)
87 87 warden (~> 1.1)
88 88 diff-lcs (1.1.3)
  89 + drapper (0.8.4)
89 90 erubis (2.7.0)
90 91 eventmachine (0.12.10)
91 92 execjs (1.2.9)
... ... @@ -254,6 +255,7 @@ DEPENDENCIES
254 255 coffee-rails (~> 3.1.0)
255 256 database_cleaner
256 257 devise (= 1.5.0)
  258 + drapper
257 259 faker
258 260 git
259 261 grit!
... ...
app/assets/javascripts/projects.js
... ... @@ -6,7 +6,8 @@ $(document).ready(function(){
6 6 $("#tree-slider tr.tree-item").live('click', function(e){
7 7 if(e.target.nodeName != "A") {
8 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 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 378 body.dashboard.project-page .news-feed .project-updates a.project-update span.update-author{color: #999; font-weight: normal; font-style: italic;}
379 379 body.dashboard.project-page .news-feed .project-updates a.project-update span.update-author strong{font-weight: bold; font-style: normal;}
380 380 /* eo Dashboard Page */
381   -
382   -
... ...
app/controllers/application_controller.rb
... ... @@ -84,4 +84,10 @@ class ApplicationController < ActionController::Base
84 84 nil
85 85 end
86 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 93 end
... ...
app/controllers/refs_controller.rb
... ... @@ -25,16 +25,14 @@ class RefsController < ApplicationController
25 25 @repo = project.repo
26 26  
27 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 31 respond_to do |format|
32   - format.html # show.html.erb
  32 + format.html
33 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 36 end
39 37 end
40 38 rescue
... ...
app/decorators/application_decorator.rb 0 → 100644
... ... @@ -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 @@
  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 @@
  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 @@
  1 +class Commit
  2 +end
... ...
app/models/tree.rb 0 → 100644
... ... @@ -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 1 #tree-breadcrumbs
5 2 %h2.icon
6 3 %span
7 4 %d
8 5 = link_to tree_project_ref_path(@project, @ref, :path => nil), :remote => true do
9 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 11 .right= render :partial => "projects/refs", :locals => { :destination => :tree }
20 12 .clear
21   -
22 13 #tree-content-holder
23   - - if tree.is_a?(Grit::Blob)
  14 + - if tree.is_blob?
24 15 = render :partial => "refs/tree_file", :locals => { :name => tree.name, :content => tree.data, :file => tree }
25 16 - else
26 17 - contents = tree.contents
... ... @@ -30,13 +21,13 @@
30 21 %th Last Update
31 22 %th
32 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 28 %td.tree-item-file-name
38 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 31 %td
41 32 %td
42 33  
... ...