]> source.dussan.org Git - redmine.git/commitdiff
Change the TimelogController's to/from dates based on the project time entries
authorEric Davis <edavis@littlestreamsoftware.com>
Tue, 14 Sep 2010 19:02:25 +0000 (19:02 +0000)
committerEric Davis <edavis@littlestreamsoftware.com>
Tue, 14 Sep 2010 19:02:25 +0000 (19:02 +0000)
Instead of looking for the earliest and latest time entry system wide for the
dates in the form, now TimelogController will only look at the time entries
for the current project (and parent/sub projects).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4087 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/timelog_controller.rb
app/models/project.rb
app/models/time_entry.rb
test/functional/timelog_controller_test.rb
test/unit/time_entry_test.rb

index 726c69d5b31bf66d34539a358248b3f7dc428c9a..45f41ae5ea400eeb4cd61fbaf426a88762e25459 100644 (file)
@@ -260,8 +260,8 @@ private
     end
     
     @from, @to = @to, @from if @from && @to && @from > @to
-    @from ||= (TimeEntry.earilest_date_for_project || Date.today) - 1
-    @to   ||= (TimeEntry.latest_date_for_project || Date.today)
+    @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today)
+    @to   ||= (TimeEntry.latest_date_for_project(@project) || Date.today)
   end
 
   def load_available_criterias
index 40898a34eb649cf96f29230362409a0ef61e9882..4b0236b37486ffbaadf52aecdfbc34a9cc12c077 100644 (file)
@@ -493,6 +493,15 @@ class Project < ActiveRecord::Base
       enabled_modules.clear
     end
   end
+
+  # Returns an array of projects that are in this project's hierarchy
+  #
+  # Example: parents, children, siblings
+  def hierarchy
+    parents = project.self_and_ancestors || []
+    descendants = project.descendants || []
+    project_hierarchy = parents | descendants # Set union
+  end
   
   # Returns an auto-generated project identifier based on the last identifier used
   def self.next_identifier
index 56801a4ca856c3368504001ca998be5bc563b28d..9bf9708910e948bfd95f02fd89574522408f97d8 100644 (file)
@@ -82,11 +82,19 @@ class TimeEntry < ActiveRecord::Base
     end
   end
 
-  def self.earilest_date_for_project
-    TimeEntry.minimum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries))
+  def self.earilest_date_for_project(project=nil)
+    finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
+    if project
+      finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
+    end
+    TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
   end
 
-  def self.latest_date_for_project
-    TimeEntry.maximum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries))
+  def self.latest_date_for_project(project=nil)
+    finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
+    if project
+      finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
+    end
+    TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
   end
 end
index 9e30a0ae82ce92c18334dc8456c4b26fbc7a01b9..0559a95ca55f83bb50e01b3627492b6eb4d0f67c 100644 (file)
@@ -283,7 +283,7 @@ class TimelogControllerTest < ActionController::TestCase
     assert_not_nil assigns(:total_hours)
     assert_equal "162.90", "%.2f" % assigns(:total_hours)
     # display all time by default
-    assert_equal '2007-03-11'.to_date, assigns(:from)
+    assert_equal '2007-03-12'.to_date, assigns(:from)
     assert_equal '2007-04-22'.to_date, assigns(:to)
   end
   
@@ -325,8 +325,8 @@ class TimelogControllerTest < ActionController::TestCase
     assert_equal 2, assigns(:entries).size
     assert_not_nil assigns(:total_hours)
     assert_equal 154.25, assigns(:total_hours)
-    # display all time by default
-    assert_equal '2007-03-11'.to_date, assigns(:from)
+    # display all time based on what's been logged
+    assert_equal '2007-03-12'.to_date, assigns(:from)
     assert_equal '2007-04-22'.to_date, assigns(:to)
   end
   
index a541fc41fab4dea8ef41946b74fdfea8b04c98b1..3069f9368fdf89e6b19bf0d9c113bbc36a959b77 100644 (file)
@@ -50,17 +50,50 @@ class TimeEntryTest < ActiveSupport::TestCase
   end
 
   context "#earilest_date_for_project" do
-    should "return the lowest spent_on value that is visible to the current user" do
+    setup do
       User.current = nil
-      assert_equal "2007-03-12", TimeEntry.earilest_date_for_project.to_s
+      @public_project = Project.generate!(:is_public => true)
+      @issue = Issue.generate_for_project!(@public_project)
+      TimeEntry.generate!(:spent_on => '2010-01-01',
+                          :issue => @issue,
+                          :project => @public_project)
     end
+    
+    context "without a project" do
+      should "return the lowest spent_on value that is visible to the current user" do
+        assert_equal "2007-03-12", TimeEntry.earilest_date_for_project.to_s
+      end
+    end
+
+    context "with a project" do
+      should "return the lowest spent_on value that is visible to the current user for that project and it's subprojects only" do
+        assert_equal "2010-01-01", TimeEntry.earilest_date_for_project(@public_project).to_s
+      end
+    end
+      
   end
 
   context "#latest_date_for_project" do
-    should "return the highest spent_on value that is visible to the current user" do
+    setup do
       User.current = nil
-      assert_equal "2007-04-22", TimeEntry.latest_date_for_project.to_s
+      @public_project = Project.generate!(:is_public => true)
+      @issue = Issue.generate_for_project!(@public_project)
+      TimeEntry.generate!(:spent_on => '2010-01-01',
+                          :issue => @issue,
+                          :project => @public_project)
     end
-  end
-  
+
+    context "without a project" do
+      should "return the highest spent_on value that is visible to the current user" do
+        assert_equal "2010-01-01", TimeEntry.latest_date_for_project.to_s
+      end
+    end
+
+    context "with a project" do
+      should "return the highest spent_on value that is visible to the current user for that project and it's subprojects only" do
+        project = Project.find(1)
+        assert_equal "2007-04-22", TimeEntry.latest_date_for_project(project).to_s
+      end
+    end
+  end  
 end