Skip to content

Instantly share code, notes, and snippets.

@pat
Created January 7, 2013 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pat/4471233 to your computer and use it in GitHub Desktop.
Save pat/4471233 to your computer and use it in GitHub Desktop.
# 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)
@ilkon
Copy link

ilkon commented Mar 8, 2014

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment