Skip to content

Instantly share code, notes, and snippets.

@abinoam
abinoam / hanami_snippet.rb
Last active July 27, 2018 15:05
A Hanami Snippet to show a callable controller
# Example from: http://hanamirb.org/guides/1.2/getting-started/#implementing-create-action
module Web::Controllers::Books
class Create
include Web::Action
def call(params)
BookRepository.new.create(params[:book])
redirect_to '/books'
@abinoam
abinoam / frontend_api.rb
Created July 9, 2018 16:58 — forked from ErvalhouS/frontend_api.rb
A read-only frontend API abstraction applicable to any application.
# frozen_string_literal: true
module Api
module V1
# Controller to consume read-only data to be used on client's frontend
class FrontEndController < ActionController::API
include Orderable
# GET /:resource
# GET /:resource.json
@abinoam
abinoam / after_commit_error.rb
Created August 29, 2017 22:14
Bug at paranoia 2.3.1 / rails 5.1.x
# frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
@abinoam
abinoam / rbreadline_multibyte_test.rb
Created September 10, 2015 03:43
RbReadline multibyte support test
#coding: utf-8
# This will remove old Readline if it's already defined
require 'rb-readline'
# This will load RbReadline for real
require 'rbreadline'
# This will load Readline that relies on RbReadline
require 'readline'
@abinoam
abinoam / codeship_heroku_with_maintenance_mode.sh
Created March 20, 2015 15:30
Codeship custom script for Heroku deploying with maintenance mode
#!/bin/sh
# This codeship custom script depends on:
# - API_KEY env var from Heroku
# - STAGING_APP_NAME
# - STAGING_APP_URL
set -e
export HEROKU_API_KEY="${API_KEY}"
@abinoam
abinoam / ruby_versions.md
Last active August 29, 2015 14:16
Ruby Versions. Launching and retirement.

Ruby Versions

Retired

  • 1.8.7 - Retired since 2013-06-30

  • 1.9.3 - Retired since 2015-02-23 - No bug fix, nor even security ones will be backported then

Security Maintenance Phase

module FactoryGirl::Syntax::Methods
def find_or_create(name, attributes = {}, &block)
factory = FactoryGirl.factory_by_name(name)
clazz = factory.build_class
factory_attributes = FactoryGirl.attributes_for(name)
attributes = factory_attributes.merge(attributes)
clazz.find_or_create_by(attributes, &block)
end
@abinoam
abinoam / encoding.rb
Last active August 29, 2015 14:02
https://www.ruby-forum.com/topic/4980931 - Gist of an irb session messing around with encodings
file = File.open "acao_e_reacao_utf16_le.txt"
=> #<File:acao_e_reacao_utf16_le.txt>
file.methods.grep /enc/
=> [:external_encoding, :internal_encoding, :set_encoding]
file.external_encoding
=> #<Encoding:UTF-8>
file.internal_encoding
#!/usr/bin/env ruby
#coding: utf-8
def del_first_three_original_1(a)
num_to_del = a.find { |e| a.count(e) >= 3 }
return a if num_to_del.nil?
3.times do
ind = a.index { |e| e == num_to_del }
a.delete_at(ind)
end
#!/usr/bin/env ruby
# If no local variable is found,
# it begins the method lookup that ends calling
# the method_missing chain until the last one raises the error
begin
p undefined_name if true
rescue
puts "Raised an error"