Skip to content

Instantly share code, notes, and snippets.

View warrenlalata's full-sized avatar
🏠
Working from home

Warren Lalata warrenlalata

🏠
Working from home
View GitHub Profile
@ThomasBush
ThomasBush / youtube-video-commands
Last active February 11, 2024 03:21
Ubuntu 20.04 Focal Fossa Rails server setup
# Generate Random Passwords
curl 'https://www.random.org/passwords/?num=2&len=24&format=plain&rnd=new'
# Create deploy user
sudo adduser deploy
sudo adduser deploy sudo
su deploy
cd ../deploy/
# Generate ssh keys 
@Deanout
Deanout / Production Deployment Commands Ubuntu 18.04
Created March 31, 2020 14:37
Deploy to production with nginx, passenger, capistrano, rails 6
These commands are meant to be followed in conjunction with:
https://www.youtube.com/watch?v=xpYpaRUFzTI
https://gorails.com/deploy/ubuntu/18.04
ssh root@1.2.3.4
adduser deploy
adduser deploy sudo
exit
ssh-copy-id root@1.2.3.4
ssh-copy-id deploy@1.2.3.4
@abohannon
abohannon / PrivateRoute.js
Created December 22, 2017 19:23
React/Redux Auth with Private Route Component
import React from 'react';
import { Route, Redirect } from 'react-router-dom';
const PrivateRoute = ({ component: Component, authed, ...rest }) => (
<Route
{...rest}
render={props => (
authed
? <Component {...props} />
: <Redirect to="/login" />
@zcaceres
zcaceres / Include-in-Sequelize.md
Last active January 8, 2024 07:14
using Include in sequelize

'Include' in Sequelize: The One Confusing Query That You Should Memorize

When querying your database in Sequelize, you'll often want data associated with a particular model which isn't in the model's table directly. This data is usually typically associated through join tables (e.g. a 'hasMany' or 'belongsToMany' association), or a foreign key (e.g. a 'hasOne' or 'belongsTo' association).

When you query, you'll receive just the rows you've looked for. With eager loading, you'll also get any associated data. For some reason, I can never remember the proper way to do eager loading when writing my Sequelize queries. I've seen others struggle with the same thing.

Eager loading is confusing because the 'include' that is uses has unfamiliar fields is set in an array rather than just an object.

So let's go through the one query that's worth memorizing to handle your eager loading.

The Basic Query

@regecoder
regecoder / gist:472ea92dbc86dc8ebe169993fa186f72
Last active February 20, 2024 08:38
CSS: Disable elastic scrolling in Chrome
html {
height: 100%;
overflow: hidden;
}
body {
height: 100%;
overflow: auto;
}
@yajra
yajra / axios-401-response-interceptor.js
Last active September 20, 2023 06:24
Axios 401 response interceptor.
// Add a 401 response interceptor
window.axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
if (401 === error.response.status) {
swal({
title: "Session Expired",
text: "Your session has expired. Would you like to be redirected to the login page?",
type: "warning",
showCancelButton: true,
@tomysmile
tomysmile / mac-setup-redis.md
Last active March 18, 2024 22:12
Brew install Redis on Mac

type below:

brew update
brew install redis

To have launchd start redis now and restart at login:

brew services start redis
@jhirbour
jhirbour / clean my spree
Last active August 12, 2020 02:14
Code to empty a spree database of product/taxons/orders
#### DON'T RUN THIS ON YOUR PRODUCTION DATA!!!! It will delete stuff !!!!!!
###
###
### FWIW this was from Spree circa 2013
# clear product data
Spree::Product.all.map { |p| p.destroy! }
Spree::Variant.with_deleted.map { |v| v.destroy! }
Spree::StockItem.all.map { |si| si.destroy! }

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example