class FlyingSphinx::Railtie < Rails::Railtie
  rake_tasks do
    require 'flying_sphinx/commands'

    ThinkingSphinx.rake_interface = FlyingSphinx::RakeInterface

    load File.expand_path('../tasks.rb', __FILE__)
  end if ENV['FLYING_SPHINX_IDENTIFIER'] || ENV['STAGED_SPHINX_IDENTIFIER']

  initializer "flying_sphinx.set_sphinx_host_and_port" do |app|
    configuration = FlyingSphinx::Configuration.new

    ThinkingSphinx::Configuration.instance.settings['connection_options'] = {
      :host     => configuration.host,
      :port     => 9306,
      :username => configuration.username
    }
  end if ENV['FLYING_SPHINX_IDENTIFIER'] || ENV['STAGED_SPHINX_IDENTIFIER']
end

class FlyingSphinx::Railtie < Rails::Railtie
  rake_tasks do
    next unless FlyingSphinx::Railtie.remote_sphinx?

    require 'flying_sphinx/commands'

    ThinkingSphinx.rake_interface = FlyingSphinx::RakeInterface

    load File.expand_path('../tasks.rb', __FILE__)
  end

  initializer "flying_sphinx.set_sphinx_host_and_port" do |app|
    FlyingSphinx::Railtie.set_up_remote_connection
  end

  config.to_prepare do
    FlyingSphinx::Railtie.set_up_remote_connection
  end

  def self.remote_sphinx?
    ENV['FLYING_SPHINX_IDENTIFIER'] || ENV['STAGED_SPHINX_IDENTIFIER']
  end

  def self.set_up_remote_connection
    return unless remote_sphinx?

    configuration = FlyingSphinx::Configuration.new

    ThinkingSphinx::Configuration.instance.settings['connection_options'] = {
      :host     => configuration.host,
      :port     => 9306,
      :username => configuration.username
    }
  end
end

Fix configuration insertion with Thinking Sphinx v4.2.
Fix configuration insertion with Thinking Sphinx v4.2.

The new config preparation callback in Thinking Sphinx's Railtie was overwriting the saved settings from Flying Sphinx, and thus the non-localhost connection and specific port were being forgotten. So, now we've got the same callback defined here in Flying Sphinx. The order will be consistent of TS before FS, because FS loads TS as a dependency (thus, the order in an app's Gemfile is irrelevant).
