Rails
Ruby is my favourite programming language for developing web apps. It's incredibly flexible and easy to learn. The gem ecosystem is rich and enables many capabilities with very minimal effort.
Common Conventions
Favourite Gems
Devise
Simple Form
SeedBank
Functionality
SearchKick
SearchKick is used to leverage ElasticSearch.
Common conventions
Reindex the entire site from the command line
rails searchkick:reindex:all
Testing
Rspec
Cheatsheets:
Cucumber
Factory Bot
FactoryBot is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.
Capybara
Orderly
Orderly is a great utility to help test when you need to test the order of content on a page
Faker
Faker is a great way to create fake data for testing purposes
Creating unique results for faker data
Faker::Name.unique.first_name
Development
Figaro
Figaro is a simple environment management tool that works really well with Heroku.
Deployment steps
figaro heroku:set -e [environment name] --remote [remote name]
Rails Generators
Reference: cdesch cheatsheet on GitHub
Scaffolding
rails g scaffold [model name in singular] field_name:field_type
Common field types are
:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean, :references
Special examples
prices:decimal{8.2} can be used for prices
Migrations
Reference: Ruby Docs
Foreign References
rails generate migration AddAddressRefToContacts address:references
Dates
Reference: Ruby Docs
Creating a date
variable_name = Date.parse('December 23, 2019')
Returning values
variable_name.strftime('%B') returns 'December'
See RubyDocs for more examples
Relationships
References
Generating References
You can generate a reference through a rails generator
rails g migration AddPostToUsers post:references
By default, Rails 5 will make this relationship a required field by the model. However, you can override it at the model level
class User < ApplicationRecord
end
class Post < ApplicationRecord
belongs_to :user, required: true
end
Reference: thejspr
You may want to create a reference that has a different name from the model name. To do that, you will need to tweak the migration file
class AddCreatorToOrders < ActiveRecord::Migration[5.2]
def change
add_reference :orders, :creator, foreign_key: { to_table: :users }
end
end
You will then need to modify the models to reflect the relationship
belongs_to :creator, class_name: 'User'
Routing
Shallow Routes
Routing hierarchy quickly gets complicated. One of the strategies suggested by the Rails Guide is to use shallow routing where you split your routes into logical and reasonable structures
Dynamically calling variables and their methods
You can dynamically call methods in Ruby and this is super helpful for testing. Let’s say you have a User class with name and email as it’s methods. You can call it dynamically in your code as the following:
user = User.new
user.email = '[email protected]'
eval("user").send("email") = '[email protected]'
Common Display Conventions
Adding commas to large numbers
<%= number_with_precision(@number, precision: 2, delimiter: ',') %>
Changing cases for string
Downcase
Changes all letters to be lower case
Upcase
Changes all letters to be upper case
Titleize
Changes the first letter of every word to be upper case
Camelize
Changes the first letter of every sentence to be upper case
Tips and Tricks
Running a rails server that is accessible from another computer
rails s --bind 0.0.0.0
This binds your rails server to your network card rather than just the default localhost
Convert Strings to Symbols
Symbols are not variables and are closer to variables. The quick way to convert a string to a symbol is to do the following. Let's say you have a symbol
:some_symbol
To represent this as a symbol through a string, you could use this:
'some_symbol'.to_sym
Active Storage
Checking for file types
@model.content_type will return you the content type.
Checking for image dimensions
Assuming that your content_type is an image, using @model.metadata will return you the width and height of the image in a hash
Decomposing the route that the page came from
@prev = Rails.application.routes.recognize_path(request.referrer)