Skip to content

Instantly share code, notes, and snippets.

View Electron-libre's full-sized avatar

Cedric Brancourt Electron-libre

View GitHub Profile

Running long jobs on remote machine with docker-machine

Use the -d flag with run command.

EG:

eval $(docker-machine env feature)
docker-compose -f delivery.docker-compose.yml run -d rails rake db:seed
@Electron-libre
Electron-libre / conf_wrapper.ex
Last active January 23, 2018 10:15
elixir_config_wrapper.ex
defmodule Gifnoc.Config do
@spec get(atom, atom, term | nil) :: term
def get(app, key, default \\ nil) when is_atom(app) and is_atom(key) do
case read_config(Application.get_env(app, key)) do
nil -> default
val -> val
end
end
def read_config({:system, var_name}), do: System.get_env(var_name)
module Analysis.GobanState exposing (GobanState, Intersection(..), Stone(..), blank, addStone)
import Matrix exposing (Matrix, repeat, set)
type alias GobanState = Matrix Intersection
type Intersection = Free | Stone
type Stone = Black | White
blank : Int -> GobanState
@Electron-libre
Electron-libre / puma.rb
Created February 22, 2017 10:51
Puma with auto worker count
pidfile ENV.fetch('PUMA_PID_PATH','/tmp/pid')
# Should not be a solution
worker_timeout Integer(ENV.fetch('PUMA_WORKER_TIMEOUT', 60))
# Default worker ( OS processes ) is set to 4 ( most machines has 4 cores )
# But adjust depending on context ( how much CPU are given to my container ? )
current_cpu_count = begin
Integer(`nproc`) - 1
rescue
4
end
@Electron-libre
Electron-libre / extend_on_demand.rb
Created September 16, 2016 10:15
Extend on demand
# this wont work for immediate values as Fixnum Symbols but there may be workaround
#
module Doublable
def self.extended(base)
case base
when ::Array
base.extend Doublable::Array
when ::String
base.extend Doublable::String
@Electron-libre
Electron-libre / pg_array_hsotre_fix.rb
Created November 7, 2013 07:59
Include this snippet into your model in order to use hstore in array with postgres and rails 4.0 ( fix is pending on the rails side )
module PgArrayHstoreFix
def self.included(base)
base.class_eval do
before_save :serialize_array_hash
end
def serialize_array_hash
self.class.attribute_names.each do |attribute|
column_definition = self.column_for_attribute(attribute)
if column_definition.array && column_definition.type == :hstore
@Electron-libre
Electron-libre / person_policies_spec.rb
Created October 16, 2013 14:28
Test Policies with pundit
require 'spec_helper'
describe PersonPolicy do
subject { PersonPolicy }
let(:person) { create(:person) }
let(:user) { create(:valid_user) }
context 'given user\'s role activities' do
@Electron-libre
Electron-libre / application_controller.rb
Last active December 25, 2015 16:39
This show how to not write right management with Pundit.
class ApplicationController < ActionController::Base
# Includes Authorization mechanism
include Pundit
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
# Globally rescue Authorization Errors in controller.
@Electron-libre
Electron-libre / postgres_array_rails_migration.rb
Created October 16, 2013 13:22
This is an example of Postgres Array use with Rails 4.
class CreateRoles < ActiveRecord::Migration
def change
create_table :roles do |t|
t.string :activities, array: true, length: 30, using: 'gin', default: '{}'
t.timestamps
end
end
end
##
@Electron-libre
Electron-libre / rest_count.rb
Created July 25, 2013 10:41
HEAD request in Rails , used to send collection informations ...
module ApiController
def index
# ...
# How
if request.head?
@count = self.class.resource_model.count(user_context, format_filters(@filters)).content
head :ok, "Content-Range" => "#{self.class.resource_model.name} #{@offset}-#{@limit}/#{@count}"
else