Skip to content

Instantly share code, notes, and snippets.

@dnagir
dnagir / Gemfile.lock
Created February 21, 2012 07:20
Rails issue #5095
GEM
remote: https://rubygems.org/
specs:
actionmailer (3.2.1)
actionpack (= 3.2.1)
mail (~> 2.4.0)
actionpack (3.2.1)
activemodel (= 3.2.1)
activesupport (= 3.2.1)
builder (~> 3.0.0)
@dnagir
dnagir / .vimrc
Created March 14, 2012 23:27
My current .vimrc
" Example Vim configuration.
" Copy or symlink to ~/.vimrc or ~/_vimrc.
set nocompatible " Must come first because it changes other options.
filetype off
call pathogen#helptags()
call pathogen#runtime_append_all_bundles()
"silent! call pathogen#runtime_append_all_bundles()
@dnagir
dnagir / database_cleaner.rb
Created March 16, 2012 03:58
Using transactional DB cleanup
# spec/support/database_cleaner.rb
require 'database_cleaner'
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with :truncation # Delete leftovers if any
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
@dnagir
dnagir / comments_controller.rb
Created May 22, 2012 06:41
Raptor in Rails?
class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.build(params[:comment])
@comment.save # etc
end
end
@dnagir
dnagir / faster_helper.rb
Created June 28, 2012 23:55
Fast specs with Rails
ENV["RAILS_ENV"] ||= 'test'
cur_dir = File.expand_path(File.dirname(__FILE__) + '/..')
$LOAD_PATH << "#{cur_dir}"
if defined? Rails
# Most likely going with the full env
puts "Running faster_helper with full Rails env..."
# Eagerly load the Rails so that stubbed dependencies still work
# http://stackoverflow.com/questions/11133712/using-rails-model-that-is-already-declared
@dnagir
dnagir / examples.rb
Created July 20, 2012 01:40
Why squeel is great
# Example 1, not so cool (look at the 'less-than' and OR condition)
# plain AR:
Property.joins(:reservations).
where(status: 'reserved').
where("reservations.status = ? OR reservations.reservation_type = ?", 'active', 'offline').
where(reservations: {reservation_fee_received: false}).
where("reservations.fee_due_at < ?", DateTime.now)
# with squeel:
@dnagir
dnagir / credit_card.rb
Created November 11, 2012 22:10
CreditCard class
class CreditCard
include ActiveAttr::Model
attribute :number
attribute :expiry
attribute :cvn
attribute :name
validate :must_be_valid
validates :number, :expiry, :cvn, :name, presence: true
@dnagir
dnagir / background_worker_starter.rb
Created November 12, 2012 00:20
Scheduler as worker
class BackgroundWorkerStarter
attr_reader :env
def initialize(env)
@env = env
end
def start!
write_pid!(pid_file) if pid_file
@dnagir
dnagir / readme.md
Created November 23, 2012 02:27
Unobtrusive and testable AJAX in Rails 3

The problem

Initially asked at RORO but I will elaborate with an example.

What we need to do is the following:

  • we list a set of items (let's say posts) and want to show a drop-down menu next to each one (quick example)
  • when the drop-down arrow is clicked the menu is loaded from the server.
@dnagir
dnagir / cloudinary_upload.rb
Created April 24, 2013 01:57
Uploading cloudinary files using DelayedJob without a shared file system. It stores the binary in the database along with DJ and will have some impact on that. So the DJ table needs to be updated to accomodate that (such as making it `UNLOGGED` etc)
module Jobs
class CloudinaryUpload < Struct.new(:uploader_class, :model, :field, :file_name, :content_type, :data)
include Logging
def upload
uploader = uploader_class.new(model, field)
store = Cloudinary::CarrierWave::Storage.new(uploader)
io = Cloudinary::Blob.new(data, original_filename: file_name, content_type: content_type)
uploader.cache!(io)
store.store!(uploader.file)