app/models/foreman_tasks/task.rb in foreman-tasks-0.10.4 vs app/models/foreman_tasks/task.rb in foreman-tasks-0.10.6
- old
+ new
@@ -15,14 +15,14 @@
self.primary_key = :id
before_create :generate_id
belongs_to :parent_task, :class_name => 'ForemanTasks::Task'
- has_many :sub_tasks, :class_name => 'ForemanTasks::Task', :foreign_key => :parent_task_id
- has_many :locks
+ has_many :sub_tasks, :class_name => 'ForemanTasks::Task', :foreign_key => :parent_task_id, :dependent => :nullify
+ has_many :locks, :dependent => :destroy
- has_many :task_group_members
+ 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]
@@ -132,29 +132,31 @@
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
+ uniq_suffix = SecureRandom.hex(3)
key_name = connection.quote_column_name(key.sub(/^.*\./, ''))
joins = <<-SQL
- INNER JOIN foreman_tasks_locks AS foreman_tasks_locks_owner
- ON (foreman_tasks_locks_owner.task_id = foreman_tasks_tasks.id AND
- foreman_tasks_locks_owner.resource_type = 'User' AND
- foreman_tasks_locks_owner.name = '#{Lock::OWNER_LOCK_NAME}')
+ INNER JOIN foreman_tasks_locks AS foreman_tasks_locks_owner#{uniq_suffix}
+ ON (foreman_tasks_locks_owner#{uniq_suffix}.task_id = foreman_tasks_tasks.id AND
+ foreman_tasks_locks_owner#{uniq_suffix}.resource_type = 'User' AND
+ foreman_tasks_locks_owner#{uniq_suffix}.name = '#{Lock::OWNER_LOCK_NAME}')
SQL
if key !~ /\.id\Z/
joins << <<-SQL
- INNER JOIN users
- ON (users.id = foreman_tasks_locks_owner.resource_id)
+ INNER JOIN users as users#{uniq_suffix}
+ ON (users#{uniq_suffix}.id = foreman_tasks_locks_owner#{uniq_suffix}.resource_id)
SQL
end
condition = if key.blank?
- sanitize_sql_for_conditions(["users.login #{operator} ? or users.firstname #{operator} ? ", value, value])
+ sanitize_sql_for_conditions(["users#{uniq_suffix}.login #{operator} ? or users#{uniq_suffix}.firstname #{operator} ? ", value, value])
elsif key =~ /\.id\Z/
value = User.current.id if value == 'current_user'
- sanitize_sql_for_conditions(["foreman_tasks_locks_owner.resource_id #{operator} ?", value])
+ sanitize_sql_for_conditions(["foreman_tasks_locks_owner#{uniq_suffix}.resource_id #{operator} ?", value])
else
- sanitize_sql_for_conditions(["users.#{key_name} #{operator} ?", value])
+ sanitize_sql_for_conditions(["users#{uniq_suffix}.#{key_name} #{operator} ?", value])
end
{ :conditions => condition, :joins => joins }
end
def progress