These are similar to the corresponding scopes on node, in that they accept
predicates for failed=true/false and pending=true/false. We do this with a
scope for failed instead of using a finder (find_all_by_failed) so we can chain
it with other scopes for lazy evaluation. These scopes are necessary for CSV
export support.

Paired-With: Ben Hengst

Signed-off-by: Nick Lewis <[email protected]>
---
Local-branch: ticket/next/7007
 app/models/resource_status.rb       |   19 +++++++++++++++
 spec/factories.rb                   |   24 ++++++++++++++++++++
 spec/models/resource_status_spec.rb |   42 +++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/app/models/resource_status.rb b/app/models/resource_status.rb
index 17b63bb..3e62ebf 100644
--- a/app/models/resource_status.rb
+++ b/app/models/resource_status.rb
@@ -34,6 +34,25 @@ class ResourceStatus < ActiveRecord::Base
     }
   }
 
+  named_scope :pending, lambda { |predicate|
+    predicate = predicate ? '' : 'NOT'
+    {
+      :conditions => <<-SQL
+        resource_statuses.id #{predicate} IN (
+          SELECT resource_statuses.id FROM resource_statuses
+            INNER JOIN resource_events ON resource_statuses.id = 
resource_events.resource_status_id
+            WHERE resource_events.status = 'noop'
+        )
+      SQL
+    }
+  }
+
+  named_scope :failed, lambda { |predicate|
+    {
+      :conditions => {:failed => predicate}
+    }
+  }
+
   def name
     "#{resource_type}[#{title}]"
   end
diff --git a/spec/factories.rb b/spec/factories.rb
index dfe91b4..9de5e2f 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -24,6 +24,10 @@ Factory.define :report do |report|
   report.time   { Factory.next(:time) }
 end
 
+Factory.define :successful_report, :parent => :report do |report|
+  report.status 'changed'
+end
+
 Factory.define :failing_report, :parent => :report do |report|
   report.status 'failed'
 end
@@ -35,6 +39,26 @@ end
 Factory.define :resource_status do |status|
 end
 
+Factory.define :failed_resource, :parent => :resource_status do |status|
+  status.failed true
+  status.after_create do |status|
+    status.events.generate!(:status => 'failed')
+  end
+end
+
+Factory.define :successful_resource, :parent => :resource_status do |status|
+  status.failed false
+  status.after_create do |status|
+    status.events.generate!(:status => 'success')
+  end
+end
+
+Factory.define :pending_resource, :parent => :successful_resource do |status|
+  status.after_create do |status|
+    status.events.generate!(:status => 'noop')
+  end
+end
+
 Factory.define :resource_event do |event|
 end
 
diff --git a/spec/models/resource_status_spec.rb 
b/spec/models/resource_status_spec.rb
index 9581f9e..380372e 100644
--- a/spec/models/resource_status_spec.rb
+++ b/spec/models/resource_status_spec.rb
@@ -55,5 +55,47 @@ describe ResourceStatus do
       end
     end
   end
+
+  describe ".pending" do
+    before :each do
+      report = Report.generate!
+      @pending_resource = Factory(:pending_resource, :title => 'pending', 
:report => report)
+      @successful_resource = Factory(:successful_resource, :title => 
'successful', :report => report)
+      @failed_resource = Factory(:failed_resource, :title => 'failed', :report 
=> report)
+    end
+
+    describe "true" do
+      it "should return resource statuses which have no pending events" do
+        ResourceStatus.pending(true).map(&:title).should == ['pending']
+      end
+    end
+
+    describe "false" do
+      it "should return resource statuses which have pending events" do
+        ResourceStatus.pending(false).map(&:title).should == ['successful', 
'failed']
+      end
+    end
+  end
+
+  describe ".failed" do
+    before :each do
+      report = Report.generate!
+      @pending_resource = Factory(:pending_resource, :title => 'pending', 
:report => report)
+      @successful_resource = Factory(:successful_resource, :title => 
'successful', :report => report)
+      @failed_resource = Factory(:failed_resource, :title => 'failed', :report 
=> report)
+    end
+
+    describe "true" do
+      it "should return resource statuses which are failed" do
+        ResourceStatus.failed(true).map(&:title).should =~ ['failed']
+      end
+    end
+
+    describe "false" do
+      it "should return resource statuses which are not failed" do
+        ResourceStatus.failed(false).map(&:title).should =~ ['successful', 
'pending']
+      end
+    end
+  end
 end
 
-- 
1.7.5.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to