Skip to content

Instantly share code, notes, and snippets.

View hakanensari's full-sized avatar

Hakan Ensari hakanensari

  • Amsterdam
  • 06:43 (UTC +02:00)
View GitHub Profile
def round_to(places, number)
x = (10 ** places).to_f
(number * x).round / x
end
def select_datetime_range(object_name, method, max_time, scheduled_at = nil)
if scheduled_at
default_date, default_hour = @thing.scheduled_at.strftime("%Y-%m-%d"), @thing.scheduled_at.hour
else
default_date, default_hour = "prompt", "prompt"
end
hours = [["Time", "prompt"]] + (0..23).collect { |hour| [("0" + hour.to_s)[-2, 2] + ":00", hour] }
html = ""
days = [["Date", "prompt"]] + (Time.now..max_time).step(1.day).collect { |day| [day.strftime("%b %d"), day.strftime("%Y-%m-%d")] }
html << select_tag("#{object_name}_#{method}_date", options_for_select(days, default_date), { :name => "#{object_name}[#{method}[date]" })
@hakanensari
hakanensari / keeps_books_models.rb
Created July 30, 2009 15:31
Modeling bookkeeping app in Rails
class Account < ActiveRecord::Base
acts_as_tree
belongs_to :asset_type
has_many :transactions
end
class AssetType < ActiveRecord::Base
has_many :accounts
end
Transaction when first created
- should belong to an account
- should have an amount
- should have a description
- should not reconcile
Transaction when matched with a transaction of the inverse amount
- should reconcile
- should not reconcile when match is removed
class String
def camelize
self.gsub(/(^|_)(.)/) { $2.upcase }
end
end
class PagesController < ApplicationController
before_filter :login_required, :except => [ :show ]
# GET /pages
# GET /pages.xml
def index
@pages = Page.find(:all)
respond_to do |format|
format.html # index.html.erb
class PagesController < ApplicationController
before_filter :login_required, :except => [ :show ]
# GET /pages
# GET /pages.xml
def index
@pages = Page.find(:all)
respond_to do |format|
format.html # index.html.erb
# Following works with PostgreSQL when I want to SELECT DISTINCT
# and ORDER BY a field but use another if the first is NULL.
has_many :things,
:through => :hashtags,
:select => "DISTINCT things.*, CASE WHEN things.completed_at IS NOT NULL THEN things.completed_at ELSE things.created_at END AS created_or_completed_at",
:order => "created_or_completed_at DESC"
Then /^inspect #{capture_model}$/ do |name|
p model(name).inspect
end
Then /^inspect the (\w+) of #{capture_model}$/ do |association, name|
p model(name).send(association).inspect
end
Then /^eval "([^"]+)"$/ do |ruby_code|
p instance_eval(ruby_code)