Skip to content

Instantly share code, notes, and snippets.

@softwaregravy
softwaregravy / gist:1132410
Created August 8, 2011 18:45
Parse redirect_url
URI.parse(response.redirect_url)
@softwaregravy
softwaregravy / gist:1132585
Created August 8, 2011 20:03
spec response url for a query param
Rack::Utils.parse_query(URI.parse(response.redirect_url).query)["param"].should == "1"
@softwaregravy
softwaregravy / gist:1132571
Created August 8, 2011 19:58
parse redirect url
CGI.parse(URI.parse(response.redirect_url).query) if URI.parse(response.redirect_url).query.present?
=> {"x"=>["1"], "y"=>["2"]}
Rack::Utils.parse_query(URI.parse(response.redirect_url).query
=> {"x"=>"1", "y"=>"2"}
response.should redirect_to named_route_path(resource.id, :param => "true")
belongs_to :job, :class_name => "Delayed::Backend::ActiveRecord::Job"
# full path name required if you're in another module
# 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)
class MyModel < ActiveRecord::Base
before_create lambda {
action_1
action_2
}
def action_1
self.mydata = "default"
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"
@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