- RubyHack 2019 - The Circle of Lifecycle Events by Nelson Wittwer
- 7 Patterns to Refactor Fat ActiveRecord Models
- We can optionnally use cgriego/active_attr
- ActiveModel::Attributes が最高すぎるんだよな。 - Qiita
# Example Request | |
# My Devise model is named Merchant | |
# Key lines to note are 16, 21, 27, 28, 29 | |
require 'rails_helper' | |
RSpec.describe 'POST /api/v2/customer_groups', type: :request do | |
let(:merchant) { FactoryGirl.create(:merchant) } | |
let(:user) { FactoryGirl.create(:user) } | |
let(:customer) { merchant.customers.create(user_id: user.id)} | |
let(:params) {{ |
# An example of subclassing in Ruby using instance singleton class module extension instead of inheritance. | |
# Can be thought of a more "rubyesque" version of the common OOP "factory" design pattern. | |
# This work is based on Fred Heath's article "Solving Design Anti-Patterns in Ruby: Fix the Factory" | |
# located here: https://www.sitepoint.com/solving-design-anti-patterns-in-ruby-fix-the-factory | |
class BaseClass | |
attr_reader :text | |
# Since our "inherited" instances won't actually be subclasses | |
# (child classes via inheritance) we should have some way of | |
# describing the "type" to which an instance belongs. |
#!/bin/bash | |
function echo_mem_stat () { | |
mem_total="$(free | grep 'Mem:' | awk '{print $2}')" | |
free_mem="$(free | grep 'Mem:' | awk '{print $7}')" | |
mem_percentage=$(($free_mem * 100 / $mem_total)) | |
swap_total="$(free | grep 'Swap:' | awk '{print $2}')" | |
used_swap="$(free | grep 'Swap:' | awk '{print $3}')" | |
swap_percentage=$(($used_swap * 100 / $swap_total)) |
db/seeds.rb
):rails db:seed
Revisiting a Rails project from when I started developing, I wanted to figure out how to really DRY my controllers out—without making them unreadable by doing half/all of the work in a before_action
. (I've used CanCanCan's load_resource
in the past. That and some other gems I looked at are a little too DRY for what I want.)
So I wrote this module to encapsulate the business logic of finding the record and loading the params data within an Accessor
object. It's not meant to cover all cases, but I'm pretty happy with it.
I have it set up with controller tests in a scaffolded app here.
The goal of this cheatsheet is to make it easy to add hand-rolled authentication to any rails app in a series of layers.
First the simplest/core layers, then optional layers depending on which features/functionality you want.
Specs |
|
---|---|
AUTHOR | Ira Herman |
LANGUAGE/STACK | Ruby on Rails Version 4, 5, or 6 |
# vid @ https://twitter.com/josh_cheek/status/835884161047080960 | |
# and @ https://vimeo.com/205773556 | |
require 'graphics' | |
class MineSweeper | |
class Cell | |
def initialize(mine:, clicked:, marked:, x:, y:, count:) | |
@x, @y, @mine, @clicked, @marked, @count = | |
x, y, mine, clicked, marked, count | |
end |
loc1_name = 'Chicago' # => "Chicago" | |
loc2_name = 'Houston' # => "Houston" | |
# Get the lib with `gem install geocoder` | |
# it ultimately calls out to this API: http://maps.googleapis.com/maps/api/geocode/json?address=Chicago&language=en&sensor=false | |
require 'geocoder' # => true | |
include Math # => Object | |
def radians(n) | |
n * PI / 180 # => 0.7309109668442155, -1.5294285014482003, 0.5194174327134308, -1.664517065837707 |