Skip to content

Instantly share code, notes, and snippets.

View piclez's full-sized avatar

Peter WD piclez

View GitHub Profile
# Generate the models Estado and Cidade:
# rails generate Estado sigla:string nome:string
# rails generate Cidade estado:references nome: string
# rails db:migrate
# Put this code on your db/seeds.rb of your Ruby On Rails application
# rails db:seed
Estado.destroy_all
{
:AC=>"Acre",
:AL=>"Alagoas",
:AP=>"Amapá",
:AM=>"Amazonas",
:BA=>"Bahia", :CE=>"Ceará",
:DF=>"Distrito Federal",
:ES=>"Espírito Santo",
:GO=>"Goiás",
:MA=>"Maranhão",
@piclez
piclez / listings_controller.rb
Created March 30, 2023 01:36 — forked from dajulia3/listings_controller.rb
Service locator with Rails Initializer for rails service loader that loads the services from a config file.
class ListingsController < ApplicationController
def index
movie_listing_service = ServiceLocator.get_service_instance(:MovieListing)
@available_movies = movie_listing_service.available_movies
render nothing: true #this is just an example don't get hung up on it :)
end
@piclez
piclez / bulk_merge.rb
Created February 16, 2023 02:45 — forked from amichal/bulk_merge.rb
csv export and bulk load any rails scope
# Bulk merge a bunch of records
#
# we expect most of the records to import to represent updates
# so this method tries to be efficent with database queries
# by using a single query to find any existing records matching the key_attributes
# updating those records and finally inserting each remaining record
#
# It DOEST NOT use a transaction or lock. callers responsibilty to
# add that if desired
#
@piclez
piclez / resize_nocrop_noscale
Created January 8, 2023 03:44 — forked from maxivak/resize_nocrop_noscale
Crop and resize an image using MiniMagick in Ruby on Rails. Two approaches.
def resize_nocrop_noscale(image, w,h)
w_original = image[:width].to_f
h_original = image[:height].to_f
if w_original < w && h_original < h
return image
end
# resize
image.resize("#{w}x#{h}")
@piclez
piclez / gomongohql.go
Created May 10, 2016 03:29 — forked from IndianGuru/gomongohql.go
gomongohql.go
package main
import (
"fmt"
"html/template"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"log"
"net/http"
"os"
@piclez
piclez / queue.js
Created November 16, 2010 18:25 — forked from dominictarr/queue.js
var sys = require('sys'),
exec = require('child_process').exec;
// fs = require('fs');
// ecsv = require('ecsv')
function Queue () {
var waiting = [];
this.max = -1;
this.current = 0;
//run checks if we're under the max processors, and runs if there is room.
@piclez
piclez / tmp.rb
Created November 16, 2010 18:25 — forked from vvs/tmp.rb
require 'tempfile'
require 'java' if defined?(JRUBY_VERSION)
require 'test/unit'
require 'fileutils'
class TestTempfilesCleanUp < Test::Unit::TestCase
def setup
@tmpdir = "tmp_#{$$}"
Dir.mkdir @tmpdir rescue nil
#!/usr/bin/env ruby -w
# This should be run through './script/runner'
options = YAML.load($stdin.read)
demo = Demo.find(options[:demo])
runner = Outback::Runner.new
runner.manager = demo.manager