Skip to content

Commit

Permalink
Adapt thinking sphinx callback syntax
Browse files Browse the repository at this point in the history
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:
-> pat/thinking-sphinx#1175
The old way is still supported, but in the changelogs it
is recommended to adapt the new style right away.
  • Loading branch information
krauselukas committed Jul 22, 2020
1 parent c9b9c48 commit ef671de
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
24 changes: 13 additions & 11 deletions src/api/app/models/attrib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions src/api/app/models/package_issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ef671de

Please sign in to comment.