I use Bamboo for my CI/CD so some of this you might not need l will state so in comments.
Create the new project:
0 1 2 |
<code>rails new project_name --webpack=react -d=postgresql -T</code> |
-T tells rails to ignore installing a testing framework.
Setup your database by entering info into config/database.yml and create.
0 1 2 3 4 5 6 |
<code>rails db:create rails db:create RAILS_ENV=test # If using encrypted credentials I use VI EDITOR='vi' rails credentials:edit</code> |
And I edit config/database.yml accordingly.
Add Rspec to your Gemfile:
0 1 2 |
<code>gem 'rspec-rails'</code> |
Here are some of my other gem additions for development and testing:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<code>group :development, :test do gem 'annotate' gem 'factory_bot_rails' gem 'faker' gem 'pry-rails' gem 'rubocop' gem 'rubocop-faker' gem 'simplecov' gem 'simplecov-bamboo' # for bamboo gem 'simplecov-summary' gem 'vcr' gem 'webmock' ... end group :development do gem 'activerecord-explainer' gem 'brakeman' gem 'bullet' gem 'bundler-audit' gem 'rubycritic', require: false ... end</code> |
Commands I often run:
rubocop -a // auto correct and check for style errors
brakeman // check for security issues
rubycritic // looks for code smells and complexity
bundler-audit // audit your gems
Install Rspec:
0 1 2 |
<code>rails g rspec:install</code> |
file location: spec/rails_helper.rb
Setup Rspec:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<code>require 'simplecov' require 'simplecov-bamboo' # for bamboo SimpleCov.start 'rails' do add_group 'Workflows', 'app/workflows' add_group 'Workers', 'app/workers' add_group 'Service Objects', 'app/service_objects' end require 'spec_helper' ... require 'rspec/rails' require 'capybara/rspec' require 'sidekiq/testing' require 'vcr' require 'webmock/rspec' ... config.infer_spec_type_from_file_location! config.after(:suite) do SimpleCov.at_exit do SimpleCov::Formatter::SummaryFormatter.new.format(SimpleCov.result) SimpleCov::Formatter::HTMLFormatter.new.format(SimpleCov.result) SimpleCov::Formatter::BambooFormatter.new.format(SimpleCov.result) end end</code> |
Running Test:
0 1 2 |
<code>rails spec</code> |
Notice that coverage folder is created.
Generate First Page:
0 1 2 |
<code>rails g controller Pages index</code> |
Modify Routes:
File location: config/routes.rb
0 1 2 3 4 5 6 |
<code>Rails.application.routes.draw do root 'pages#index' match '*path', to: 'pages#index', via: :all end</code> |
Running webpacker server for live updates.
./bin/webpacker-dev-server
Update views for React:
File location: app/views/pages/index.html.erb
0 1 2 |
<code><div></div></code> |
File location: app/views/layouts/application.html.erb
0 1 2 |
<code><%= javascript_pack_tag 'hello_react' %></code> |
This should have Hello React! displayed and if you change the jsx file it should live update.