app/models/foreman_tasks/task.rb in foreman-tasks-0.13.1 vs app/models/foreman_tasks/task.rb in foreman-tasks-0.13.2

- old
+ new

@@ -24,32 +24,28 @@ has_many :remote_tasks, :class_name => 'ForemanTasks::RemoteTask', :primary_key => :external_id, :foreign_key => :execution_plan_id, :dependent => :destroy has_many :task_group_members, :dependent => :destroy has_many :task_groups, :through => :task_group_members - if Rails::VERSION::MAJOR < 4 - has_many :recurring_logic_task_groups, :through => :task_group_members, :conditions => { :type => 'ForemanTasks::TaskGroups::RecurringLogicTaskGroup' }, :source => :task_group - has_many :owners, :through => :locks, :source => :resource, :source_type => 'User', - :conditions => ['foreman_tasks_locks.name = ?', Lock::OWNER_LOCK_NAME] - else - has_many :recurring_logic_task_groups, -> { where :type => 'ForemanTasks::TaskGroups::RecurringLogicTaskGroup' }, - :through => :task_group_members, :source => :task_group - # in fact, the task has only one owner but Rails don't let you to - # specify has_one relation though has_many relation - has_many :owners, -> { where(['foreman_tasks_locks.name = ?', Lock::OWNER_LOCK_NAME]) }, - :through => :locks, :source => :resource, :source_type => 'User' - end + has_many :recurring_logic_task_groups, -> { where :type => 'ForemanTasks::TaskGroups::RecurringLogicTaskGroup' }, + :through => :task_group_members, :source => :task_group + # in fact, the task has only one owner but Rails don't let you to + # specify has_one relation though has_many relation + has_many :owners, -> { where(['foreman_tasks_locks.name = ?', Lock::OWNER_LOCK_NAME]) }, + :through => :locks, :source => :resource, :source_type => 'User' scoped_search :on => :id, :complete_value => false scoped_search :on => :action, :complete_value => false scoped_search :on => :label, :complete_value => true scoped_search :on => :state, :complete_value => true scoped_search :on => :result, :complete_value => true scoped_search :on => :started_at, :complete_value => false scoped_search :on => :start_at, :complete_value => false scoped_search :on => :ended_at, :complete_value => false scoped_search :on => :parent_task_id, :complete_value => true + scoped_search :relation => :locks, :on => :resource_id, :complete_value => false, :rename => 'location_id', :ext_method => :search_by_taxonomy, :only_explicit => true + scoped_search :relation => :locks, :on => :resource_id, :complete_value => false, :rename => 'organization_id', :ext_method => :search_by_taxonomy, :only_explicit => true scoped_search :relation => :locks, :on => :resource_type, :complete_value => true, :rename => 'resource_type', :ext_method => :search_by_generic_resource scoped_search :relation => :locks, :on => :resource_id, :complete_value => false, :rename => 'resource_id', :ext_method => :search_by_generic_resource scoped_search :relation => :owners, :on => :id, :complete_value => true, @@ -139,10 +135,24 @@ condition = sanitize_sql_for_conditions(["foreman_tasks_locks.#{key_name} #{operator} ?", value]) { :conditions => condition, :joins => :locks } end + def self.search_by_taxonomy(key, operator, value) + uniq_suffix = SecureRandom.hex(3) + resource_type = key == 'location_id' ? 'Location' : 'Organization' + + joins = <<-SQL + LEFT JOIN foreman_tasks_locks AS foreman_tasks_locks_taxonomy#{uniq_suffix} + ON (foreman_tasks_locks_taxonomy#{uniq_suffix}.task_id = foreman_tasks_tasks.id AND + foreman_tasks_locks_taxonomy#{uniq_suffix}.resource_type = '#{resource_type}') + SQL + # Select only those tasks which either have the correct taxonomy or are not related to any + sql = "foreman_tasks_locks_taxonomy#{uniq_suffix}.resource_id #{operator} ? OR foreman_tasks_locks_taxonomy#{uniq_suffix}.resource_id IS NULL" + { :conditions => sanitize_sql_for_conditions([sql, value]), :joins => joins } + end + def self.search_by_owner(key, operator, value) return { :conditions => '0 = 1' } if value == 'current_user' && User.current.nil? key = 'owners.login' if key == 'user' # using uniq suffix to avoid colisions when searching by two different owners via ScopedSearch @@ -226,10 +236,10 @@ part[1][:text] else part.to_s end end.join('; ') - parts.join(' ') + parts.join(' ').strip end protected def generate_id