Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am ivanyv on github.
  • I am ivanyv (https://keybase.io/ivanyv) on keybase.
  • I have a public key whose fingerprint is 66D2 81C2 EAFF 19BA 9B22 4404 D0BB 1424 7607 0208

To claim this, I am signing this object:

Verifying that +ivan is my openname (Bitcoin username). https://onename.io/ivan
@ivanyv
ivanyv / Rakefile
Created June 13, 2011 16:39
Painfully specify Rails migration versions for rake db:migrate
# Put this inside Rakefile
if ENV['VERSION'].present? && ENV['VERSION'].to_i.to_s != ENV['VERSION']
pattern = '*' + ENV['VERSION'].split(//).map { |l| "#{l}*" }.join
migrations = Dir[File.join(Rails.root, 'db', 'migrate', pattern)]
if migrations.size == 0
puts "No migrations matching `#{ENV['VERSION']}` found."
exit
else
migration = File.basename(migrations.first)
if migrations.size > 1
@ivanyv
ivanyv / application.rb
Created June 29, 2011 21:12
Exclusively cache Rails GET requests on nginx
config.action_controller.page_cache_directory = Rails.root.join('public', 'cache-GET')
@ivanyv
ivanyv / chunky_pixels.rb
Created July 1, 2011 22:42
Make chunky pixels the brute force way
require 'rubygems'
require 'chunky_png'
orig = ChunkyPNG::Image.from_file('input.png')
rows = (orig.width / 10.0).ceil
cols = (orig.height / 10.0).ceil
canvas = ChunkyPNG::Canvas.new(rows * 10, cols * 10).replace(orig)
(0...rows).each do |row|
x = row * 10
# app/models/my_model.rb
module MyApp
module Model
def self.included(base)
base.send :include, Mongoid::Document
base.send :include, Mongoid::Timestamps
base.send :include, ActiveAdmin::Mongoid::Patches
end
end
@ivanyv
ivanyv / serialize.rb
Created March 17, 2012 04:40
Easy peasy serializer separation for DDD
# app/controllers/users_controller.rb
class UsersController < ApplicationController
respond_to :json, :xml
def show
@user = @user.find(params[:id])
respond_with Serializer(@user)
end
end
@ivanyv
ivanyv / globals.rb
Last active December 30, 2015 00:59
Crazy global variables
require 'singleton'
class G
include Singleton
attr_accessor :variables
def self.[](var)
variables[var]
end
@ivanyv
ivanyv / application_form.rb
Created July 21, 2016 23:42
Easy form/service objects and decorators
# app/models/application_form.rb
module ApplicationForm
extend ActiveSupport::Concern
class_methods do
def model_name
# Project::Form.model_name => Project.model_name
name.deconstantize.constantize.model_name
end
end
@ivanyv
ivanyv / ARCH.md
Last active November 14, 2016 21:43
Exploring a monolith Rails architecture based on "domains" (which I call "modules")

Architecture

Modules

The app is organized into "modules" (not Ruby modules), like "Core", "Admin", "Directory", etc.

Each module contains models, controllers, and every other related class.

When I say "modules", I'm actually talking about domains, so why call them modules? I'm actually torn on this as I like how "modules" fits much better. But I also know