Skip to content

Instantly share code, notes, and snippets.

@bf4
bf4 / ability.rb
Last active October 14, 2017 15:15
Cancan, rolify, and active admin
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.has_role? :admin # rolify
can :manage, :all
can :access, :ckeditor
# Performed checks for actions:
can [:read, :create, :destroy], Ckeditor::Picture
@bf4
bf4 / test_schema_jsonapi.json
Last active September 8, 2017 15:53
JSON API Schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "JSON:API Schema",
"type": "object",
"properties": {
"link": { "$ref": "file://schemata/link.json#" },
"meta": { "$ref": "file://schemata/meta.json#" }
}
}

A collection of Linux commands and concepts I tend to forget

@bf4
bf4 / gist:e9a7016e4c5dca2d6b8a19d6e10d23c0
Created August 9, 2017 03:20 — forked from lvnilesh/gist:e44e3dd1c97eb0467e7d71594e1e9744
How I like to run my Jupyter notebooks

Step 0. Install Docker

Step 1. For your chosen project, decide where you want to store your notebooks and files in a workingfolder.

Step 2. Open that folder in the terminal cd workingfolder

Step 3. Run this command

docker run -it --rm -v $PWD:/home/jovyan/work -p 8888:8888 jupyter/all-spark-notebook

What

A technique for writing parsers.

Why

  • Easy to understand
  • Generally applicable
  • Full power of the programming language at your disposal
  • Declarative
@bf4
bf4 / services.md
Last active August 2, 2017 13:26
Analytics, Metrics and Error monitoring services (and roll your own via logging, etc)
@bf4
bf4 / setup-pow.bash
Created November 10, 2015 16:41
pow and powprox install/reinstall
#!/usr/bin/env bash
install() {
brew cask list lunchy >/dev/null || brew cask install lunchy
pwd=$(pwd)
reinstall_openssl
reinstall_nginx
cd # In case user uses gemsets, ensure we're installing powder outside of a project
@bf4
bf4 / reminder.py
Created November 25, 2015 08:35 — forked from bmaca/reminder.py
A python program to remind you to take a break every hour on your 8 hour work shift ;)
import time
import webbrowser
total_breaks = 8
break_count = 0
while (break_count < total_breaks):
time.sleep(3600)
webbrowser.open("https://31.media.tumblr.com/288d6e631cd930de65547ef5044fefb8/tumblr_mlksb86paT1qbuvyto1_500.gif")
break_count = break_count + 1
# use inverse_of to 'just work'
class User < ActiveRecord::Base
has_many :contributions, inverse_of: :user
has_many :posts, through: :contributions
end
class Post < ActiveRecord::Base
has_many :contributions, inverse_of: :post
has_many :contributors, through: :contributions,
source: :user
@bf4
bf4 / find_relations_in_batches.rb
Created March 22, 2017 18:00
ActiveRecord Rails Find Relations in Batches
# Finds and yields relations in batches. Adapted from record.find_in_batches since that yields
# arrays and we want to operate on relationships.
# Adapted from:
# https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/relation/batches.rb#L98-L115
def find_in_batches(target, batch_size: default_batch_size, start: nil)
relation = target
if logger && (target.arel.orders.present? || target.arel.taken.present?)
logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size")
end
relation = relation.reorder(target.send(:batch_order)).limit(batch_size)