Skip to content

Instantly share code, notes, and snippets.

@runemadsen
runemadsen / description.markdown
Created September 26, 2011 15:23
Reverse polymorphic associations in Rails

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

@mkhatib
mkhatib / geo.js
Created May 24, 2013 02:55
A Javascript utility function to generate number of random Geolocations around a center location and in a defined radius.
/**
* Generates number of random geolocation points given a center and a radius.
* @param {Object} center A JS object with lat and lng attributes.
* @param {number} radius Radius in meters.
* @param {number} count Number of points to generate.
* @return {array} Array of Objects with lat and lng attributes.
*/
function generateRandomPoints(center, radius, count) {
var points = [];
for (var i=0; i<count; i++) {
@Vercoutere
Vercoutere / 1.Summary.md
Last active November 15, 2018 09:34
Laravel 5 middleware class that sets a context in a multi-tenant app + updated version for easier testing with example testcase.
@puckbag
puckbag / guzzle-custom-curlfactory.php
Last active November 1, 2019 23:06
Custom Guzzle CurlFactory
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlFactory;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Handler\CurlMultiHandler;
use GuzzleHttp\Handler\EasyHandle;
@rodrigoddalmeida
rodrigoddalmeida / ecto_soft_delete.exs
Last active August 29, 2017 16:47
Soft Delete in Ecto
model = Repo.get!(Model, id)
Ecto.Changeset.change(model)
|> Ecto.Model.Timestamps.put_timestamp(:deleted_at, Ecto.DateTime, true)
|> Repo.update
@lokhman
lokhman / ubuntu-hardening.md
Last active June 10, 2024 19:59
List of things for hardening Ubuntu

WARNING

May contain out of date information. Check the comments below!

The list of actions listed below was taken mostly from Book Of Zeus with minor modifications and did the job well for Ubuntu version, which was available at that moment (May 2016). This gist was created for internal use and was never meant to be discovered by the web, although Google managed to find and index this page, which was a great surprise for me. Please check the original source for the updated information (links are provided in most of the sections), and read the comments below: they provide more details about the usage experience.

System Updates

http://bookofzeus.com/harden-ubuntu/initial-setup/system-updates/

Keeping the system updated is vital before starting anything on your system. This will prevent people to use known vulnerabilities to enter in your system.

@nerdyworm
nerdyworm / rename.sh
Created July 30, 2016 17:40
rename a phoenix project
#!/bin/bash
set -e
CURRENT_NAME="CurentName"
CURRENT_OTP="current_name"
NEW_NAME="NewName"
NEW_OTP="new_name"
@CarlosEduardo
CarlosEduardo / Doctrine-Multi-Tenancy.md
Last active February 18, 2024 19:33
Multi-Tenancy (tenant) Strategy for Doctrine ORM
@ahmadshah
ahmadshah / README.md
Last active January 6, 2021 15:21
Ecto Soft Delete

Soft Delete Ecto Repo

The goal is to support soft delete functionality in Ecto.Repo. With the suggestion by @imranismail, another repo is created and the remaining functionalities are delegate to the original MyApp.Repo.

The new repo get/2 and all/1 functions will exclude the soft deleted record by default. delete/1 and delete_all/1 will update the delete_at column by default instead of deleting.

Example

MyApp.Repo.get(MyApp.User, 1) //will return nil if record is in soft delete state
@ahmadshah
ahmadshah / randomizer.ex
Created September 1, 2016 10:28
Random string in elixir
defmodule Randomizer do
@moduledoc """
Random string generator module.
"""
@doc """
Generate random string based on the given legth. It is also possible to generate certain type of randomise string using the options below:
* :all - generate alphanumeric random string
* :alpha - generate nom-numeric random string