Reputation: 12224
What a difference a version makes! In my Rails 3.2 application, I was using concerns, and they were set up like so:
--app
--models
category.rb
product.rb
--concerns
--category
third_party_a.rb
third_party_b.rb
--product
third_party_a.rb
third_party_b.rb
--warehouse
third_party_a.rb
third_party_b.rb
I have concerns with the same name, namespaced into different directories. It was how I was taught to do it, and it makes a lot better sense than the Rails 4.2 default of lumping a bunch of Ruby files in a concerns directory. If I have multiple objects that need to interact with multiple third party APIs, then this is the very best way.
But upgrading this application to Rails 4.2, well, it refuses to work. I'm getting lots, and lots, and lots of complaints on the concerns:
Unable to autoload constant Category::ThirdPartyA
it "expected the file to define it" or something. The file looks like this:
module EbayInteraction
extend ActiveSupport::Concern
module ClassMethods
...
No, there's no other namespace in there. But Rails 3.2 didn't require it. And it worked flawlessly.
So I go into the file and change the declaration to:
module Category::ThirdPartyA
I have no idea if that is correct. I am very confused by modules and namespaces, and neither concept makes any sense at all to me.
And then I go into the model itself and include
the concern like this:
include Category::ThirdPartyA
And I get past the error. Then complains about ThirdPartyB, so I do the same. And so on.
When I'm finished, I now get this complaint:
Circular dependency detected while autoloading constant Category::ThirdPartyA
That is a silly and misleading error. That concern has exactly one slim method. No possibility of anything circular.
Now I am stuck. My only alternative is to rename all of my files and lump them into the concerns directory. I really don't want to do that, it's not maintainable or extensible. What is going on here?
Upvotes: 2
Views: 804
Reputation: 141
I've been able to accomplish this by moving my namespaced concerns out of the concerns
directory.
# app/models/model_one.rb
class ModelOne < ActiveRecord::Base
include Helpers
end
# app/models/model_one/helpers.rb
class ModelOne
module Helpers
extend ActiveSupport::Concern
def some_method
# do stuff
end
end
end
Upvotes: 0
Reputation: 12224
I have decided against wasting any productive time with trying to fix my excellently-namespaced concerns in 4.2. The core team appears to have "improved" a working piece of convention once again.
I prepended all of the concern filenames with the name of the model in which each is include
d. And changed the module name accordingly.
I moved all of my concerns from their model subdirectory up a level to be in concerns.
Now everything works again, even though it is disgusting and gross-looking. Concerns are stupid anyway. Just a way to put fat model stuff into a different file. I'm moving this app to use the Trailblazer gem and architecture. That will insulate me from any silliness that's introduced in Rails 5, and is the only way I'll stick with the platform in the future.
Upvotes: 3