Skip to content

Instantly share code, notes, and snippets.

class Person < ActiveRecord::Base
# needs_loan :boolean(1)
has_many :loan_requests
def update_needs_loan!
needs_loan = loan_requests.any? { |l| l.active? }
save!
end
end
@LRDesign
LRDesign / deploy.rb
Created March 21, 2010 18:22 — forked from netzpirat/deploy.rb
Capistrano recipe to sync rails database and files, using remote database.yml instead of local for remote credentials
set :sync_directories, ["public/assets", "public/galleries"]
set :sync_backups, 3
# doesn't include any joins from the :include => eager load call!
Payment Load (0.3ms) SELECT * FROM `payments` WHERE (`payments`.`paid_at` BETWEEN '2010-03-01 00:00:00' AND '2010-03-31 23:59:59') ORDER BY paid_at ASC
# Gives the ability to cascade upward for an attribute in a nested_set
# So, for example, if a project does not have a client specified, it will
# inherit the client of its parent, or further ancestor.
module Cascade
def self.included(klass)
klass.extend(ClassMethods)
end
module ClassMethods
def cascades(*args)
require 'tsort'
module Rails
module Initializable
def self.included(base)
base.extend ClassMethods
end
class Initializer
attr_reader :name, :block
@LRDesign
LRDesign / gist:4588699
Created January 21, 2013 19:47
The kind of thing that happens when you don't TDD... At least this class works the *second* time you instantiate it...
class Ability
include CanCan::Ability
def initialize(user)
def initialize(user)
user ||= User.new # guest user (not logged in)
# abilities defined here
if user.role == "admin"