Helpful Ruby snippets for pry, rspec, and controller creation.
Written by Anthony Ongaro, on 2023-01-23
Put these into your ruby.json file or snippets.code-snippets in VS Code
"Insert Pry Stop": {
"prefix": "pry",
"body": ["binding.pry"],
"description": "Do some debugging with pry"
},
"Debugger": {
"prefix": "dbg",
"body": ["puts \"DBG: $1=#{$1.inspect}\""]
},
"Save And Open Page": {
"prefix": "sop",
"body": ["save_and_open_page"]
},
Note: Plural controller name, all downcase.
"Controllers Setup": {
"prefix": "contr",
"body": [
"class ${1/(.*)/${1:/pascalcase}/}Controller < ApplicationController",
"\tdef index",
"\t\t@${1} = ${1/(.*)/${1:/pascalcase}/}$2.all",
"\tend\r\r",
"\tdef show",
"\t\t@${1}$2 = ${1/(.*)/${1:/pascalcase}/}$2.find(params[:id])",
"\tend\r\r",
"\tdef new",
"\tend\r\r",
"\tdef create",
"\tend\r\r"
"\tdef edit",
"\tend\r\r"
"\tdef update",
"\tend\r\r"
"\tdef destroy",
"\tend",
"end",
]
},
"Model Setup": {
"prefix": "model",
"body": [
"class $1 < ApplicationRecord",
"\t$0",
"end"
]
},
Note: Tab is your friend.
// Testing
"RSpec Setup": {
"prefix": "test",
"body": [
"require 'rails_helper'\r\r",
"RSpec.describe $1, type: :$2 do",
"\tdescribe '$3' do",
"\t\tit '$4' do",
"\t\t\t$5",
"\t\tend",
"\tend",
"end"
]
},
"Let Test Block": {
"prefix": "let",
"body": [
"let!(:$1) { $2.create!($3: $0) }"
]
},
"Expect Have Content": {
"prefix": "content",
"body": ["expect(page).to have_content($1)"]
},
"Within Expect Have Content": {
"prefix": "with",
"body": [
"within(\"#$1\") do",
"\texpect(page).to have_content($2)",
"end"
]
},
}