OSDN Git Service

decorators & tree model
authorDmitriy Zaporozhets <dzaporozhets@sphereconsultinginc.com>
Sun, 20 Nov 2011 20:32:12 +0000 (22:32 +0200)
committerDmitriy Zaporozhets <dzaporozhets@sphereconsultinginc.com>
Sun, 20 Nov 2011 20:32:12 +0000 (22:32 +0200)
12 files changed:
Gemfile
Gemfile.lock
app/assets/javascripts/projects.js
app/assets/stylesheets/projects.css.scss
app/controllers/application_controller.rb
app/controllers/refs_controller.rb
app/decorators/application_decorator.rb [new file with mode: 0644]
app/decorators/commit_decorator.rb [new file with mode: 0644]
app/decorators/tree_decorator.rb [new file with mode: 0644]
app/models/commit.rb [new file with mode: 0644]
app/models/tree.rb [new file with mode: 0644]
app/views/refs/_tree.html.haml

diff --git a/Gemfile b/Gemfile
index f1f1791..459a8b4 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -21,6 +21,7 @@ gem "git"
 gem "acts_as_list"
 gem 'rdiscount'
 gem 'acts-as-taggable-on', '~> 2.1.0'
+gem 'drapper'
 
 group :assets do
   gem 'sass-rails',   "~> 3.1.0"
index a0d9d66..04c7764 100644 (file)
@@ -86,6 +86,7 @@ GEM
       orm_adapter (~> 0.0.3)
       warden (~> 1.1)
     diff-lcs (1.1.3)
+    drapper (0.8.4)
     erubis (2.7.0)
     eventmachine (0.12.10)
     execjs (1.2.9)
@@ -254,6 +255,7 @@ DEPENDENCIES
   coffee-rails (~> 3.1.0)
   database_cleaner
   devise (= 1.5.0)
+  drapper
   faker
   git
   grit!
