Skip to content

Instantly share code, notes, and snippets.

# What I really wanted my logic to be
def my_method(options = {})
if options.has_key?(:key) && !options[:key].nil?
if options[:key]
# do something using the option here
else
# do something else
end
end
end
@softwaregravy
softwaregravy / gist:1210199
Created September 11, 2011 22:11
Options of Nil and Fasle
# The way I basically always check
def my_method(options = {})
if options[:key]
# do something using the option here
end
end
# Probably the proper way to check a hash for options
def my_method(options = {})
if options.has_key?(:key) && !options[:key].nil?
# do something using the option here
end
end
@softwaregravy
softwaregravy / rspec-syntax-cheat-sheet.rb
Created September 9, 2011 21:25 — forked from dnagir/rspec-syntax-cheat-sheet.rb
RSpec 2 syntax cheat sheet by example
# RSpec 2.0 syntax Cheet Sheet by http://ApproachE.com
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")
@softwaregravy
softwaregravy / my_models_controller.rb
Created August 30, 2011 03:10
The controller cleanup
def update
@my_model = MyModel.find_by_id(params[:id])
params[:my_model].try(:[], "my_value_array").try(:reject!){|v| v.blank? }
if @my_model.update_attributes(params[:my_model])
# blah blah blah
end
end
@softwaregravy
softwaregravy / my_model.rb
Created August 30, 2011 03:04
Finding the Object in the Builder
# my_value_array :text
class MyModel < AciveRecord::Base
serialize :my_value_array, Array
end
class MyModel < ActiveRecord::Base
before_create :action_1
before_create :action_2 # might depend on action_1
def action_1
self.mydata = "default"
end
def action_2
self.mycomplexdata = default + "more data"
class MyModel < ActiveRecord::Base
before_create lambda {
action_1
action_2
}
def action_1
self.mydata = "default"
end
# Bit.ly API implementation - thanks to Richard Johansso http://gist.github.com/112191
require 'httparty'
class Api::Bitly
include HTTParty
base_uri 'api.bit.ly'
format :json
# Usage: Bitly.shorten("http://example.com")
def self.shorten(url)
belongs_to :job, :class_name => "Delayed::Backend::ActiveRecord::Job"
# full path name required if you're in another module