Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check to see if adapter is loaded before requiring an explicit file #83

Merged
merged 3 commits into from
Aug 23, 2015

Conversation

rusterholz
Copy link

This change simply checks to see if the database-specific adapter is already loaded before it tries to require a file with an explicit path. With this change, projects that use geokit-rails can choose to provide their own adapters in initializers without causing unnecessary UnsupportedAdapter errors.

This simple change is the end of a very long story for my team. (Read on if you dare!) Our Rails app uses a custom database adapter to perform cryptographic functions (the excellent pgcrypto adapter by @flipsasser). For Geokit's purposes, the PGCrypto adapter can be treated identically to the PostgreSQL adapter. We had a lot of trouble getting geokit-rails to handle the situation correctly:

  • Out of the box, line 101 was raising an UnsupportedAdapter error. We knew that the code solution was as simple as class Geokit::Adapters::PGCrypto < Geokit::Adapters::PostgreSQL; end but the question was where to put that code.
  • First we tried placing a file in our own project at /lib/geokit-rails/adapters/pgcrypto.rb. That allowed geokit-rails to work, but only in lazy-loaded environments. In eager-loaded environments, Rails would find that file before geokit-rails did, and it would raise a NameError because it expects a class like Geokit::Adapters::PGCrypto to be found at the path /lib/geokit/adapters/pgcrypto.rb instead.
  • Next we tried placing the code at /lib/geokit/adapters/pgcrypto.rb, where Rails would expect it to be. This allows eager-loading to work, but geokit-rails would still trip and fall over anyway because the require on line 96 was explicitly looking for a file under geokit-rails/.
  • Trying to be clever, we placed a file at /lib/geokit-rails/adapters/pgcrypto.rb that simply loaded the file at /lib/geokit/adapters/pgcrypto.rb, but this suffered from the same problem where Rails would raise a NameError when trying to eager-load the file.

After tearing our hair out for a while, we decided that the best solution was to fork the gem and submit the PR you see here. We think this is a pretty common-sense solution that might benefit others without requiring any other changes.

@@ -93,7 +93,9 @@ module ClassMethods
# A proxy to an instance of a finder adapter, inferred from the connection's adapter.
def adapter
@adapter ||= begin
require File.join('geokit-rails', 'adapters', connection.adapter_name.downcase)
unless Adapters.const_defined?(connection.adapter_name.camelcase)
require File.join('geokit-rails', 'adapters', connection.adapter_name.downcase)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [91/80]
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require File.join('geokit-rails', 'adapters', connection.adapter_name.downcase)
unless Adapters.const_defined?(connection.adapter_name.camelcase)
filename = connection.adapter_name.downcase
require File.join('geokit-rails', 'adapters', filename)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@mnoack
Copy link
Member

mnoack commented Aug 23, 2015

Sorry for not merging this earlier. Excellent fix, although I hope it doesn't result in people not submitting their geocoders via PR's

mnoack pushed a commit that referenced this pull request Aug 23, 2015
Check to see if adapter is loaded before requiring an explicit file
@mnoack mnoack merged commit 5fe24e6 into geokit:master Aug 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants