Commit d741fea2579de15f20c0cd130addcf4342a546de

add a target_id to Task that is responsible for setting the target obj as ready

Commit diff

app/models/repository.rb

 
7777
7878 def create_new_repos_task
7979 Task.create!(:target_class => self.class.name,
80 :command => "create_git_repository", :arguments => gitdir)
80 :command => "create_git_repository",
81 :arguments => gitdir,
82 :target_id => self.id)
8183 end
8284
8385 def create_delete_repos_task
toggle raw diff

app/models/ssh_key.rb

 
3434
3535 def create_new_task
3636 Task.create!(:target_class => self.class.name,
37 :command => "add_to_authorized_keys", :arguments => self.to_key)
37 :command => "add_to_authorized_keys",
38 :arguments => self.to_key,
39 :target_id => self.id)
3840 end
3941
4042 def create_delete_task
toggle raw diff

app/models/task.rb

 
11class Task < ActiveRecord::Base
2 belongs_to :target, :polymorphic => true
32
43 def self.find_all_pending
54 find(:all, :conditions => {:performed => false})
1919 self.performed = true
2020 self.performed_at = Time.now
2121 save!
22 unless target_id.blank?
23 obj = target_class.constantize.find_by_id(target_id)
24 if obj && obj.respond_to?(:ready)
25 obj.ready = true
26 obj.save!
27 end
28 end
2229 end
2330 end
2431
toggle raw diff

db/migrate/017_add_target_id_to_tasks.rb

 
1class AddTargetIdToTasks < ActiveRecord::Migration
2 def self.up
3 add_column :tasks, :target_id, :integer
4 end
5
6 def self.down
7 remove_column :tasks, :target_id
8 end
9end
toggle raw diff

db/schema.rb

 
99#
1010# It's strongly recommended to check this file into your version control system.
1111
12ActiveRecord::Schema.define(:version => 16) do
12ActiveRecord::Schema.define(:version => 17) do
1313
1414 create_table "committerships", :force => true do |t|
1515 t.integer "user_id"
8888 t.datetime "performed_at"
8989 t.datetime "created_at"
9090 t.datetime "updated_at"
91 t.integer "target_id"
9192 end
9293
9394 add_index "tasks", ["performed"], :name => "index_tasks_on_performed"
toggle raw diff

spec/models/repository_spec.rb

 
133133 task = Task.find(:first, :conditions => ["target_class = 'Repository'"], :order => "id desc")
134134 task.command.should == "create_git_repository"
135135 task.arguments.should match(/#{@repository.gitdir}$/)
136 task.target_id.should == @repository.id
136137 end
137138
138139 it "creates a Task on destroy" do
toggle raw diff

spec/models/ssh_key_spec.rb

 
8585 task = Task.find(:first, :conditions => ["target_class = 'SshKey'"], :order => "id desc")
8686 task.command.should == "add_to_authorized_keys"
8787 task.arguments.should == ssh_key.to_key
88 task.target_id.should == ssh_key.id
8889 end
8990
9091 it "creates a Task on destroy" do
toggle raw diff

spec/models/task_spec.rb

 
1818 @task.performed_at.should_not == nil
1919 end
2020
21 it "marks the object as ready if it has a target_id" do
22 target = repositories(:johans)
23 target.ready = false
24 target.save!
25 @task.target_id = target.id
26 @task.target_class.constantize.should_receive(@task.command) \
27 .with(@task.arguments).and_return(true)
28 @task.perform!
29 target.reload.ready?.should == true
30 end
31
2132 it "finds tasks that needs performin'" do
2233 @task.update_attributes(:performed => true)
2334 Task.find_all_pending.should == [tasks(:add_key)]
toggle raw diff