Skip to content

Instantly share code, notes, and snippets.

View dnch's full-sized avatar

Dan Cheail dnch

  • melbourne, au
View GitHub Profile
@dnch
dnch / babies_controller.rb
Created December 22, 2011 22:49 — forked from ptagell/babies_controller.rb
Update controller
def update
if @baby.update_attributes(params[:baby])
if params[:notify] == true
flash[:notice] = "Done"
else
redirect_to(root_url(:host => with_subdomain(@baby.subdomain)), :notice => 'Your baby was successfully updated and everyone has been told the good news.')
end
else
render :action => "edit"
end
@dnch
dnch / user.rb
Created December 19, 2011 22:50 — forked from jimsynz/user.rb
WTF!
class User
include Mongoid::Document
field :email
validates_format_of :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i, :message => "is not a valid email adress."
end
@dnch
dnch / gist:1350453
Created November 9, 2011 05:02 — forked from damncabbage/gist:1350367
Calculating Mass-Assignable ActiveRecord Attributes
class Article < ActiveRecord::Base
# Has attributes: :title, :body, :active
attr_protected :active
end
class Image < ActiveRecord::Base
# Has attributes: :title, :filename, :active
attr_accessible :title
@dnch
dnch / intro.md
Created October 11, 2010 04:42 — forked from radar/intro.md

Ruby Intro - Setlist

  • no fucking semi-colons (unless you really want them)
  • irb
  • local variable definition
  • +, *, /, - are all methods
  • puts vs print
  • multiplying strings
  • using if & when
  • defining a method
grey='\e[0;90m'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$grey%}("
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$grey%}) %{$fg[yellow]%}✗%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$grey%})"
function pat_prompt {
(( spare_width = ${COLUMNS} ))
prompt=" "
@dnch
dnch / survey
Created November 2, 2009 00:56 — forked from radar/survey.md
What did you do to get good at Rails?
Who taught you what you know?
Do you have any fond (or not so fond) memories of your learning experiences?
What was your first production app and what did you learn from it?
What tools do you use to make your life easier?
What did you find particularly hard to grok when learning Rails?
When did you start on Rails?
Did you learn Ruby first or try to jump straight into Rails?
Did you read any books to get good at Rails?
What system do you use?
# named_scope :between, lambda {|start_date, end_date, field|
# { :conditions => ["\"#{field}\" >= ? AND \"#{field}\" <= ?", start_date, end_date] }
# }
From the docs...
A range may be used in the hash to use the SQL BETWEEN operator
Student.find(:all, :conditions => { :grade => 9..12 })
def v1(value)
value || "-"
end
def v2(object, method)
object && value = object.send(method) ? value : "-"
end
def self.retrieve_capture_fields(the_page, type_of_capture)
case the_page
when Event then retrieve_capture_data(type_of_capture, the_event_or_hero_page)
when HeroPage then
if the_event_or_hero_page.is_this_hero_page_associated_with_an_event && the_page.event
retrieve_capture_data(type_of_capture, the_page.event)
end
end
end
def self.determine_if_this_is_an_event_or_hero_page_mode(args)
if args[:event_url]
"event"
elsif args[:url]
"hero_page"
end
end