Skip to content

Instantly share code, notes, and snippets.

View giacomomacri's full-sized avatar

Giacomo Macrì giacomomacri

View GitHub Profile
@giacomomacri
giacomomacri / foreign_keys_check.rb
Last active March 30, 2018 13:05
A simple rails concern for checking if a record is referenced in some foreign_key (by id)
module ForeignKeysCheck
extend ActiveSupport::Concern
def has_foreign_key_references?
self.class.all_constraints.group_by {|c| c["table_name"] }.each do |table_name, constraints|
check_query = "SELECT 1 from #{table_name} WHERE "
check_query << constraints.map { |d| "#{d['column_name']} = #{self.id}" }.join(' OR ')
result = ActiveRecord::Base.connection.execute(check_query)
result.count
return true if result.count > 0
@giacomomacri
giacomomacri / en.yml
Last active October 25, 2017 14:02
merge I18n keys from multiple files and write in a single file
en:
app:
my_key: ..
# Config for Nginx to act as a front-end for Riak
# The main goal is to proxy all GETs directly to Riak, and disallow anything else (POST, PUT, etc)
# Also, disallow use of the map/reduce query links (i.e. /riak/bucket/key/_,_,_)
# Config is in /etc/nginx/sites-available/default or somewhere like that
# Set up load-balancing to send requests to all nodes in the Riak cluster
# Replace these IPs/ports with the locations of your Riak nodes
upstream riak_hosts {
server 127.0.0.1:8098;
@giacomomacri
giacomomacri / deploy.rb
Created March 28, 2014 13:10
A workaround for skipping Rails asset pipeline compilation in Capistrano 2 and avoid to compile asset on servers.
# Comment load 'deploy/assets' in Capfile
# Usage
# cap deploy -> deploy with asset pipeline compilation
# cap deploy -s skip_asset=true -> deploy without asset pipeline compilation, just keep the old asset
namespace :deploy do
task :default do
update
# migrate # uncomment this when you need to run migrations
assets.precompile
@giacomomacri
giacomomacri / forms_helper.rb
Last active April 11, 2016 13:39
A Simple Custom Form Builder Helper for Twitter Bootstrap 3. The main part of this code is taken from https://coderwall.com/p/x63_qg
# app/helpers/forms_helper.rb
module FormsHelper
# A custom FormBuilder class for Rails forms with Twitter Bootstrap 3
class CustomFormBuilder < ActionView::Helpers::FormBuilder
# ActionView::Helpers::FormHelper and ActionView::Helpers::FormBuilder
# methods each have different args. This hash stores the names of the args
# and the methods that have those args in order to DRY up the method aliases
# defined below.
@giacomomacri
giacomomacri / localeapp_attr.rake
Created October 10, 2012 12:56
Add Models Attribute on LocaleApp
namespace :localeapp do
desc "Usage: rake localeapp:add_models_keys['Model1 Model2 Model3']"
task :add_models_keys, [:models] => :environment do |task, args|
models = args[:models].split(' ')
models.each do |model|
model_attributes = model.constantize.attribute_names - %w(id)
I18n.available_locales.each do |locale|
model_attributes.each do |at|
key = "activerecord.attributes.#{model.downcase}.#{at}"
@giacomomacri
giacomomacri / ascii_convert.rb
Created October 9, 2012 12:51
Convert from/to ASCII-8 bit
my_string = "jackmacrack"
my_string_ascii = ""
my_string.each_char { |c| my_string_ascii += "#{c.ord}".rjust(3, '0') } #padding to 3 char, otherwise it's impossibile to reconvert
puts my_string_ascii # => 106097099107109097099114097099107
my_original_string = ""
my_string_ascii.scan(/.../).each {|c| my_original_string += c.to_i.chr }
@giacomomacri
giacomomacri / gist:3002605
Created June 27, 2012 09:00
Adapt image to a fixed container
Dir.glob("*.png") do |file|
puts "#{File.basename(file)}"
system "convert #{file} -trim -background transparent -resize 190x340 -gravity center -extent 190x340 converted/#{file}"
end
@giacomomacri
giacomomacri / deploy
Created April 24, 2012 16:06
Asset deploy
namespace :deploy do
namespace :assets do
task :precompile, :roles => :web, :except => { :no_release => true } do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
else
logger.info "Skipping asset pre-compilation because there were no asset changes"
end
end
@giacomomacri
giacomomacri / descrizione.markdown
Created November 10, 2011 11:36
Different MongoDB Version with homebrew

Homebrew non supporta l'installazione scegliendo la versione da installare.

Il mio caso è stato : Ho installato mongodb2.0 ma ho bisogno di mongodb1.8

brew create http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.8.4.tgz

Questo crea un file ruby: /user/local/Library/Formula/mongodb-osx-x86_64.rb

Per aprire questo file (homebrew chiama formula) :