Jsonapi-resources: Unable to autoload constant Engine in LinkBuilder#build_engine_name

Created on 20 Sep 2015  路  11Comments  路  Source: cerebris/jsonapi-resources

Hi guys,

The line 58 from link_builder.rb gives the following error (Unable to autoload constant Engine) when used with another gem containing an engine.rb file.

 "#{ scopes.first.to_s.camelize }::Engine".safe_constantize

To reproduce this issue:

This issue was first opened on this repo, but I think it should actually be fixed here as this could happen with any rails engine having a file named engine.rb.

In this case, the autoloader expect lib/knock/engine.rb to define an Engine constant, even though by convention it is namespaced as Knock::Engine.

Somehow I feel like this is actually a weird behaviour from the autoloader, but maybe it would still be good to add a rescue here anyway or something like this.

If you think there is actually something wrong in my code, don't hesitate to reopen the issue on my side and letting me know what you think.

Cheers!

Most helpful comment

A workaround for the time being is to create an initializer containing:

module JSONAPI
  class LinkBuilder
    private

    def build_engine_name
      scopes = module_scopes_from_class(primary_resource_klass)

      unless scopes.empty?
        begin
          "#{ scopes.first.to_s.camelize }::Engine".safe_constantize
        rescue LoadError => e
        end
      end
    end
  end
end

All 11 comments

why is this issue closed? what was the resolution?

@christophermlne This issue is still open. It was the issue in the nsarno/knock project that is closed.

my bad.

Has this been fixed? Is there a workaround of any kind?

I'm not aware of anything yet. I'll try and see if I can submit a PR here in the coming weeks.

A workaround for the time being is to create an initializer containing:

module JSONAPI
  class LinkBuilder
    private

    def build_engine_name
      scopes = module_scopes_from_class(primary_resource_klass)

      unless scopes.empty?
        begin
          "#{ scopes.first.to_s.camelize }::Engine".safe_constantize
        rescue LoadError => e
        end
      end
    end
  end
end

I ran into the same issue with:

rails 5.0.0.1
jsonapi-resources 0.8.1
knock 2.0

The fix by @alessandro1997 above worked.

It would be nice if someone maintaining this project would attempt to address this issue. I don't like the idea of a monkey patch long term. 1 1/2 years is a long time for this not to be at least acknowledged by the maintainers.

@hoverlover I'll see what I can do, but it's a relatively low priority for me since I don't use engines and I'm not sure how to demonstrate the issue off the top of my head. Any help such as a failing test in a PR would be appreciated.

This issue is with knock and not jsonapi-resources so it should be closed here. It can be reproduced without jsonapi-resources.

Just confirmed what @tindron said the issue is on the knock side. 馃憤

Was this page helpful?
0 / 5 - 0 ratings