Skip to content

Instantly share code, notes, and snippets.

@sadjow
sadjow / en.yml
Created August 16, 2013 19:15 — forked from denyago/en.yml
Very quickly implemented MIME Type validator for CarrierWave. See also: https://gist.github.com/1009861 and https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Validate-attachment-file-size
en:
errors:
messages:
wrong_content_type: "is the wrong content type"
@sadjow
sadjow / remove.sh
Created July 11, 2013 20:01
How to remover a DEFINER from MySQL
cat new_schema.sql | sed -e 's@/\*!500[0-9][0-9] DEFINER=[^\*]*\*/@@' > new_schema2.sql

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article < ActiveRecord::Base
@sadjow
sadjow / functions.js
Created May 10, 2013 00:03
Update a JavaScript object with a dotted path string. You pass the string path and the object you want.,
// company = {}
// Use putObject("owner.name", company,"Sadjow");
// Returns {owener: {name: "Sadjow"}}
function putObject(path, object, value) {
var modelPath = path.split(".");
function fill(object, elements, depth, value) {
var hasNext = ((depth + 1) < elements.length);
if(depth < elements.length && hasNext) {
@sadjow
sadjow / deploy.rb
Created April 11, 2013 21:13
Capistrano configuration file.
require "bundler/capistrano"
#set :rvm_ruby_string, "1.9.3"
set :rvm_type, :system
set :use_sudo, false
default_run_options[:pty] = true
set :shared_children, shared_children + %w{public/uploads}
@sadjow
sadjow / bootstrap_pagination.html
Created February 15, 2013 19:43
Default bootstrap markup for pagination
<div class="pagination">
<ul>
<li class="prev disabled"><span>previous</span></li>
<li class="active"><span>1</span></li>
<li><a rel="next" href="">2</a></li>
<li class="next"><a rel="next" href="">next</a></li>
</ul>
@sadjow
sadjow / _addthis.haml
Created February 7, 2013 20:33
AddThis HAML Buttons
%a.addthis_button_facebook_like{"addthis:url" => product_url(p), "fb:like:layout" => "button_count"}
%a.addthis_button_tweet{'tw:text' => "#{p.name} #{app_title}: #{product_url(p)}"}
@sadjow
sadjow / truncate.haml
Last active December 12, 2015 04:38
Truncate a text with HTML/HAML
= raw truncate(strip_tags(item.description), :length => 90, :omission => '... ')
@sadjow
sadjow / rmagick.sh
Created January 25, 2013 17:50 — forked from zoras/rmagick.sh
sudo apt-get install libdjvulibre-dev libjpeg-dev libtiff-dev libwmf-dev libmagickcore-dev libmagickwand-dev libmagick++-dev rvm
sudo gem install rmagick
ok!
Fetching: rmagick-2.13.1.gem (100%) Building native extensions. This could take a while ... Successfully installed rmagick-2.13.1 1 gem installed
require 'RMagick' #=> true
derby = require 'derby'
{get, view, ready} = derby.createApp module
derby.use(require '../../ui')
FeedParser = require('feedparser')
parser = new FeedParser()
## ROUTES ##