Skip to content

Instantly share code, notes, and snippets.

@patsanch
patsanch / cropper.rb
Created May 6, 2011 07:48
Paperclip Cropper
# http://pastie.org/995357
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
puts "CROP: #{crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ')}"
crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ')
else
super
@patsanch
patsanch / rotator.rb
Created May 6, 2011 07:49
Paperclip Rotator
module Paperclip
class Rotator < Thumbnail
def transformation_command
if rotate_command
# having the "super" at the end ensures that your thumbnails are allways "inside the limits of your styles"
# for some reason, super returns: ["-resize", "\"300x300>\""]:Array
"#{rotate_command} #{super.join(' ')}"
else
super
end
<% content_for(:head) do %>
<%= stylesheet_link_tag "jquery.Jcrop" %>
<%= javascript_include_tag "jquery.Jcrop.min" %>
<script type="text/javascript" charset="utf-8">
$(function() {
$('#cropbox').Jcrop({
onChange: update_crop,
onSelect: update_crop,
setSelect: [0, 0, 500, 500],
aspectRatio: 1
@patsanch
patsanch / as3.rake
Created September 4, 2013 22:28 — forked from otobrglez/as3.rake
# 1) Put 's3' gem in Gemfile.
# 2) Create as3.yml configuration for S3
# 3) Create initializer for as3.yml
# 4) Make "assets" folder inside your bucket
# 5) After running task run "RAILS_ENV=production rake assets:precompile"
# 6) Invoke task by running "rake as3:upload"
namespace :as3 do
desc "Uploads compiled assets (public/assets) to Amazone AS3"
task :upload do
@patsanch
patsanch / 0_reuse_code.js
Last active August 29, 2015 14:06
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@patsanch
patsanch / 01_step.sh
Last active August 29, 2015 14:07 — forked from Mikke/01_step.sh
rails g scaffold Book title:string description:text
rails g scaffold Pubhouse title:string address:text
rails g model BookPubhouse book_id:integer pubhouse_id:integer
rake db:migrate
# TODO can be optimized to use custom class and `model_ids` method
module SortPhotos
def sort_photos!(ids)
raise ArgumentError, "ids isn't an array" if !ids.is_a?(Array)
ids.map!(&:to_i)
old_ids = self.photo_ids
ids = ids - (ids - old_ids)
raise ArgumentError, "Not all the ids were passed to sort! method" if old_ids.size != ids.size
reflections[:photos].klass.update_all(['position = FIND_IN_SET(id, ?)', ids.join(',')], {:id => ids})
t = 236 # seconds
Time.at(t).utc.strftime("%H:%M:%S")
=> "00:03:56"
# Reference
# http://stackoverflow.com/questions/3963930/ruby-rails-how-to-convert-seconds-to-time
@patsanch
patsanch / yaml_to_json.rb
Created November 4, 2015 00:44 — forked from xiaohanyu/yaml_to_json.rb
convert yaml to json in ruby
require 'json'
require 'yaml'
input_filename = ARGV[0]
output_filename = input_filename.sub(/(yml|yaml)$/, 'json')
input_file = File.open(input_filename, 'r')
input_yml = input_file.read
input_file.close
@patsanch
patsanch / db_server.rb
Created January 16, 2016 01:05 — forked from exoer/db_server.rb
Create postgres users and databases from Chef
include_recipe "postgresql::server90"
# inspiration from
# https://gist.github.com/637579
execute "create-root-user" do
code = <<-EOH
psql -U postgres -c "select * from pg_user where usename='root'" | grep -c root
EOH
command "createuser -U postgres -s root"