summaryrefslogtreecommitdiffstats
path: root/lib/puppet/rails/inventory_node.rb
blob: 52f8621a40f57768d9c8923c1478c3261d36b0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require 'puppet/rails/inventory_fact'

class Puppet::Rails::InventoryNode < ::ActiveRecord::Base
  has_many :facts, :class_name => "Puppet::Rails::InventoryFact", :foreign_key => :node_id, :dependent => :delete_all

  named_scope :has_fact_with_value, lambda { |name,value|
    {
      :conditions => ["inventory_facts.name = ? AND inventory_facts.value = ?", name, value],
      :joins => :facts
    }
  }

  named_scope :has_fact_without_value, lambda { |name,value|
    {
      :conditions => ["inventory_facts.name = ? AND inventory_facts.value != ?", name, value],
      :joins => :facts
    }
  }

  def facts_to_hash
    facts.inject({}) do |fact_hash,fact|
      fact_hash.merge(fact.name => fact.value)
    end
  end
end