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') }

Instance Method Summary collapse

Instance Method Details

#build_query_from_hosts(ids) ⇒ Object



40
41
42
43
# File 'app/models/targeting.rb', line 40

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

#dynamic?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/targeting.rb', line 32

def dynamic?
  targeting_type == DYNAMIC_TYPE
end

#resolve_hosts!Object

Raises:

  • (::Foreman::Exception)


22
23
24
25
26
27
28
29
30
# File 'app/models/targeting.rb', line 22

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.touch(:resolved_at)
  self.save!
  self.hosts = User.as(user.) { Host.authorized('edit_hosts', Host).search_for(search_query) }
end

#static?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/targeting.rb', line 36

def static?
  targeting_type == STATIC_TYPE
end