This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def round_to(places, number) | |
x = (10 ** places).to_f | |
(number * x).round / x | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Account < ActiveRecord::Base | |
acts_as_tree | |
belongs_to :asset_type | |
has_many :transactions | |
end | |
class AssetType < ActiveRecord::Base | |
has_many :accounts | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string = <<EOF | |
foo | |
bar | |
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def camelize | |
self.gsub(/(^|_)(.)/) { $2.upcase } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer