Class: Targeting

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/targeting.rb

Constant Summary collapse

STATIC_TYPE =
'static_query'
DYNAMIC_TYPE =
'dynamic_query'
TYPES =
{ STATIC_TYPE => N_('Static Query'), DYNAMIC_TYPE => N_('Dynamic Query') }
RESOLVE_PERMISSION =
:view_hosts

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_query_from_hosts(ids) ⇒ Object



54
55
56
57
# File 'app/models/targeting.rb', line 54

def self.build_query_from_hosts(ids)
  hosts = Host.where(:id => ids).all.group_by(&:id)
  hosts.map { |id, h| "name = #{h.first.name}" }.join(' or ')
end

Instance Method Details

#cloneObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/targeting.rb', line 23

def clone
  if static?
    self.dup
  else
    Targeting.new(
      :user => self.user,
      :bookmark_id => self.bookmark.try(:id),
      :targeting_type => self.targeting_type,
      :search_query => self.search_query
    )
  end.tap(&:save)
end

#dynamic?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/targeting.rb', line 46

def dynamic?
  targeting_type == DYNAMIC_TYPE
end

#resolve_hosts!Object

Raises:

  • (::Foreman::Exception)


36
37
38
39
40
41
42
43
44
# File 'app/models/targeting.rb', line 36

def resolve_hosts!
  raise ::Foreman::Exception, _('Cannot resolve hosts without a user') if user.nil?
  raise ::Foreman::Exception, _('Cannot resolve hosts without a bookmark or search query') if bookmark.nil? && search_query.blank?

  self.search_query = bookmark.query if dynamic? && bookmark.present?
  self.save!
  self.touch(:resolved_at)
  self.hosts = User.as(user.) { Host.authorized(RESOLVE_PERMISSION, Host).search_for(search_query) }
end

#resolved?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/models/targeting.rb', line 59

def resolved?
  self.resolved_at.present?
end

#static?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/targeting.rb', line 50

def static?
  targeting_type == STATIC_TYPE
end