Skip to content

Instantly share code, notes, and snippets.

View emdeeeks's full-sized avatar

Gareth Griffiths emdeeeks

View GitHub Profile
@niinyarko
niinyarko / create_spec.rb
Last active November 16, 2021 19:03
Request Spec Example for Devise Token Auth Gem with Rails 5.1
# 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) {{
@pongo-pygmaeus
pongo-pygmaeus / subclassing_with_modules.rb
Last active May 30, 2022 04:29
An example of subclassing in Ruby using instance singleton class module extension instead of inheritance.
# 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.
@mnishiguchi
mnishiguchi / ruby_poro.md
Last active September 24, 2024 13:05
ruby, rails - Ruby Poros, tableless models, service objects
@Jekis
Jekis / toggle_swap.sh
Last active August 17, 2024 01:06
Empty swap. Clear swap. Move swap to RAM. Ubuntu.
#!/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))
@thibaudgg
thibaudgg / tutorial.md
Last active January 23, 2023 05:38
Rails Authentication from Scratch - Step by Step

Rails Authentication from Scratch - Step by Step

Prerequisites

  • Clone the Cloud9 thibaudgg/rails-weblog workspace. Go to https://c9.io/new/clone and then choose the "MAS-RAD / Ruby on Rails" team, click on the "Clone workspace" tab, then choose the "thibaudgg/rails-weblog" workspace.
  • Seed the database, this will create some posts and comments (see db/seeds.rb):
rails db:seed

Socket

[Document on ruby-doc.org] [ruby_doc]

Introduction

Ruby provides a standard library Socket for networking programming in lower layer, such as TCP and UDP. There are also other libraries for application layers like HTTP, FTP and TELNET, they are not included in this tutorial.

What is Socket

Resourceable module

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.

Usage

@iscott
iscott / simple_authentication_rails_5_bcrypt_and_has_secure_password.md
Last active November 14, 2024 10:54
Cheat Sheet: Simple Authentication in Rails 5 with has_secure_password

Cheat Sheet: Simple Authentication in Rails 6 with has_secure_password

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
@JoshCheek
JoshCheek / minesweeper.rb
Last active November 15, 2021 20:12
Mine Sweeper
# 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
@JoshCheek
JoshCheek / location_distance.rb
Last active October 26, 2021 02:43
Distance between locations, given their names
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