Skip to content

Instantly share code, notes, and snippets.

module Api::ControllerHelpers
extend ActiveSupport::Concern
included do
include InstanceMethods
before_action :prepare_restful_params
end
module InstanceMethods
def prepare_restful_params
@Altech
Altech / patch.rb
Last active November 5, 2017 11:50
A patch to ActiveModel::Serializer
module ActiveModel
class Serializer
# @api private
def relationship_value_for(association, adapter_options, adapter_instance)
return association.options[:virtual_value] if association.options[:virtual_value]
association_serializer = association.serializer
association_object = association_serializer && association_serializer.object
return unless association_object
# [Note] @Altech
@Altech
Altech / preloader.rb
Created June 21, 2017 07:49
Preloader for RESTful API using ActiveModelSerializers
module Api
## Used in ActionController
class Preloader
# @param [Array] attributes fields to select.
# @param [Hash] associations associations to include and fields to select of them.
# @example When you select id and name from companies,
# preload_for(companies, [:id, :name], {})
# @example When you includes posts and employees and employees avatar,
# preload_for(companies, [], {posts: {}, employees: {avatar: {}}})
def self.preload_for(rel, attributes, associations)
@Altech
Altech / kvs.erl
Created October 25, 2013 05:55
simple kvs server in erlang.
-module(kvs).
-export([start/0, store/2, lookup/1]).
start() -> register(kvs, spawn(fun() -> loop() end)).
store(Key, Value) -> rpc({store, Key, Value}).
lookup(Key) -> rpc({lookup, Key}).
rpc(Q) ->
@Altech
Altech / modify_json.rb
Created October 18, 2013 03:28
modify json interactively on Ruby data structure.
#!/usr/bin/env ruby
def modify_json(file_path)
require 'json'
require 'colorize'
require 'pry'
raise "The file is not exist!".red if not File.exists? file_path
json = JSON.parse(File.read(file_path))
puts "json.keys: " + json.keys.join(",")
binding.pry
@Altech
Altech / rails_template.rb
Created August 16, 2013 10:18
my rails init script.
# Gems
# ==================================================
# Segment.io as an analytics solution (https://github.com/segmentio/analytics-ruby)
gem "analytics-ruby"
# For encrypted password
gem "bcrypt-ruby"
# Useful SASS mixins (http://bourbon.io/)
gem "bourbon"
@Altech
Altech / squeeze.rb
Created June 16, 2013 07:36
squeeze in Ruby.
class String
def squeeze(c)
s = ""
now = false
0.upto(self.size-1) do |i|
if self[i] == c
if now
next
else
s << self[i]
@Altech
Altech / modification-mode.el
Created May 19, 2013 05:33
simple minor modes to decorate buffer text(bold, underlie).
(define-minor-mode modi-temporary-mode
"presentation mode of modi."
nil
" modi:temporary"
(list
(cons (kbd "C-;") 'modi:underline-region-temporary)
(cons (kbd "C-'") 'modi:bold-region-temporary)
(cons (kbd "C-.") 'modi:remove-decorations-of-region))
(modi:visualize-decorations-of-buffer))
@Altech
Altech / browse-function-document.el
Last active December 16, 2015 19:10
This command enable you to browse local Haskell document of the pointed function. It uses ghc-mod and hoogle-command.
;; **requirements** :: hoogle(cabal), ghc-mod(cabal,elisp)
(defun haskell-open-doc ()
(interactive)
(let ((sym (thing-at-point 'symbol)))
(let* ((list-string (shell-command-to-string (concat "hoogle " sym)))
(list (haskell-open-doc-select-candidate (remove-if 'haskell-open-doc-filter (haskell-open-doc-parse list-string)))))
(if list
(let ((mod (nth 0 list)) (fun (nth 1 list)) (type (nth 2 list)))
(haskell-open-doc-uri-with-uri-fragment (concat (ghc-display-document-without-browse (ghc-resolve-package-name mod) mod nil) "#v:" fun)))))))
@Altech
Altech / random_set_generator.rb
Last active January 12, 2016 04:54
ドミニオンのランダムセットジェネレーター(暗黒時代対応)。
# used packs
queried_packs = ["暗黒時代", "陰謀", "基本", "繁栄"]
target_list = eval(DATA.read)
.select{|k,v| queried_packs.include? k}
.map{|k,v| v.map{|s| [k,s]}}.flatten(1)
set = Hash.new
10.times do