Skip to content

Instantly share code, notes, and snippets.

View ahmadshah's full-sized avatar
:octocat:
Yo!

Ahmad Shah Hafizan Hamidin ahmadshah

:octocat:
Yo!
View GitHub Profile
@ahmadshah
ahmadshah / metable.rb
Created July 19, 2016 17:58
Metable Concern for Rails ActiveRecord
module Metable
extend ActiveSupport::Concern
included do
serialize :meta, JSON
end
def is_metable?
has_attribute?(:meta)
end
@ahmadshah
ahmadshah / authorize.rb
Last active July 29, 2016 10:43
Access Control List
module Guard
class Authorize
attr_reader :user, :roles
def initialize(user)
build_user_roles(user)
end
def authorities(role)
raise StandardError, "User is not associated to #{role} role" unless self.is? role
@ahmadshah
ahmadshah / README.md
Last active March 24, 2025 13:29
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
<?php
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Laravel\Lumen\Routing\Controller as BaseController;
class Controller extends BaseController {
public function __construct()
{
@ahmadshah
ahmadshah / node.ex
Created September 21, 2016 07:14
Fetch node from map using dot notation in elixir
defmodule Node do
defmacro fetch_node(map, node) do
quote do
String.split(unquote(node), ".")
|> Enum.reduce(unquote(map), fn(n, m) -> Map.get(m, n) end)
end
end
end
defmodule Reactor.Service.Authenticator do
@behaviour Reactor.Contract.Service.Authenticator
@moduledoc """
User authentication service.
"""
use Reactor.Resolver, repositories: [:user]
alias Comeonin.Bcrypt, as: Comeonin
alias Reactor.Entity.User, as: UserEntity
@ahmadshah
ahmadshah / docker-compose.yml
Created October 27, 2016 18:42
elixir docker
version: '2'
services:
db:
image: mariadb
restart: always
volumes:
- ./docker/data/mysql:/var/lib/mysql
ports:
- "3306:3306"
@ahmadshah
ahmadshah / docker-compose.yml
Created September 23, 2017 19:24
Restcomm docker compose
version: "2"
services:
restcomm:
image: restcomm/restcomm:latest
container_name: "restcomm"
environment:
- RCBCONF_STATIC_ADDRESS=127.0.0.1
- ENVCONFURL=https://raw.githubusercontent.com/RestComm/Restcomm-Docker/master/env_files/restcomm_env_locally.sh
ports:
- "8080:8080"
defmodule Fashionista.Event.UserIsAuthenticated do
use Fashionista.Service.Kong
import Joken
@response_fields ~w(
username custom_id message secret id algorithm created_at key consumer_id
)
def create_token(email, id) do