public
Created

  • Download Gist
tenancy_middleware.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
# Could perhaps live in app/lib, or app/middleware?
class TenancyMiddleware < ThinkingSphinx::Middlewares::Middleware
def call(contexts)
contexts.each do |context|
Inner.new(context).call
end
 
app.call contexts
end
class Inner
def initialize(context)
@context = context
end
def call
tenant = options[:tenant]
return unless tenant
classes = options[:classes] || []
indices = options[:indices] || []
set = ThinkingSphinx::IndexSet.new(classes, indices)
options[:classes] = []
options[:indices] = set.select { |index|
index.name[/_#{tenant.id}_(core|delta)$/]
}
end
private
def options
@context.search.options
end
end
end
 
# Put this line in an initializer:
ThinkingSphinx::Middlewares::DEFAULT.insert(0, TenancyMiddleware)

Hi Pat,

Correction:
instead of

options[:indices] = set.select { |index|
        index.name[/_#{tenant.id}_(core|delta)$/]
}

there should be

options[:indices] = set.select { |index|
        index.name[/_#{tenant.id}_(core|delta)$/]
}.map(&:name)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.