Skip to content

Commit

Permalink
'other' projects in add activity drop down should be sorted alfabetic…
Browse files Browse the repository at this point in the history
…ally
  • Loading branch information
Ania committed Dec 14, 2010
1 parent 3c46e37 commit 12bcf6d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
9 changes: 2 additions & 7 deletions app/controllers/activities.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class Activities < Application
# TODO: extract everything related to calendar to separated Calendar controller
RECENT_ACTIVITIES_NUM = 3

provides :json

Expand Down Expand Up @@ -203,13 +202,9 @@ def load_owner
end

def load_projects
@recent_projects = current_user.projects.active.sort_by do |p|
last_activity = Activity.first(:project_id => p.id, :user_id => current_user.id, :order => [:date.desc])
last_activity.date
end
@recent_projects = @recent_projects.reverse[0...RECENT_ACTIVITIES_NUM]
@recent_projects = current_user.recent_projects
# .all(:order => ["activities.created_at DESC"], :limit => RECENT_ACTIVITIES_NUM)
@other_projects = Project.active.all(:order => [:name]) - @recent_projects
@other_projects = (Project.active - @recent_projects).sort_by { |p| p.name.downcase }
end

def load_users
Expand Down
1 change: 0 additions & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def self.with_activities_for(user)
all('activities.user_id' => user.id, :unique => true)
end


# instance methods

def calendar_viewable?(user)
Expand Down
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class User
include DataMapper::Resource

RECENT_ACTIVITIES_NUM = 3

property :id, Serial
property :name, String, :required => true
Expand Down Expand Up @@ -68,6 +70,13 @@ def self.with_activities_for_client(client)
active.all('activities.project.client_id' => client.id, :unique => true)
end

def recent_projects
self.projects.active.sort_by do |p|
last_activity = Activity.first(:project_id => p.id, :user_id => id, :order => [:date.desc])
last_activity.date
end.reverse[0...RECENT_ACTIVITIES_NUM]
end

def authenticated?(password)
crypted_password == encrypt(password) && active
end
Expand Down
23 changes: 23 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@
end
end

describe "recent projects" do
before :each do
@user = Employee.generate
end

it "should retrieve RECENT_ACTIVITIES_NUM projects sorted by activity date" do
Project.all.map(&:name)
projects = ['ani project', 'another project', 'project ani', 'not important'].map do |name|
p = Project.generate(:name => name)
p.users << @user
p.save
p
end
Activity.generate(:project => projects[0], :date => Date.today-2, :user => @user)
Activity.generate(:project => projects[1], :date => Date.today-1, :user => @user)
Activity.generate(:project => projects[2], :date => Date.today, :user => @user)
Activity.generate(:project => projects[0], :date => Date.today+1, :user => @user)
recent = @user.recent_projects
recent.size.should == User::RECENT_ACTIVITIES_NUM
recent.map(&:name).should == [projects[0].name, projects[2].name, projects[1].name]
end
end

describe "with_activities_for_client" do
before :each do
@user = Employee.generate
Expand Down

0 comments on commit 12bcf6d

Please sign in to comment.