Project

General

Profile

« Previous | Next » 

Revision 5d264a2d

Added by Ohad Levy over 12 years ago

  • ID 5d264a2db100fd1ba7c505697076c4072698b094

refactor - various facts cleanups

View differences:

app/controllers/fact_values_controller.rb
def index
begin
values = FactValue.no_timestamp_facts.search_for(params[:search],:order => params[:order])
flash.clear
rescue => e
error e.to_s
values = FactValue.no_timestamp_facts.search_for ""
......
respond_to do |format|
format.html do
@fact_values = values.paginate :page => params[:page]
@timestamps = FactValue.timestamp_facts.host_id_eq(@fact_values.map(&:host_id).uniq)
@fact_values = values.required_fields.paginate :page => params[:page]
end
format.json do
render :json => FactValue.build_facts_hash(values.all(:include => [:fact_name, :host]))
app/controllers/facts_controller.rb
# we currently only support JSON in this controller
return not_found unless api_request?
render :json => Puppet::Rails::FactName.all(:select => "name", :conditions => ["fact_names.name <> ?",:timestamp])
render :json => FactName.no_timestamp_fact
end
end
app/controllers/users_controller.rb
@user = User.find(params[:id])
if @user.user_facts.count == 0
user_fact = @user.user_facts.build :operator => "==", :andor => "or"
user_fact.fact_name_id = Puppet::Rails::FactName.first.id if Puppet::Rails::FactName.first
true
user_fact.fact_name_id = FactName.first.id if FactName.first
end
end
app/helpers/fact_names_helper.rb
module FactNamesHelper
end
app/helpers/fact_values_helper.rb
module FactValuesHelper
def fact_from record
time_ago_in_words(Time.parse(@timestamps.select{|fv| fv.host_id == record.host_id}.first.value).utc) + " ago"
time_ago_in_words(record.host.last_compile) + " ago"
rescue
"N/A"
end
app/models/fact_value.rb
scoped_search :in => :host, :on => :name, :rename => :host, :default_order => true
named_scope :no_timestamp_facts, :include => [:fact_name],
:conditions => ["fact_names.name <> ?","--- !ruby/sym _timestamp"]
:conditions => ["fact_names.name <> ?",:_timestamp]
named_scope :timestamp_facts, :joins => [:fact_name],
:conditions => ["fact_names.name = ?","--- !ruby/sym _timestamp"]
:conditions => ["fact_names.name = ?",:_timestamp]
named_scope :distinct, { :select => 'DISTINCT "fact_values.value"' }
named_scope :required_fields, { :include => :host }
default_scope :order => 'LOWER(fact_values.value)'
# Todo: find a way to filter which values are logged,
# this generates too much useless data
app/models/user_fact.rb
class UserFact < ActiveRecord::Base
belongs_to :user
belongs_to :fact_name, :class_name => "Puppet::Rails::FactName"
belongs_to :fact_name
validates_inclusion_of :andor, :in => %w{and or}
validates_inclusion_of :operator, :in => %w{= != > >= < <= }
app/views/users/_user_fact.html.erb
<table class="fields">
<tr>
<td>Name</td>
<td> <%= f.collection_select :fact_name_id, Puppet::Rails::FactName.all.sort, :id, :name, :size => 10 %> </td>
<td> <%= f.collection_select :fact_name_id, FactName.all, :id, :name, :size => 10 %> </td>
<td> <%= f.select :operator, %w{= != > >= < <= } %></td>
<td>Value</td>
<td>
config/initializers/puppet.rb
has_many :user_facts
has_many :users, :through => :user_facts
named_scope :no_timestamp_fact, :conditions => ["fact_names.name <> ?",:_timestamp]
named_scope :timestamp_facts, :conditions => ["fact_names.name = ?", :_timestamp]
default_scope :order => 'LOWER(fact_names.name)'
def to_param
name
end
end
FactName = Puppet::Rails::FactName
# workaround for puppet bug http://projects.reductivelabs.com/issues/3949
if Facter.puppetversion == "0.25.5"
begin
test/functional/hosts_controller_test.rb
test 'user with edit host rights and facts are set should succeed in viewing host1' do
setup_user_and_host "Edit"
as_admin do
fn_id = Puppet::Rails::FactName.find_or_create_by_name("architecture").id
fn_id = FactName.find_or_create_by_name("architecture").id
FactValue.create! :host => @host1, :fact_name_id => fn_id, :value => "x86_64"
FactValue.create! :host => @host2, :fact_name_id => fn_id, :value => "i386"
UserFact.create! :user => @one, :fact_name_id => fn_id, :criteria => "x86_64", :operator => "=", :andor => "or"
......
test 'user with edit host rights and facts are set should fail to view host2' do
setup_user_and_host "Edit"
as_admin do
fn_id = Puppet::Rails::FactName.find_or_create_by_name("architecture").id
fn_id = FactName.find_or_create_by_name("architecture").id
FactValue.create! :host => @host1, :fact_name_id => fn_id, :value => "x86_64"
FactValue.create! :host => @host2, :fact_name_id => fn_id, :value => "i386"
UserFact.create! :user => @one, :fact_name_id => fn_id, :criteria => "x86_64", :operator => "=", :andor => "or"
......
test "should get disabled hosts for a user with a fact_filter via json" do
one = users(:one)
one.roles << [roles(:manager)]
fn = Puppet::Rails::FactName.create :name =>"architecture"
fn = FactName.create :name =>"architecture"
ufact = UserFact.create :user => one, :fact_name => fn, :criteria => "="
assert !(ufact.new_record?)
test/unit/fact_value_test.rb
class FactValueTest < ActiveSupport::TestCase
def setup
@host = hosts(:one)
@fact_name = Puppet::Rails::FactName.create(:name => "my_facting_name")
@fact_name = FactName.create(:name => "my_facting_name")
@fact_value = FactValue.create(:value => "some value", :host => @host, :fact_name => @fact_name)
end
test/unit/host_mailer_test.rb
@options[:env] = nil
@options[:factname] = "Kernel"
@options[:factvalue] = "Linux"
fn = Puppet::Rails::FactName.create :name => @options[:factname]
fn = FactName.create :name => @options[:factname]
FactValue.create :value => @options[:factvalue], :fact_name => fn, :host => @host
assert HostMailer.deliver_summary(@options).body.include?(@options[:factname])
end

Also available in: Unified diff