Class: Targeting
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Targeting
- Defined in:
- app/models/targeting.rb
Constant Summary collapse
- STATIC_TYPE =
'static_query'.freeze
- DYNAMIC_TYPE =
'dynamic_query'.freeze
- TYPES =
{ STATIC_TYPE => N_('Static Query'), DYNAMIC_TYPE => N_('Dynamic Query') }.freeze
- RESOLVE_PERMISSION =
:view_hosts
Class Method Summary collapse
Instance Method Summary collapse
- #clone ⇒ Object
- #dynamic? ⇒ Boolean
- #resolve_hosts! ⇒ Object
- #resolved? ⇒ Boolean
- #static? ⇒ Boolean
Class Method Details
.build_query_from_hosts(ids) ⇒ Object
57 58 59 60 61 |
# File 'app/models/targeting.rb', line 57 def self.build_query_from_hosts(ids) return '' if ids.empty? hosts = Host.where(:id => ids).distinct.pluck(:name) "name ^ (#{hosts.join(', ')})" end |
Instance Method Details
#clone ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/targeting.rb', line 21 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
49 50 51 |
# File 'app/models/targeting.rb', line 49 def dynamic? targeting_type == DYNAMIC_TYPE end |
#resolve_hosts! ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/targeting.rb', line 34 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.resolved_at = Time.zone.now self.validate! # avoid validation of hosts objects - they will be loaded for no reason. # pluck(:id) returns duplicate results for HostCollections host_ids = User.as(user.login) { Host.(RESOLVE_PERMISSION, Host).search_for(search_query).pluck(:id).uniq } # this can be optimized even more, by introducing bulk insert self.targeting_hosts.build(host_ids.map { |id| { :host_id => id } }) self.save(:validate => false) end |
#resolved? ⇒ Boolean
63 64 65 |
# File 'app/models/targeting.rb', line 63 def resolved? self.resolved_at.present? end |
#static? ⇒ Boolean
53 54 55 |
# File 'app/models/targeting.rb', line 53 def static? targeting_type == STATIC_TYPE end |