Autoload paths and nested services classes crash in Ruby

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Autoload paths and nested services classes crash in Ruby



I've multiple issues to load / require classes under my app/services folder in a Rails 5 project and I'm starting to give up on this issue.


app/services



First of all and to be clear, services/ are simple PORO classes I use throughout my project to abstract most of the business logic from the controllers, models, etc.


services/



The tree looks like this


app/
services/
my_service/
base.rb
funny_name.rb
my_service.rb
models/
funny_name.rb



First, when I tried to use MyService.const_get('FunnyName') it got FunnyName from my models directory. It does not seem to have the same behavior when I do MyService::FunnyName directly though, in most of my tests and changes this was working fine, it's odd.


MyService.const_get('FunnyName')


FunnyName


MyService::FunnyName



I realised Rails config.autoload_paths does not load things recursively ; it would makes sense that the first FunnyName to be catch is the models/funny_name.rb because it's definitely loaded but not the other.


config.autoload_paths


FunnyName


models/funny_name.rb



That's ok, let's find a workaround. I added this to my application.rb :


application.rb


config.autoload_paths += Dir[Rails.root.join('app', 'services', '**/')]



Which will add all the subdirectories of services into config.autoload_paths. Apparently it's not recommended to write things like that since Rails 5 ; but the idea does look right to me.


config.autoload_paths



Now, when I start my application it crashes and output something like this



Unable to autoload constant Base, expected
/.../backend/app/services/my_service/base.rb to define it (LoadError)



Names were changed but it's the matching path from the tree I wrote previously



The thing is, base.rb is defined in the exact file the error leads me, which contains something like


base.rb


class MyService
class Base
end
end



So I try other workaround, lots of them, nothing ever works. So I end up totally removing the autoload_paths and add this directly in the application.rb


autoload_paths


application.rb


Dir[Rails.root.join('app', 'services', '**', '*.rb')].each { |file| require file }



Now the base.rb is correctly loaded, the MyService.const_get('FunnyName') will actually return the correct class and everything works, but it's a disgusting workaround. Also, it has yet not been tested in production but it might create problems depending the environment.


base.rb


MyService.const_get('FunnyName')


production



Requiring the whole tree from the application.rb sounds like a bad idea and I don't think it can be kept this way.


application.rb



What's the cleanest way to add custom services/ directory in Rails ? It contains multiple subdirectories and classes with simple names which are also present in other parts of the app (models, base.rb, etc.)


services/


base.rb



How do you avoid confusing the autoload_paths ? Is there something else I don't know which could do the trick ? Why did base.rb even crash here ?


autoload_paths


base.rb





Please add one of your service definitions (first line should do it) to your question.
– jvillian
Jul 28 at 0:13





what do you mean ? should i write what's a service to me ?
– Laurent
Jul 28 at 0:18







I mean, does app/services/my_class/base.rb start like class MyClass::Base?
– jvillian
Jul 28 at 0:19


app/services/my_class/base.rb


class MyClass::Base





It's written below, sorry I had to edit it at first because i wasn't highlighted as code
– Laurent
Jul 28 at 0:21





Oh, I see. That's incorrect. class MyClass should be module MyClass.
– jvillian
Jul 28 at 0:23


class MyClass


module MyClass




3 Answers
3



When I do this (which is in all of my projects), it looks something like this:


app
|- services
| |- sub_service
| | |- service_base.rb
| | |- useful_service.rb
| |- service_base.rb



I put all common method definitions in app/services/service_base.rb:


app/services/service_base.rb



app/services/service_base.rb


class ServiceBase

attr_accessor *%w(
args
).freeze

class < self

def call(args={})
new(args).call
end

end

def initialize(args)
@args = args
end

end



I put any methods common to the sub_services in app/services/sub_service/service_base.rb:


sub_services


app/services/sub_service/service_base.rb



app/services/sub_service/service_base.rb


class SubService::ServiceBase < ServiceBase

def call

end

private

def a_subservice_method
end

end



And then any unique methods in useful_service:


useful_service



app/services/sub_service/useful_service.rb


class SubService::UsefulService < SubService::ServiceBase

def call
a_subservice_method
a_useful_service_method
end

private

def a_useful_service_method
end

end



Then, I can do something like:


SubService::UsefulService.call(some: :args)





Alright, but the issue isn't about those, it's more about rails autoloading and customised directories ... the biggest error being base.rb apparently can't be defined twice in the same project and a service with the same name of a model - which can occur - has some kind of mismatch ..
– Laurent
Jul 28 at 1:01


base.rb





Welp, I guess the point is that if you do it like I showed (which is pretty much the same as the other answer, IMO), then autoloading and customised directories will work flawlessly. But, yes, you are correct. If you begin doing things in unconventional ways, you will run into problems with autoloading and customised directories.
– jvillian
Jul 28 at 1:07





