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

Autoloading of adapters fails with "undefined method 'loaded' for Mysql2:Class. #130

Open
danshep opened this issue Feb 6, 2018 · 0 comments

Comments

@danshep
Copy link

danshep commented Feb 6, 2018

     NoMethodError:
       undefined method `loaded' for Mysql2:Class
     # org/jruby/RubyBasicObject.java:1657:in `method_missing'
     # C:/jruby-9.1.15.0/lib/ruby/gems/shared/gems/geokit-rails-2.3.0/lib/geokit-rails/acts_as_mappable.rb:111:in `geokit_finder_adapter'
     # C:/jruby-9.1.15.0/lib/ruby/gems/shared/gems/geokit-rails-2.3.0/lib/geokit-rails/acts_as_mappable.rb:343:in `sphere_distance_sql'
     # C:/jruby-9.1.15.0/lib/ruby/gems/shared/gems/geokit-rails-2.3.0/lib/geokit-rails/acts_as_mappable.rb:213:in `distance_sql'

This is due to faulty logic in geokit_finder_adapter:

      # A proxy to an instance of a finder adapter, inferred from the connection's adapter.
      def geokit_finder_adapter
        @geokit_finder_adapter ||= begin
          unless Adapters.const_defined?(connection.adapter_name.camelcase)
            filename = connection.adapter_name.downcase
            require File.join("geokit-rails", "adapters", filename)
          end
          klass = Adapters.const_get(connection.adapter_name.camelcase)
          if klass.class == Module
            # For some reason Mysql2 adapter was defined in Adapters.constants but was Module instead of a Class
            filename = connection.adapter_name.downcase
            require File.join("geokit-rails", "adapters", filename)
            # Re-init the klass after require
            klass = Adapters.const_get(connection.adapter_name.camelcase)
          end
          klass.load(self) unless klass.loaded || skip_loading
          klass.new(self)
        rescue LoadError
          raise UnsupportedAdapter, "`#{connection.adapter_name.downcase}` is not a supported adapter."
        end
      end

The call to const_defined and const_get should pass the extra 'false' parameter - otherwise they will find the top-level '::Mysql2' constant and return that instead. ie:

irb(main):001:0> module X1; module X2; end; end
irb(main):004:0> module X2; end
irb(main):005:0> module X3; end
irb(main):006:0> X3.const_defined?('X2')
=> true
irb(main):007:0> X3.const_defined?('X2', false)
=> false
irb(main):008:0> X1.const_defined?('X2', false)
=> true
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

No branches or pull requests

1 participant