Organizating your files in rails: Namespaces versus classes suffixes (MyClassService/MyClassJob)

Organizating your files in rails: Namespaces versus classes suffixes (MyClassService/MyClassJob)

In this short post I will review some of the different ways you can organize your classes using a namespaces or without it and just creating a new folder and place there your classes and adding a suffix.

Let’s start by checking the different ways you can organize your files(using rails 3 or above).

In this example we are organizating our classes into the folder called `services`.

Placing your classes in the /lib folder

You can do it by placing all your classes in the `/lib/services` folder if you are a `module` guy I remember I heard something like that so if that is your choice you can do it like so:

Create your folder

$ mkdir /lib/services

and you should add this to your config/application.rb

config.autoload_paths += %W(#{config.root}/lib/services)

So now you can create your classes as follow:
/lib/services/my_super_dupper_validator.rb

class Services::MySuperDupperValidator
  # your content goes here
end

Placing your classes inside the `/app` folder

I have used two different ways to organize your services inside the /app folder

Not using namespaces

In case you don’t want to use a namespaces you just need to create a folder called /services inside your /app folder:

$ mkdir /app/services

Then you can place all your classes like
file app/services/my_validator_service.rb

class MyValidatorService
  # your code goes here
end

In case that your services is not being loaded you can check if your folder is being loaded correctly by running in console:

ActiveSupport::Dependencies.autoload_paths

If you don’t see the app/services folder in the list:

"/Users/heridev/Projects/my-rails-app/app/services",

you need to add it to your load_paths in the config/application.rb file:

config.autoload_paths += %W(#{config.root}/app/services)
Using a namespaces in the /app folder

WARNING NOTE: Seems like “autoload” is not a reload friendly in development mode so when there is a modification in another file you would see the error `constant not declared for your class` so please use the `lib/` folder to place your classes instead if you want to use a namespaces in your class.
Now, if you want to use a namespaces like me well it’s pretty easy:

Let’s start by creating our app/services folder and inside that folder create a new file called services.rb with the following content:

app/services/services.rb

module Services
  autoload :TimeZoneValidator, 'time_zone_validator'
end

If you see I’m going to create and use a class called `TimeZoneValidator` and we need to specify all the services that we want to load because we don’t want to load everything, so my service validator file will look like this:
app/services/time_zone_validator.rb

class Services::TimeZoneValidator
  # your code goes here
end

As I said in the previous paragraph if you see the error Constant Services not loaded or something like that is because your files are not being loaded correctly in that case you can look into the load paths:

ActiveSupport::Dependencies.autoload_paths

And based on that you could specify your folder to be loaded accordingly to your needs.

As always if you know a different way to organize your files you can send your comments via twitter my handler name is @heridev.

Regards.

No Comments

Post A Comment