From 3b116ae8e791f8bd696468fbc25ee57b8b8e4bd3 Mon Sep 17 00:00:00 2001 From: Lukas Krause Date: Wed, 22 Jul 2020 15:07:43 +0200 Subject: [PATCH] Adapt thinking sphinx callback syntax Thinking Sphinx v5.0 introduces explicit callbacks. Since we are using the real time indices, we already have explicit callbacks set. But at the same time, a new syntax for the callback got introduced with: -> /~https://github.com/pat/thinking-sphinx/pull/1175 The old way is still supported, but in the changelogs it is recommended to adapt the new style right away. --- src/api/app/models/attrib.rb | 24 +++++++++++++----------- src/api/app/models/package_issue.rb | 9 ++------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/api/app/models/attrib.rb b/src/api/app/models/attrib.rb index c68f3d7af358..cf1d393f78f0 100644 --- a/src/api/app/models/attrib.rb +++ b/src/api/app/models/attrib.rb @@ -18,17 +18,6 @@ class Attrib < ApplicationRecord accepts_nested_attributes_for :values, allow_destroy: true accepts_nested_attributes_for :issues, allow_destroy: true - #### Callbacks macros: before_save, after_save, etc. - after_save ThinkingSphinx::RealTime.callback_for( - :package, [:package] - ), if: -> { package_id_previously_changed? } - - after_save ThinkingSphinx::RealTime.callback_for( - :project, [:project] - ), if: -> { project_id_previously_changed? } - - #### Scopes (first the default_scope macro if is used) - #### Validations macros validates_associated :values validates_associated :issues @@ -43,6 +32,7 @@ class Attrib < ApplicationRecord :validate_allowed_values_for_attrib_type after_commit :write_container_attributes, on: [:create, :destroy, :update] + after_save :populate_to_sphinx #### Class methods using self. (public and then private) def self.find_by_container_and_fullname(container, fullname) @@ -149,6 +139,18 @@ def validate_allowed_values_for_attrib_type def write_container_attributes container.write_attributes if container && !container.destroyed? end + + def populate_to_sphinx + return unless package_id_previously_changed? || project_id_previously_changed? + + package_id_previously_changed? ? define_sphinx_callback(:package, [:package]) : define_sphinx_callback(:project, [:project]) + end + + def define_sphinx_callback(reference, path = []) + ThinkingSphinx::RealTime::Callbacks::RealTimeCallbacks.new( + reference, path + ).after_save(self) + end end # == Schema Information diff --git a/src/api/app/models/package_issue.rb b/src/api/app/models/package_issue.rb index 022f7532fdac..2baf15ea2681 100644 --- a/src/api/app/models/package_issue.rb +++ b/src/api/app/models/package_issue.rb @@ -7,13 +7,8 @@ class PackageIssue < ApplicationRecord joins('LEFT JOIN package_kinds ON package_kinds.package_id = package_issues.package_id').where('package_kinds.kind = "patchinfo"') } - after_save ThinkingSphinx::RealTime.callback_for( - :package, [:package] - ) - - after_save ThinkingSphinx::RealTime.callback_for( - :project, [:package, :project] - ) + ThinkingSphinx::Callbacks.append(self, reference = :package, :behaviours => [:real_time], :path => [:package]) + ThinkingSphinx::Callbacks.append(self, reference = :project, :behaviours => [:real_time], :path => [:package, :project]) def self.sync_relations(package, issues) retries = 10