Introduce
Currently, the increasing support of existing frameworks as well as libraries and plugins can speak to the teeth of the programmers. Instead of writing from the beginning, using available support tools speeds up a lot.
Like other programming languages, Ruby also has a lot of libraries that provide special functions that programmers can use, these libraries are organized according to their own functions and are called Gem .
Note: Rails is also a Ruby Gem.
In this article I will introduce the common, useful and commonly used gems in the process of developing a rails application.
1. Check the code quality
One of the basic features of code is quality and simplicity. It directly affects the speed and performance of an application. To check and detect bulky code and refactor properly, we use the following library:
Rubocop It is a tool to analyze and detect any code that does not follow the convention, understand as simple as a spell checker. Convention reference, see here . In addition, Rubocop
allows you to sửa các rule theo ý mình
in the rubocop.yml
file.
Overcommit is used to manage and configure Git hooks. It is a great tool to ensure good code quality, it will run every time I commit.
Bundle audit Check if the gems used by the project have any security holes.
brakeman Check out the security of Ruby on Rails application. Avoid security issues such as injection …
2. Debug
Pry you probably used binding.pry
however, it is a small function of Gem pry only. I find this function very convenient for debugging.
Better Errors to display errors. It replaces the default Rails error page, displaying more useful information about the error.
Byebug debugging is also very easy, it allows you to pause a running thread, execute each line of code, change the value of the variables, chỉ cần thêm byebug vào đoạn code muốn kiểm tra
, open the page in your browser or app. , then go to the console running the server, will see it is stopping the correct code set byebug, type next
to run the next line, continue
to run to the next byebug (if any), break
to get rid of all byebug.
3. Testing
After the product development process, it is indispensable to test the product quality from the dev, before the participation of Tester, QA, the dev will conduct writing Unit Test. Here are some games that support writing tests.
Rspec is an indispensable gem when writing unit tests, it has an intuitive interface and is easier to write than Rails’s default writing method, in addition it can combine with other gems to test easier. Specifically, there have been a lot of talks about Rspec, people can look to read to understand more.
The Factory Bot is used to create test data for Ruby objects and has a simple syntax definition.
Faker used to generate random data, demo data such as name, address, phone number, … When you want to use it, just call like Faker :: Name.name
Simplecov is a tool to support statistics of percentage coverage code of the Unit Test wrote. With an easy-to-see interface, it will display an overarching result, including the percentage of test completion of each file, highlighting different colors to distinguish the code that has been written test, the code that has not been written test.
Basically, based on these 4 Gem, you have the ability to write Unit tests for your product.
There are also some Gems you can refer to for use when needed
Database Cleaner Cleans the DB test, avoiding failures due to overlapping data
Capybara helps you test web applications by simulating a script of many steps users interact with the application, such as entering the homepage, entering login information and password, pressing the login button. It can run in parallel with Rspec.
Screenshot capybara is used along with Capybara
and Cucumber
, Rspec
or Minitest
save screenshots when errors occur.
Parallel Tests help test is written TestUnit
, Rspec
and Cucumber
run faster when it runs in parallel on multiple CPUs. It divides the test into groups, by number of lines or runtime, and runs each group individually with its own database test. This gem supports Rails and other Ruby frameworks.
Shoulda Matchers is used to write tests more quickly and quickly, it is available for ActiveModel, ActiveRecord, ActionController.
RSpec :: Retry sometimes, when we write tests there will be cases where it sometimes fails, and you want to retry the test case itself. Can configure the number of rerun test cases
4. Deployment
These are the two most popular gems used to support Dev’s teeth in deploying process. After config is complete, the deployment is just 1 note only
Usually I use Capistrano. Below is a guide to deploy rails app to EC2. You can refer to Deploy Rails app on EC2
5. Authentication and Authorization
No matter what application, system users are always concerned about security. To ensure this, use some of the following gems:
Devise Authentication for a few notes. support to the teeth that almost every rails application uses.
Ruby-JWT is a deployment library according to RFC 7519 OAuth JSON Web Token (JWT) standard . As a tool for user authentication, it is considered to be one of the most secure communication systems between the two parties.
CanCanCan and Pundit are 2 gems used for authorization (decentralized) extremely convenient. Easy configuration, simple to use.
Tobe continue …