Skip to content

Commit

Permalink
Ordering projects on project list
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Bugno committed Nov 26, 2010
1 parent d7092f4 commit afb582c
Show file tree
Hide file tree
Showing 13 changed files with 877 additions and 6 deletions.
13 changes: 11 additions & 2 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ProjectsController < ApplicationController
before_filter :locate_project, :only => [:show, :build, :edit, :update, :remove, :destroy]
before_filter :locate_project, :only => [:show, :build, :edit, :update, :remove, :destroy, :arrange]
def index
@projects = Project.order("created_at DESC")
@projects = Project.order("position ASC")
end

def show
Expand Down Expand Up @@ -42,6 +42,15 @@ def destroy
redirect_to projects_path
end

def arrange
if params[:up]
@project.move_higher
elsif params[:down]
@project.move_lower
end
redirect_to projects_path
end

private
def locate_project
@project = Project.find(params[:id])
Expand Down
3 changes: 3 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
class Project < ActiveRecord::Base
STATUS_NOT_BUILT = "status_not_built"

has_many :builds, :dependent => :destroy
before_destroy :remove_build_folder

acts_as_list

def recent_build
builds.order("created_at DESC").first
end
Expand Down
7 changes: 6 additions & 1 deletion app/views/projects/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
%br
= ("author: " + link_to(project.recent_build.author, "mailto:#{project.recent_build.email}")).html_safe
%br
= "message: " + truncate(project.recent_build.commit_message, :length => 35)
= "message: " + truncate(project.recent_build.commit_message, :length => 55)
.info
- if ! project.first?
= link_to("Up", arrange_project_path(project, :up => true))
- if ! project.last?
= link_to("Down", arrange_project_path(project, :down => true))
2 changes: 1 addition & 1 deletion app/views/projects/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
.info
= ("author: " + link_to(build.author, "mailto:#{build.email}")).html_safe
%br
= "message: " + truncate(build.commit_message, :length => 35)
= "message: " + truncate(build.commit_message, :length => 55)
%br
= "scheduled at: " + build.scheduled_at.strftime("%Y-%m-%d %H:%M")
.info
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BigTuna::Application.routes.draw do
resources :projects do
member { get "build"; get "remove" }
member { get "build"; get "remove"; get "arrange" }
end
resources :builds
match "/hooks/:hook_name", :to => "hooks#post_commit"
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20101126094722_add_position_to_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddPositionToProject < ActiveRecord::Migration
def self.up
add_column(:projects, :position, :integer)
end

def self.down
remove_column(:projects, :position)
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20101125175448) do
ActiveRecord::Schema.define(:version => 20101126094722) do

create_table "builds", :force => true do |t|
t.integer "project_id"
Expand Down Expand Up @@ -52,6 +52,7 @@
t.integer "max_builds"
t.string "hook_name"
t.text "steps"
t.integer "position"
end

end
24 changes: 24 additions & 0 deletions test/integration/projects_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def setup
def teardown
FileUtils.rm_rf("test/files/repo")
FileUtils.rm_rf("builds/valid")
FileUtils.rm_rf("builds/valid2")
FileUtils.rm_rf("builds/invalid")
super
end
Expand Down Expand Up @@ -48,4 +49,27 @@ def teardown
click_button "Yes, I'm sure"
end
end

test "user can reorder projects on project list" do
project1 = Project.make(:steps => "echo 'ha'", :name => "Valid", :vcs_source => "test/files/repo", :vcs_type => "git")
project2 = Project.make(:steps => "echo 'sa'", :name => "Valid2", :vcs_source => "test/files/repo", :vcs_type => "git")
visit "/"
within("#project_#{project2.id}") do
assert page.has_content?("Up")
assert ! page.has_content?("Down")
end
within("#project_#{project1.id}") do
assert page.has_content?("Down")
assert ! page.has_content?("Up")
end
click_link "Down"
within("#project_#{project1.id}") do
assert page.has_content?("Up")
assert ! page.has_content?("Down")
end
within("#project_#{project2.id}") do
assert page.has_content?("Down")
assert ! page.has_content?("Up")
end
end
end
23 changes: 23 additions & 0 deletions vendor/plugins/acts_as_list/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ActsAsList
==========

This acts_as extension provides the capabilities for sorting and reordering a number of objects in a list. The class that has this specified needs to have a +position+ column defined as an integer on the mapped database table.


Example
=======

class TodoList < ActiveRecord::Base
has_many :todo_items, :order => "position"
end

class TodoItem < ActiveRecord::Base
belongs_to :todo_list
acts_as_list :scope => :todo_list
end

todo_list.first.move_to_bottom
todo_list.last.move_higher


Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license
12 changes: 12 additions & 0 deletions vendor/plugins/acts_as_list/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rake'
require 'rake/testtask'

desc 'Default: run acts_as_list unit tests.'
task :default => :test

desc 'Test the acts_as_ordered_tree plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
3 changes: 3 additions & 0 deletions vendor/plugins/acts_as_list/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$:.unshift "#{File.dirname(__FILE__)}/lib"
require 'active_record/acts/list'
ActiveRecord::Base.class_eval { include ActiveRecord::Acts::List }
Loading

0 comments on commit afb582c

Please sign in to comment.