index b58cf27..4ffbd14 100644 (file)
@@ -6,7 +6,8 @@ $(document).ready(function(){
   $("#tree-slider tr.tree-item").live('click', function(e){
     if(e.target.nodeName != "A") {
       e.stopPropagation();
-      $(this).find("td.tree-item-file-name a").click();
+      link = $(this).find("td.tree-item-file-name a")
+      link.click();
       return false;
     }
   });
index 9098c19..4c20273 100644 (file)
@@ -378,5 +378,3 @@ body.dashboard.project-page .news-feed .project-updates a.project-update span.up
 body.dashboard.project-page .news-feed .project-updates a.project-update span.update-author{color: #999; font-weight: normal; font-style: italic;}
 body.dashboard.project-page .news-feed .project-updates a.project-update span.update-author strong{font-weight: bold; font-style: normal;}
 /* eo Dashboard Page */
-
-
index 95d9403..b37deaf 100644 (file)
@@ -84,4 +84,10 @@ class ApplicationController < ActionController::Base
       nil
     end
   end
+
+  def no_cache_headers
+    response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
+    response.headers["Pragma"] = "no-cache"
+    response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
+  end
 end
index 0da429b..a694115 100644 (file)
@@ -25,16 +25,14 @@ class RefsController < ApplicationController
     @repo = project.repo
 
     @commit = @repo.commits(@ref).first
-    @tree = @commit.tree
-    @tree = @tree / params[:path] if params[:path]
+    @tree = Tree.new(@commit.tree, project, @ref, params[:path])
+    @tree = TreeDecorator.new(@tree)
 
     respond_to do |format|
-      format.html # show.html.erb
+      format.html
       format.js do
-        # diasbale cache to allow back button works
-        response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
-        response.headers["Pragma"] = "no-cache"
-        response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
+        # disable cache to allow back button works
+        no_cache_headers
       end
     end
   rescue
diff --git a/app/decorators/application_decorator.rb b/app/decorators/application_decorator.rb
new file mode 100644 (file)
index 0000000..8c35cae
--- /dev/null
@@ -0,0 +1,28 @@
+class ApplicationDecorator < Drapper::Base
+  # Lazy Helpers
+  #   PRO: Call Rails helpers without the h. proxy
+  #        ex: number_to_currency(model.price)
+  #   CON: Add a bazillion methods into your decorator's namespace
+  #        and probably sacrifice performance/memory
+  #  
+  #   Enable them by uncommenting this line:
+  #   lazy_helpers
+
+  # Shared Decorations
+  #   Consider defining shared methods common to all your models.
+  #   
+  #   Example: standardize the formatting of timestamps
+  #
+  #   def formatted_timestamp(time)
+  #     h.content_tag :span, time.strftime("%a %m/%d/%y"), 
+  #                   :class => 'timestamp' 
+  #   end
+  # 
+  #   def created_at
+  #     formatted_timestamp(model.created_at)
+  #   end
+  # 
+  #   def updated_at
+  #     formatted_timestamp(model.updated_at)
+  #   end
+end
diff --git a/app/decorators/commit_decorator.rb b/app/decorators/commit_decorator.rb
new file mode 100644 (file)
index 0000000..e4dc027
--- /dev/null
@@ -0,0 +1,7 @@
+class CommitDecorator < ApplicationDecorator
+  decorates :commit
+
+  def breadcrumbs
+
+  end
+end
diff --git a/app/decorators/tree_decorator.rb b/app/decorators/tree_decorator.rb
new file mode 100644 (file)
index 0000000..8379006
--- /dev/null
@@ -0,0 +1,33 @@
+class TreeDecorator < ApplicationDecorator
+  decorates :tree
+
+  def breadcrumbs(max_links = 2)
+    if path
+      part_path = ""
+      parts = path.split("\/")
+
+      yield(h.link_to("..", "#", :remote => :true)) if parts.count > max_links
+
+      parts.each do |part|
+        part_path = File.join(part_path, part) unless part_path.empty?
+        part_path = part if part_path.empty?
+
+        next unless parts.last(2).include?(part) if parts.count > max_links
+        yield(h.link_to(h.truncate(part, :length => 40), h.tree_file_project_ref_path(project, ref, :path => part_path), :remote => :true))
+      end
+    end
+  end
+
+  def up_dir?
+    !!path
+  end
+
+  def up_dir_path
+    file = File.join(path, "..")
+    h.tree_file_project_ref_path(project, ref, file)
+  end
+
+  def history_path
+    h.project_commits_path(project, :path => path, :ref => ref)
+  end
+end
diff --git a/app/models/commit.rb b/app/models/commit.rb
new file mode 100644 (file)
index 0000000..09030ca
--- /dev/null
@@ -0,0 +1,2 @@
+class Commit
+end
diff --git a/app/models/tree.rb b/app/models/tree.rb
new file mode 100644 (file)
index 0000000..032290a
--- /dev/null
@@ -0,0 +1,24 @@
+class Tree
+  attr_accessor :path, :tree, :project, :ref
+
+  delegate :contents,
+    :basename,
+    :name,
+    :data,
+    :text?,
+    :colorize,
+    :to => :tree
+
+  def initialize(raw_tree, project, ref = nil, path = nil)
+    @project, @ref, @path = project, ref, path,
+    @tree = if path
+              raw_tree / path
+            else
+              raw_tree
+            end
+  end
+
+  def is_blob?
+    tree.is_a?(Grit::Blob)
+  end
+end
index 5dd55ee..498acbd 100644 (file)
@@ -1,26 +1,17 @@
--#%a.right.button{:href => "#"} Download
--#-if can? current_user, :admin_project, @project
-  %a.right.button.blue{:href => "#"} EDIT
 #tree-breadcrumbs
   %h2.icon
     %span
     %d
       = link_to tree_project_ref_path(@project, @ref, :path => nil), :remote => true do
         = @project.name
-      - if params[:path]
-        - part_path = ""
-        - params[:path].split("\/").each do |part|
-          - part_path = File.join(part_path, part) unless part_path.empty?
-          - if part_path.empty?
-            - part_path = part
-          \/
-          = link_to truncate(part, :length => 40), tree_file_project_ref_path(@project, @ref, :path => part_path), :remote => :true
-        &nbsp;
+      - tree.breadcrumbs(2) do |link|
+        \/
+        = link
+      &nbsp;
   .right= render :partial => "projects/refs", :locals => { :destination => :tree }
 .clear
-
 #tree-content-holder
-  - if tree.is_a?(Grit::Blob)
+  - if tree.is_blob?
     = render :partial => "refs/tree_file", :locals => { :name => tree.name, :content => tree.data, :file => tree }
   - else
     - contents = tree.contents
         %th Last Update
         %th
           Last commit
-          = link_to "history", project_commits_path(@project, :path => params[:path], :ref => @ref), :class => "right"
-      - if params[:path]
-        - file = File.join(params[:path], "..")
-        %tr{ :class => "tree-item", :url => tree_file_project_ref_path(@project, @ref, file) }
+          = link_to "history", tree.history_path, :class => "right"
+      
+      - if tree.up_dir?
+        %tr{ :class => "tree-item", :url => tree.up_dir_path }
           %td.tree-item-file-name
             = image_tag "dir.png"
-            = link_to "..", tree_file_project_ref_path(@project, @ref, file), :remote => :true
+            = link_to "..", tree.up_dir_path, :remote => :true
           %td
           %td