diff --git a/src/api/app/models/attrib.rb b/src/api/app/models/attrib.rb index c68f3d7af358..12b120c1c07f 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,19 @@ 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? + + define_sphinx_callback(:package, [:package]) if package_id_previously_changed? + define_sphinx_callback(:project, [:project]) if project_id_previously_changed? + 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..00960b8f0580 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, :package, behaviours: [:real_time], path: [:package]) + ThinkingSphinx::Callbacks.append(self, :project, behaviours: [:real_time], path: [:package, :project]) def self.sync_relations(package, issues) retries = 10