Skip to content

Instantly share code, notes, and snippets.

View emdeeeks's full-sized avatar

Gareth Griffiths emdeeeks

View GitHub Profile
@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 August 14, 2025 00:56
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
@davetron5000
davetron5000 / verbose.rb
Created February 14, 2017 16:40
DRY Madness
# This is DRY run amok. There is no reason to create an abstraction around
# loading all the notes when we have it already: Note.all
# The private methods add no value.
class NotesController < ApplicationController
def index
load_notes
end
def show
load_note
@JoshCheek
JoshCheek / procedural_circle.rb
Created January 31, 2017 21:58
Procedural circle
# Based on https://codepen.io/clawtros/pen/yVONbR
require 'graphics'
SIZE = 0.5
Node = Struct.new :x, :y, :∆x, :∆y, :color, :lifespan, :on_death do
def successor
new∆x = ∆x*0.98 + (rand - 0.5) / 2
new∆y = ∆y*0.98 + (rand - 0.5) / 2
#!/usr/bin/env python
#encoding: utf-8
import os
import time
print(" _ _ ___ ______ \n" +
" (_) | / _ \ | ___ \\\n" +
" _ __ ___ _| |_ _ __ ___ / /_\ \| |_/ /\n" +
"| '_ ` _ \| | __| '_ ` _ \| _ || __/ \n" +
"| | | | | | | |_| | | | | | | | || | \n" +
@dux
dux / decorator_clone.rb
Last active October 20, 2023 13:51
Simple ruby decorators with example
class DecoratorClone
def initialize(model)
@model = model
end
def method_missing(m, *args)
raise NameError, "Decorator method '#{m}' not found" unless @model.respond_to?(m)
@model.send(m, *args)
end
end