Backend tooling
In this article we will show you how we keep our code readable and understandable.
Prerequisites
You have finished the Initial setup guide.
Tooling
We want our code to be readable and understandable and, since our codebase is large and complex, it's important to follow some basic rules when writing code. To enforce these rules, we use RUBOCOP with easy_style.
Installation and usage
You need to have easy_style gem in your Gemfile/gemspec, most projects should already have this one. If not, you can put it there.
RuboCop configuration inherits easy_style: default.yml and the repo config files.
To do so for platform and plugins, put this into Gemfile:
Dealing with offenses
Fixable offenses
Fix them or generate .rubocop_todo.yml, see Notes and known issuees at the end of this article. Also check this article.
Unfixable offenses
The code will probably look differently, but you can try to refactor it.
Can you also try to disable the cop within source ode.
Integration with your IDE
Most IDEs have some kind of support for RUBOCOP such as:
- Highlighting offenses
- Suggesting fixes
Install your RubyMine
-
Make sure this is checked:
*Preferences >> Editor >> Inspections >> Ruby | Gems and gem management | RuboCop*(you can type Rubocop to search box in preferences) -
Make sure all gems are installed in your project, even for rys and plugin repositories >> run
bundle install/updateif not
You can use Problems tool to see a list of rubocop offenses in a given file:
View >> Tool Windows >> Problems.
Activate suggestions for autocorrect feature by pressing
Alt + Enter.
Notes and knows issues
The root .rubocop.yml inherits easy_style: default.yml and .rubocop_ignore.yml. It does not inherit .rubocop_todo.yml.
CI uses .rubocop_ci.yml as the RuboCop config file, so local development and CI can report different results. This is intentional: developers are encouraged to fix existing RuboCop offenses, but pipelines should not fail only because edited code is near offenses that already existed.
For generating .rubocop_todo.yml use this:
- rys:
alias cop_gen_rys="echo \"source 'https://rubygems.org'\" > gems.rb && echo \"gem 'easy_style'\" >> gems.rb && bundle update && bundle exec rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 10000 && git restore .rubocop.yml && git restore gems.rb && bundle update"
- rys regenerate:
alias cop_regen_rys="echo \"source 'https://rubygems.org'\" > gems.rb && echo \"gem 'easy_style'\" >> gems.rb && bundle update && bundle exec rubocop --regenerate-todo && git restore .rubocop.yml && git restore gems.rb && bundle update"
- platform and plugins:
alias cop_gen_platform="echo \"source 'https://rubygems.org'\" > Gemfile && echo \"gem 'easy_style'\" >> Gemfile && bundle update && bundle exec rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 10000 && git restore .rubocop.yml && git restore Gemfile && bundle update"
Bundle exec rubocop can have different result on your computer than on CI (this will be hopefully resolved soon), to achieve the same result you can do this:
- In rys repos: replace content of
gems.rbwithsource 'https://rubygems.org'andgem 'easy_style' - In platform: replace content of Gemfile with
source 'https://rubygems.org'andgem 'easy_style'