[RUBY] 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
Implement the Like feature in Ajax with Rails.
Use pagy for pagination in your Rails app.
Use images in Rails
Call your own method with PreAuthorize in Spring Security
Japaneseize using i18n with Rails
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
About the symbol <%%> in Rails erb
[Rails] Use cookies in API mode
Use LocationAwareLogger for your own Logger
Handle your own annotations in Java
Use the Findbugs plugin in Eclipse
How to use classes in Java?
[Ruby on Rails] How to log in with only your name and password using the gem devise
[Rails] How to get the user information currently logged in with devise
[Rails] Use devise to get information about the currently logged in user
How to display the text entered in text_area in Rails with line breaks
[Rails] How to apply the CSS used in the main app with Administrate
Get the value of enum saved in DB by Rails with attribute_before_type_cast
How to create your own annotation in Java and get the value
How to use custom helpers in rails
Create your own validator with Bean Validation
[Rails] How to use rails console with docker
[Rails] How to use the map method
Prepare the format environment with "Rails" (VScode)
[Java] Use cryptography in the standard library
Fill the screen with buttons in TableLayout
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