Use your own classes in the lib directory with Rails6

How to create and use your own class, This time, I will introduce how to make it under root / lib.

environment

rails 6.0.3

application.rb settings

application.rb


module Myapp
  class Application < Rails::Application
    config.load_defaults 6.0
    config.paths.add 'lib', eager_load: true #Add this
  end
end

Add under lib to autoload_paths. About autoload_paths here

Configuration under lib

lib
 ├── assets
 ├── asks
 └── utils
     └── hoge.rb

I have created utils / hoge.rb. Let's give it a descriptive name.

Contents of hoge.rb

hoge.rb


class Utils::Hoge
end

Or

hoge.rb


module Utils
  class Hoge
     def greeting
         'hello'
     end
  end
end

Let's define a class with the name of the parent directory as the namespace. By doing this, you can hijack the rules of autoload_paths and call it as follows.

application_controller.rb



class ApplicationController < ActionController::Base
   hoge = Utils::Hoge.new
   hoge.greeting
   #=> hello  
end

Finally

While writing this article, I found the following post. (The lack of research was revealed ...)

Fixed autoload_paths to eliminate mystery error

According to Rails Guaid "Before Rails 5, it was common to include lib / and below in autoload_paths, but now it is not recommended to put the code in a location other than the app folder and make it an autoload target." It is said that an error specific to the production environment may occur.

Since the app is included in autoload_paths by default, It seems good to use that.

So why does the lib directory exist? I wonder if people who are good at development use it properly.

That's all for this time, when new questions have arisen. Dedicated every day

Recommended Posts

Use your own classes in the lib directory with Rails6
Call your own class created under lib with Rails
Use JDBC Manager with the settings in jdbc.dicon.
Use your own docker-compose.yml on the command line
Use images in Rails
Call your own method with PreAuthorize in Spring Security
Why can I use the rails command installed with gem? ??
Use multiple databases with Rails 6.0
Use multiple checkboxes in Rails6!
[Docker] Use whenever with Docker + Rails
[Rails / Routing] How to refer to the controller in the directory you created
Redirect to your own domain with rack-rewrite on Heroku x Rails
Make your own sampler with JMeter
Use constructor with arguments in cucumber-picocontainer
[Rails] Use cookies in API mode
Use LocationAwareLogger for your own Logger
Handle your own annotations in Java
Use the Findbugs plugin in Eclipse
[Ruby on Rails] How to log in with only your name and password using the gem devise
Use Timecop in Rails web app to travel time on your browser
[Rails] How to get the user information currently logged in with devise
[Rails] Use devise to get information about the currently logged in user
[Rails] How to apply the CSS used in the main app with Administrate
How to create your own annotation in Java and get the value
Create your own validator with Bean Validation
Understand java interface in your own way
The identity of params [: id] in rails
Prepare the format environment with "Rails" (VScode)
Rails refactoring story learned in the field
[Java] Use cryptography in the standard library
How to use MySQL in Rails tutorial
Check the processing contents with [rails] binding.pry
[Rails] Reset the database in the production environment
How to use credentials.yml.enc introduced in Rails 5.2
Rails6: Extract the image in Action Text
[Rails] How to register multiple records in the intermediate table with many-to-many association
Create your own shortcuts in Xcode to eliminate the hassle of pod install
[Rails] How to operate the helper method used in the main application with Administrate