Using the thinking of the Sphinx and staying DRY (do not repeat yourself)

In the large project I'm working on, we have many models that we want to index and search. I repeat things over and over ... and I know that it can be bad later when we make changes!

Is there a good way to save DRY code when using Thinking Sphinx? In particular, I have the following code block in each model that I want to index:

define_index do
  ... # model specific code goes here
  set_property :delta => true
  has :created_at
end

sphinx_scope(:only_active) do
  { :conditions => { :status => 1 } }
end

I expect this common code to grow in size and functionality as the project evolves ... not to mention that bugs can be fixed. Therefore, obviously, I would like to express this. I would like to be able to do something like:

define_index_with_common_settings do
  ... # model specific code goes here
end

... , .

? ?

+3
1

mixin :

module CommonSphinxIndexSettings
  extend ActiveSupport::Concern
  module ClassMethods
    def define_index_with_common_settings
      yield self
      set_property :delta => true
      has :created_at
    end

    sphinx_scope(:only_active) do
      { :conditions => { :status => 1 } }
    end
  end
end

, AR:

ActiveRecord::Base.send :include, CommonSphinxIndexSettings

!

. ( self yield), - ThinkingSphinx. , .

+1

Source: https://habr.com/ru/post/1781541/


All Articles