Skip to content

Instantly share code, notes, and snippets.

View mgreenly's full-sized avatar

Michael Greenly mgreenly

View GitHub Profile
@mgreenly
mgreenly / boot_strap_form_builder.rb
Created December 28, 2011 05:09
More on Twitter Bootsrap error handling in rails #1
class BootStrapFormBuilder < ActionView::Helpers::FormBuilder
# non-relevant code removed....
def text_field(*args, &block)
input_wrapper(args.first) {
super(*args, &block)
}
end
@mgreenly
mgreenly / _form.html.erb
Created December 28, 2011 05:10
More on Twitter Bootsrap error handling in rails #2
<%= form_for(@item, :builder => BootStrapFormBuilder) do |f| %>
<fieldset>
<legend><%= content_for :page_heading %></legend>
<%= f.text_field :pn %>
<%= f.text_field :desc %>
<%= f.text_area :memo, :rows => 3 %>
<%= f.select :vendor_id, Company.all(:order => [:name]).collect {|company| [company.name, company.id]}, {:include_blank => true}, {:class => 'span4'} %>
SELECT
users.fname,
users.lname,
TIMEZONE('UTC', users.created_at) AT TIME ZONE 'CST6CDT'
FROM
users;
t = DateTime.parse("2011/01/01T00:00") + (Time.now.getlocal.utc_offset * -1).seconds
# Sat, 01 Jan 2011 06:00:00 +0000
t.to_time.localtime
# 2011-01-01 00:00:00 -0600
@mgreenly
mgreenly / gist:1563536
Created January 5, 2012 03:13
Date Time Parse Is In UTC Time
Time.zone
# (GMT-06:00) Central Time (US & Canada)
DateTime.parse("2011/01/01T00:00").utc?
# true
DateTime.parse("2011/01/01T00:00").to_time.getlocal
# 2010-12-31 18:00:00 -0600
@mgreenly
mgreenly / date.rb
Created January 7, 2012 02:22
Extending Date._parse in Ruby
# lib/core_ext/date.rb
require 'date'
class Date
class << self
alias :original_parse :_parse
def _parse(input, comp=true)
# mm-dd-yyyy
@mgreenly
mgreenly / gist:1573597
Created January 7, 2012 02:52
Date Time Parsing in Controllers #1
class ItemsController < ApplicationController
# unrelated code....
def create
@item = Item.new(params[:item])
@item.invented_at = DateTime.parse(params[:item][:invented_at])
if @item.save
redirect_to @item, :notice =>"The Item successfully created"
@mgreenly
mgreenly / gist:1573618
Created January 7, 2012 02:59
Date Time Parsing in Controllers #2
# app/controllers/items_controller.rb
class ItemsController < ApplicationController
# unrelated code....
def create
@item = Item.new(params[:item])
@item.invented_at = Time.strptime(params[:item][:invented_at], "%m/%d/%Y")
if @item.save
@mgreenly
mgreenly / string_agg.rb
Created January 21, 2012 19:18
postgres aggregate string`
User.
select("users.id, users.full_name, string_agg(roles.name, ', ') as roles_col").
joins(:memberships => :role).
group('users.id').
each_with_object([[:id, :full_name, :roles]]){ |u,a|
a << [u.id, u.full_name, u.roles_col.split(',').map(&:strip) ]
}
@mgreenly
mgreenly / database.yml
Created January 27, 2012 16:33
generic database.yml for postgres
development:
adapter: postgresql
encoding: unicode
database: <%= File.basename(Rails.root) %>_development
pool: 5
username: <%= ENV['USER'] %>
password:
test:
adapter: postgresql