From 24f04c0a6154c6d5890642fb8a8ec16b44146547 Mon Sep 17 00:00:00 2001
From: gitlabhq <m@gitlabhq.com>
Date: Fri, 21 Oct 2011 14:03:34 +0300
Subject: [PATCH] recent radio button

---
 app/controllers/projects_controller.rb | 34 ++++++++++++--------------
 app/models/note.rb                     |  1 +
 app/models/project.rb                  | 28 +++++++++++++++++++++
 app/views/projects/show.html.haml      |  7 ++++--
 app/views/projects/wall.html.haml      |  5 +++-
 spec/requests/projects_spec.rb         |  2 +-
 6 files changed, 55 insertions(+), 22 deletions(-)

diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index dc94bae2c71b8..5ccfe4bb3429d 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -60,24 +60,21 @@ def update
   end
 
   def show
-    if @project.repo_exists?
-      @date = case params[:view]
-              when "week" then Date.today - 7.days
-              else Date.today
-              end.at_beginning_of_day
-
-      @heads = @project.repo.heads
-      @commits = @heads.map do |h| 
-        @project.repo.log(h.name, nil, :since => @date)
-      end.flatten.uniq { |c| c.id }
-
-      @commits.sort! do |x, y|
-        y.committed_date <=> x.committed_date
-      end
+    return render "projects/empty" unless @project.repo_exists?
+    @date = case params[:view]
+            when "week" then Date.today - 7.days
+            when "day" then Date.today
+            else nil
+            end
+
+    if @date
+      @date = @date.at_beginning_of_day
 
+      @commits = @project.commits_since(@date)
       @messages = project.notes.since(@date).order("created_at DESC")
-    else 
-      return render "projects/empty"
+    else
+      @commits = @project.fresh_commits
+      @messages = project.notes.fresh.limit(10)
     end
   end
 
@@ -89,11 +86,12 @@ def wall
     @date = case params[:view]
             when "week" then Date.today - 7.days
             when "all" then nil
-            else Date.today
+            when "day" then Date.today
+            else nil
             end
 
     @notes = @project.common_notes.order("created_at DESC")
-    @notes = @notes.since(@date.at_beginning_of_day) if @date
+    @notes = @date ? @notes.since(@date.at_beginning_of_day) : @notes.fresh.limit(10)
     @note = Note.new
   end
 
diff --git a/app/models/note.rb b/app/models/note.rb
index 71fd9dcd13649..e3dabce479104 100644
--- a/app/models/note.rb
+++ b/app/models/note.rb
@@ -24,6 +24,7 @@ class Note < ActiveRecord::Base
 
   scope :last_week, where("created_at  >= :date", :date => (Date.today - 7.days))
   scope :since, lambda { |day| where("created_at  >= :date", :date => (day)) }
+  scope :fresh, order("created_at DESC")
 
   mount_uploader :attachment, AttachmentUploader
 end
diff --git a/app/models/project.rb b/app/models/project.rb
index f51bd9b3ad48f..d70b18e7934f7 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -126,6 +126,34 @@ def commit(commit_id = nil)
     end
   end
 
+  def heads 
+    @heads ||= repo.heads
+  end
+
+  def fresh_commits
+    commits = heads.map do |h| 
+      repo.commits(h.name, 10)
+    end.flatten.uniq { |c| c.id }
+
+    commits.sort! do |x, y|
+      y.committed_date <=> x.committed_date
+    end
+
+    commits[0..10]
+  end
+
+  def commits_since(date)
+    commits = heads.map do |h| 
+      repo.log(h.name, nil, :since => date)
+    end.flatten.uniq { |c| c.id }
+
+    commits.sort! do |x, y|
+      y.committed_date <=> x.committed_date
+    end
+
+    commits
+  end
+
   def tree(fcommit, path = nil)
     fcommit = commit if fcommit == :head
     tree = fcommit.tree
diff --git a/app/views/projects/show.html.haml b/app/views/projects/show.html.haml
index ff6078c15a0a9..85019ecbe5dbc 100644
--- a/app/views/projects/show.html.haml
+++ b/app/views/projects/show.html.haml
@@ -1,9 +1,12 @@
 %div
-  %h2.left Recent history
+  %h2.left History
   .right
     = form_tag project_path(@project), :method => :get do
       .span-2
-        = radio_button_tag :view, "day", (params[:view] || "day") == "day", :onclick => "this.form.submit()", :id => "day_view"
+        = radio_button_tag :view, "recent", (params[:view] || "recent") == "recent", :onclick => "this.form.submit()", :id => "recent_view"
+        = label_tag "recent_view","Recent"
+      .span-2
+        = radio_button_tag :view, "day", params[:view] == "day", :onclick => "this.form.submit()", :id => "day_view"
         = label_tag "day_view","Today"
       .span-2
         = radio_button_tag :view, "week", params[:view] == "week", :onclick => "this.form.submit()", :id => "week_view"
diff --git a/app/views/projects/wall.html.haml b/app/views/projects/wall.html.haml
index d3dcb3530aa66..ed22478c9243c 100644
--- a/app/views/projects/wall.html.haml
+++ b/app/views/projects/wall.html.haml
@@ -4,7 +4,10 @@
   .right
     = form_tag wall_project_path(@project), :method => :get do
       .span-2
-        = radio_button_tag :view, "day", (params[:view] || "day") == "day", :onclick => "this.form.submit()", :id => "day_view"
+        = radio_button_tag :view, "recent", (params[:view] || "recent") == "recent", :onclick => "this.form.submit()", :id => "recent_view"
+        = label_tag "recent_view","Recent"
+      .span-2
+        = radio_button_tag :view, "day", params[:view] == "day", :onclick => "this.form.submit()", :id => "day_view"
         = label_tag "day_view","Today"
       .span-2
         = radio_button_tag :view, "week", params[:view] == "week", :onclick => "this.form.submit()", :id => "week_view"
diff --git a/spec/requests/projects_spec.rb b/spec/requests/projects_spec.rb
index a1bac0624048c..945c1ea26f592 100644
--- a/spec/requests/projects_spec.rb
+++ b/spec/requests/projects_spec.rb
@@ -73,7 +73,7 @@
     end
 
     it "should beahave like dashboard" do 
-      page.should have_content("Recent history")
+      page.should have_content("History")
     end
 
   end
-- 
GitLab