Skip to content

Instantly share code, notes, and snippets.

View pablomarti's full-sized avatar
🧙‍♂️
💻

Pablo Martí pablomarti

🧙‍♂️
💻
View GitHub Profile
@pablomarti
pablomarti / engine.rb
Created March 26, 2013 04:21
Use OmniAuth middleware with Facebook inside of an engine. Example: https://github.com/pablomarti/simple_user/blob/master/lib/simple_user/engine.rb
module SimpleUser
require 'rubygems'
require 'omniauth'
require 'omniauth-facebook'
class Engine < ::Rails::Engine
isolate_namespace SimpleUser
middleware.use OmniAuth::Builder do

I use this Capistrano task so I don't have manually do git push before cap deploy. It includes some error checking to make sure I'm on the right branch (master) and haven't got any uncommitted changes.

Simply add the code below to config/deploy.rb, then run cap deploy:push to test, and cap deploy to deploy as usual.

@pablomarti
pablomarti / example_activejob.rb
Created May 3, 2016 01:25 — forked from ChuckJHardy/example_activejob.rb
Example ActiveJob with RSpec Tests
class MyJob < ActiveJob::Base
queue_as :urgent
rescue_from(NoResultsError) do
retry_job wait: 5.minutes, queue: :default
end
def perform(*args)
MyService.call(*args)
end
@pablomarti
pablomarti / gist:80e5dee84a1e4eb229c126297039907f
Created March 28, 2018 15:28
Postgres - Kill connections
SELECT pg_terminate_backend(pid) FROM pg_stat_activity
WHERE pid <> pg_backend_pid() AND datname = current_database();
@pablomarti
pablomarti / rails http status codes
Created May 21, 2018 14:26 — forked from mlanett/rails http status codes
HTTP status code symbols for Rails
HTTP status code symbols for Rails
Thanks to Cody Fauser for this list of HTTP responce codes and their Ruby on Rails symbol mappings.
Status Code Symbol
1xx Informational
100 :continue
101 :switching_protocols
102 :processing

If you're writing web applications with Ruby there comes a time when you might need something a lot simpler, or even faster, than Ruby on Rails or the Sinatra micro-framework. Enter Rack.

Rack describes itself as follows:

Rack provides a minimal interface between webservers supporting Ruby and Ruby frameworks.

Before Rack came along Ruby web frameworks all implemented their own interfaces, which made it incredibly difficult to write web servers for them, or to share code between two different frameworks. Now almost all Ruby web frameworks implement Rack, including Rails and Sinatra, meaning that these applications can now behave in a similar fashion to one another.

At it's core Rack provides a great set of tools to allow you to build the most simple web application or interface you can. Rack applications can be written in a single line of code. But we're getting ahead of ourselves a bit.

@pablomarti
pablomarti / gist:21fc70dd09775ff225d46e3a9fc6dc4e
Created October 16, 2018 18:06
Force bundle to store in local using flags for OSX
ARCHFLAGS="-arch x86_64" bundle install --path vendor/bundle
@pablomarti
pablomarti / model.rb
Created February 13, 2019 02:09
Modifying Rails validator on runtime
unique_email_validator = validators_on(:email).find { |v|
v.is_a?(ActiveRecord::Validations::UniquenessValidator)
}
def unique_email_validator.validate(*users)
return false if users.map(&:chatbox).all?
return false unless app_users?(users.collect(&:email))
super
@pablomarti
pablomarti / support.rb
Created February 20, 2019 05:29
Load supporting files into a Rails model
module Support
extend ActiveSupport::Concern
included do
support_path = "app/support/#{name.underscore}/"
actions_path = File.join('.', "#{support_path}actions/*.rb")
queries_path = File.join('.', "#{support_path}queries/*.rb")
events_path = File.join('.', "#{support_path}events/*.rb")
Dir[actions_path, queries_path, events_path].each do |f|
@pablomarti
pablomarti / Dockerfile
Created April 8, 2020 07:04 — forked from oinak/Dockerfile
Example docker config for rails develompent
FROM ruby:2.4
## In case of postgresql for heroku:
# RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main" >> /etc/apt/sources.list.d/postgeresql.list \
# && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
# && apt-get update \
# && apt-get update \
# && apt-get install -y --no-install-recommends \
# postgresql-client-9.6 pv ack-grep ccze unp htop vim \
# && rm -rf /var/lib/apt/lists/* \