Commit d741fea2579de15f20c0cd130addcf4342a546de
- Date: Wed Dec 26 18:15:31 +0000 2007
- Committer: Johan Sørensen (johan@johansorensen.com)
- Author: Johan Sørensen (johan@johansorensen.com)
- Commit SHA1: d741fea2579de15f20c0cd130addcf4342a546de
- Tree SHA1: dd65416791bccbc9658478c85c3c349f0cc1dc94
add a target_id to Task that is responsible for setting the target obj as ready
Commit diff
| |   |
| 77 | 77 | |
| 78 | 78 | def create_new_repos_task |
| 79 | 79 | 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) |
| 81 | 83 | end |
| 82 | 84 | |
| 83 | 85 | def create_delete_repos_task |
| toggle raw diff |
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -77,7 +77,9 @@ class Repository < ActiveRecord::Base
def create_new_repos_task
Task.create!(:target_class => self.class.name,
- :command => "create_git_repository", :arguments => gitdir)
+ :command => "create_git_repository",
+ :arguments => gitdir,
+ :target_id => self.id)
end
def create_delete_repos_task |
| |   |
| 34 | 34 | |
| 35 | 35 | def create_new_task |
| 36 | 36 | 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) |
| 38 | 40 | end |
| 39 | 41 | |
| 40 | 42 | def create_delete_task |
| toggle raw diff |
--- a/app/models/ssh_key.rb
+++ b/app/models/ssh_key.rb
@@ -34,7 +34,9 @@ class SshKey < ActiveRecord::Base
def create_new_task
Task.create!(:target_class => self.class.name,
- :command => "add_to_authorized_keys", :arguments => self.to_key)
+ :command => "add_to_authorized_keys",
+ :arguments => self.to_key,
+ :target_id => self.id)
end
def create_delete_task |
| |   |
| 1 | 1 | class Task < ActiveRecord::Base |
| 2 | | belongs_to :target, :polymorphic => true |
| 3 | 2 | |
| 4 | 3 | def self.find_all_pending |
| 5 | 4 | find(:all, :conditions => {:performed => false}) |
| … | … | |
| 19 | 19 | self.performed = true |
| 20 | 20 | self.performed_at = Time.now |
| 21 | 21 | 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 |
| 22 | 29 | end |
| 23 | 30 | end |
| 24 | 31 | |
| toggle raw diff |
--- a/app/models/task.rb
+++ b/app/models/task.rb
@@ -1,5 +1,4 @@
class Task < ActiveRecord::Base
- belongs_to :target, :polymorphic => true
def self.find_all_pending
find(:all, :conditions => {:performed => false})
@@ -20,6 +19,13 @@ class Task < ActiveRecord::Base
self.performed = true
self.performed_at = Time.now
save!
+ unless target_id.blank?
+ obj = target_class.constantize.find_by_id(target_id)
+ if obj && obj.respond_to?(:ready)
+ obj.ready = true
+ obj.save!
+ end
+ end
end
end
|
| |   |
| 1 | class 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 |
| 9 | end |
| toggle raw diff |
--- /dev/null
+++ b/db/migrate/017_add_target_id_to_tasks.rb
@@ -0,0 +1,9 @@
+class AddTargetIdToTasks < ActiveRecord::Migration
+ def self.up
+ add_column :tasks, :target_id, :integer
+ end
+
+ def self.down
+ remove_column :tasks, :target_id
+ end
+end |
| |   |
| 9 | 9 | # |
| 10 | 10 | # It's strongly recommended to check this file into your version control system. |
| 11 | 11 | |
| 12 | | ActiveRecord::Schema.define(:version => 16) do |
| 12 | ActiveRecord::Schema.define(:version => 17) do |
| 13 | 13 | |
| 14 | 14 | create_table "committerships", :force => true do |t| |
| 15 | 15 | t.integer "user_id" |
| … | … | |
| 88 | 88 | t.datetime "performed_at" |
| 89 | 89 | t.datetime "created_at" |
| 90 | 90 | t.datetime "updated_at" |
| 91 | t.integer "target_id" |
| 91 | 92 | end |
| 92 | 93 | |
| 93 | 94 | add_index "tasks", ["performed"], :name => "index_tasks_on_performed" |
| toggle raw diff |
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 16) do
+ActiveRecord::Schema.define(:version => 17) do
create_table "committerships", :force => true do |t|
t.integer "user_id"
@@ -88,6 +88,7 @@ ActiveRecord::Schema.define(:version => 16) do
t.datetime "performed_at"
t.datetime "created_at"
t.datetime "updated_at"
+ t.integer "target_id"
end
add_index "tasks", ["performed"], :name => "index_tasks_on_performed" |
| |   |
| 133 | 133 | task = Task.find(:first, :conditions => ["target_class = 'Repository'"], :order => "id desc") |
| 134 | 134 | task.command.should == "create_git_repository" |
| 135 | 135 | task.arguments.should match(/#{@repository.gitdir}$/) |
| 136 | task.target_id.should == @repository.id |
| 136 | 137 | end |
| 137 | 138 | |
| 138 | 139 | it "creates a Task on destroy" do |
| toggle raw diff |
--- a/spec/models/repository_spec.rb
+++ b/spec/models/repository_spec.rb
@@ -133,6 +133,7 @@ describe Repository do
task = Task.find(:first, :conditions => ["target_class = 'Repository'"], :order => "id desc")
task.command.should == "create_git_repository"
task.arguments.should match(/#{@repository.gitdir}$/)
+ task.target_id.should == @repository.id
end
it "creates a Task on destroy" do |
| |   |
| 85 | 85 | task = Task.find(:first, :conditions => ["target_class = 'SshKey'"], :order => "id desc") |
| 86 | 86 | task.command.should == "add_to_authorized_keys" |
| 87 | 87 | task.arguments.should == ssh_key.to_key |
| 88 | task.target_id.should == ssh_key.id |
| 88 | 89 | end |
| 89 | 90 | |
| 90 | 91 | it "creates a Task on destroy" do |
| toggle raw diff |
--- a/spec/models/ssh_key_spec.rb
+++ b/spec/models/ssh_key_spec.rb
@@ -85,6 +85,7 @@ describe SshKey do
task = Task.find(:first, :conditions => ["target_class = 'SshKey'"], :order => "id desc")
task.command.should == "add_to_authorized_keys"
task.arguments.should == ssh_key.to_key
+ task.target_id.should == ssh_key.id
end
it "creates a Task on destroy" do |
| |   |
| 18 | 18 | @task.performed_at.should_not == nil |
| 19 | 19 | end |
| 20 | 20 | |
| 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 | |
| 21 | 32 | it "finds tasks that needs performin'" do |
| 22 | 33 | @task.update_attributes(:performed => true) |
| 23 | 34 | Task.find_all_pending.should == [tasks(:add_key)] |
| toggle raw diff |
--- a/spec/models/task_spec.rb
+++ b/spec/models/task_spec.rb
@@ -18,6 +18,17 @@ describe Task do
@task.performed_at.should_not == nil
end
+ it "marks the object as ready if it has a target_id" do
+ target = repositories(:johans)
+ target.ready = false
+ target.save!
+ @task.target_id = target.id
+ @task.target_class.constantize.should_receive(@task.command) \
+ .with(@task.arguments).and_return(true)
+ @task.perform!
+ target.reload.ready?.should == true
+ end
+
it "finds tasks that needs performin'" do
@task.update_attributes(:performed => true)
Task.find_all_pending.should == [tasks(:add_key)] |