Well it's not about being unconventional so much, if a service starts to be really complex (i'm saying service but it could be any design pattern) you need to abstract into multiple PORO down the class so it's easily testable, etc. but why would you use "_service" to each one of those internally used subclasses to avoid collision ? Namespaces are supposed to deal with this exact issue. I hope you see what I mean, I don't get why Rails do that, and how to solve it properly .. Still investigating ...
– Laurent
Jul 28 at 1:25







Defining class Base inside of class MyClass in the file 'app/services/my_class/base.rb` is unconventional, I believe. That file structure, conventionally, expects that MyClass is a module, not a class. Which is what both answers recommend. But, hey, what do I know? BTW, in my projects, I have dozens and dozens of POROs (services, decorators, presenters, managers, etc.) that call each other in endless permutations. For things I use over and over, I extract them into gems. All testable. All quite lovely.
– jvillian
Jul 28 at 1:34




class Base


class MyClass


MyClass


module


class





It seems neither of the answers provide you with a satisfactory solution for autoloading and customized directory structures. I'll look forward to better answers. Best of luck.
– jvillian
Jul 28 at 3:01



With your tree,


app/
services/
my_class/
base.rb
funny_name.rb
my_class.rb
models/
funny_name.rb



services/my_class/base.rb should look similar to:


module MyClass
class Base



services/my_class/funny_name.rb should look similar to:


module MyClass
class FunnyName



services/my_class.rb should look similar to:


class MyClass



models/funny_name.rb should look similar to:


class FunnyName



I say "should look similar to" because class/module are interchangable; Rails is merely looking for these constants to be defined in these locations.



You don't need to add anything to your autoload path. Rails automatically picks up everything in app


app



Anecdotal: With your services directory, it's fairly common to treat their naming convention (both name of file and underlying constant) to be "_service.rb" or "ThingService" — just like how controllers look. Models don't get this suffix because they're treated as first-class objects.



GitLab has some great file structure that is very worth a look at. https://gitlab.com/gitlab-org/gitlab-ce





Thanks, i know all this, actually all my services classes have "_service.rb" added at the end, but those services can become very complex so i usually create "my_service/" directory and then inside ... that's where it goes wrong. Due to the numbers of classes it can contain, and the fact those classes are called internally to the service, I keep the names simple, like base.rb but I started to get weird results when the names match some models, or other classes ... Now i'm wondering how to find a good workaround for this
– Laurent
Jul 28 at 1:14


base.rb





The real issue is, if it's namespaced, why is there a problem with name crash ?
– Laurent
Jul 28 at 1:15





Let me see if I can reproduce it real quick, one sec.
– Josh Brody
Jul 28 at 1:16





I wasn't able to reproduce it with a really naive example. If you want to reproduce this in an example Rails app and push it to Github I can give it a look.
– Josh Brody
Jul 28 at 1:19



Working solution



After deeper investigation and attempts, I realised that I had to eager_load the services to avoid getting wrong constants when calling meta functionalities such as const_get('MyClassWithModelName').


eager_load


const_get('MyClassWithModelName')



But here's is the thing : the classic eager_load_paths won't work because for some reason those classes will apparently be loaded before the entire core of Rails is initialized, and simple class names such as Base will actually be mixed up with the core, therefore make everything crash.


eager_load_paths


Base



Some could say "then rename Base into something else" but should I change a class name wrapped into a namespace because Rails tell me to ? I don't think so. Class names should be kept simple, and what I do inside a custom namespace is no concern of Rails.



I had to think it through and write down my own hook of Rails configuration. We load the core and all its functionalities and then service/ recursively.


service/



On a side note, it won't add any weight to the production environment, and it's very convenient for development.



Code to add



Place this in config/environment/development.rb and all other environment you want to eager load without Rails class conflicts (such as test.rb in my case)


config/environment/development.rb


test.rb


# we eager load all services and subdirectories after Rails itself has been initializer
# why not use `eager_load_paths` or `autoload_paths` ? it makes conflict with the Rails core classes
# here we do eager them the same way but afterwards so it never crashes or has conflicts.
# see `initializers/after_eager_load_paths.rb` for more details
config.after_eager_load_paths = Dir[Rails.root.join('app', 'services', '**/')]



Then create a new file initializers/after_eager_load_paths.rb containing this


initializers/after_eager_load_paths.rb


# this is a customized eager load system
# after Rails has been initialized and if the `after_eager_load_paths` contains something
# we will go through the directories recursively and eager load all ruby files
# this is to avoid constant mismatch on startup with `autoload_paths` or `eager_load_paths`
# it also prevent any autoload failure dû to deep recursive folders with subclasses
# which have similar name to top level constants.
Rails.application.configure do
if config.after_eager_load_paths.instance_of? Array
config.after_initialize do
config.after_eager_load_paths.each do |path|
Dir["#{path}/*.rb"].each { |file| require file }
end
end
end
end



Works like a charm. You can also change require by load if you need it.


require


load






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name