Permalink
Browse files

Refactor Project#update_notifiers

  • Loading branch information...
1 parent 637f94b commit b8b783d3f72beccb6ea3a16d039e3ceea0e961ba @sr sr committed Mar 29, 2009
Showing with 84 additions and 49 deletions.
  1. +0 −13 lib/integrity/notifier.rb
  2. +16 −2 lib/integrity/project.rb
  3. +0 −34 test/unit/notifier_test.rb
  4. +68 −0 test/unit/project_test.rb
@@ -18,14 +18,6 @@ def self.available
constants.map { |name| const_get(name) }.select { |notifier| valid_notifier?(notifier) }
end
- def self.update_notifiers(project, enabled, config={})
- all(:project_id => project).destroy!
- list_of_enabled_notifiers(enabled).each do |name|
- create! :project_id => project, :name => name, :config => config[name]
- end
-
- end
-
def notify_of_build(build)
to_const.notify_of_build(build, config)
end
@@ -36,11 +28,6 @@ def to_const
self.class.module_eval(name)
end
- def self.list_of_enabled_notifiers(names)
- [*names].reject { |n| n.nil? }
- end
- private_class_method :list_of_enabled_notifiers
-
def self.valid_notifier?(notifier)
notifier.respond_to?(:to_haml) && notifier.respond_to?(:notify_of_build) && notifier != Notifier::Base
end
@@ -94,8 +94,22 @@ def notifies?(notifier)
!notifiers.first(:name => notifier.to_s.split(/::/).last, :project_id => id).blank?
end
- def update_notifiers(*args)
- Notifier.update_notifiers(id, *args)
+ def enabled_notifiers
+ notifiers.all(:enabled => true)
+ end
+
+ def update_notifiers(to_enable, config={})
+ to_disable = enabled_notifiers.select { |notifier| ! to_enable.include?(notifier.name) }
+ to_disable.each { |notifier| notifier.update_attributes(:enabled => false) }
+
+ Array(to_enable).each { |name|
+ notifier = notifiers.first(:name => name, :project_id => id)
+ notifier ||= notifiers.new(:name => name, :config => config, :project_id => id)
+
+ notifier.enabled = true
+ notifier.config = config[name] || {}
+ notifier.save
+ }
end
private
@@ -80,38 +80,4 @@ class NotifierTest < Test::Unit::TestCase
Notifier::IRC.expects(:notify_of_build).with(build, irc.config)
irc.notify_of_build(build)
end
-
- describe "Updating notifiers of a project" do
- it "creates new notifiers for the project" do
- project = Project.generate
- lambda do
- project.update_notifiers(["IRC", "Twitter"],
- {"IRC" => {"uri" => "irc://irc.freenode.net/integrity"},
- "Twitter" => {"username" => "john"}})
- end.should change(project.notifiers, :count).from(0).to(2)
- end
-
- it "deletes all of previous notifiers" do
- project = Project.generate(:notifiers => [Notifier.gen(:irc), Notifier.gen(:twitter)])
- lambda do
- project.update_notifiers("IRC", {"IRC" => {:foo => "bar"}})
- project.reload
- end.should change(project.notifiers, :count).from(2).to(1)
- end
-
- it "does nothing if given nil as the list of notifiers to enable" do
- lambda { Project.gen.update_notifiers(nil, {}) }.should_not change(Notifier, :count)
- end
-
- it "doesn't destroy any of the other notifiers that exist for other projects" do
- irc = Notifier.generate(:irc)
-
- project = Project.gen
- project.update_notifiers("IRC", {"IRC" => irc.config})
-
- lambda do
- Project.gen.update_notifiers("IRC", {"IRC" => irc.config})
- end.should_not change(project.notifiers, :count)
- end
- end
end
@@ -220,6 +220,74 @@ class ProjectTest < Test::Unit::TestCase
end
end
+ context "Dealing with notifiers" do
+ it "knows which notifiers are enabled" do
+ notifiers = [Notifier.gen(:irc, :enabled => false),
+ Notifier.gen(:twitter, :enabled => true)]
+ project = Project.gen(:notifiers => notifiers)
+
+ assert_equal 1, project.enabled_notifiers.size
+ end
+
+ it "creates and enable given notifiers" do
+ project = Project.gen
+ project.update_notifiers(["IRC", "Twitter"],
+ {"IRC" => {"uri" => "irc://irc.freenode.net/integrity"},
+ "Twitter" => {"username" => "john"}})
+
+ assert_equal 2, Notifier.count
+ assert_equal "IRC", project.notifiers.first.name
+ assert_equal "Twitter", project.notifiers.last.name
+ end
+
+ it "disables notifiers that are not included in the list" do
+ project = Project.gen
+ project.update_notifiers(["IRC", "Twitter"],
+ {"IRC" => {"uri" => "irc://irc.freenode.net/integrity"},
+ "Twitter" => {"username" => "john"}})
+
+ lambda {
+ project.update_notifiers(["IRC"],
+ {"IRC" => {"uri" => "irc://irc.freenode.net/integrity"}})
+ }.should change(project.enabled_notifiers, :count).from(2).to(1)
+
+ assert ! project.notifiers.first(:name => "Twitter").enabled?
+ assert project.notifiers.first(:name => "IRC").enabled?
+ end
+
+ it "creates new notifiers for the project" do
+ project = Project.generate
+ lambda do
+ project.update_notifiers(["IRC", "Twitter"],
+ {"IRC" => {"uri" => "irc://irc.freenode.net/integrity"},
+ "Twitter" => {"username" => "john"}})
+ end.should change(project.notifiers, :count).from(0).to(2)
+ end
+
+ it "don't deletes all of previous notifiers" do
+ project = Project.generate(:notifiers => [Notifier.gen(:irc), Notifier.gen(:twitter)])
+ lambda do
+ project.update_notifiers("IRC", {"IRC" => {:foo => "bar"}})
+ project.reload
+ end.should_not change(project.notifiers, :count)
+ end
+
+ it "does nothing if given nil as the list of notifiers to enable" do
+ lambda { Project.gen.update_notifiers(nil, {}) }.should_not change(Notifier, :count)
+ end
+
+ it "doesn't destroy any of the other notifiers that exist for other projects" do
+ irc = Notifier.generate(:irc)
+
+ project = Project.gen
+ project.update_notifiers("IRC", {"IRC" => irc.config})
+
+ lambda do
+ Project.gen.update_notifiers("IRC", {"IRC" => irc.config})
+ end.should_not change(project.notifiers, :count)
+ end
+ end
+
describe "When retrieving state about its notifier" do
before(:each) do
@project = Project.generate

0 comments on commit b8b783d

Please sign in to comment.