Skip to content

Instantly share code, notes, and snippets.

@jonpaul
jonpaul / class.rb
Created July 6, 2012 18:55
import method
class Transition
def self.sql_dump_import(dump_file)
sql = File.open(dump_file).read
sql.split(';').each do |sql_statement|
ActiveRecord::Base.connection.execute(sql_statement) unless sql_statement.blank?
end
return true
end
#SomeController
def show
with_404 { Model.find(params[:id]) }
end
#ApplicationController
@jonpaul
jonpaul / wtf.rb
Created June 30, 2012 17:31
Carnegie Mellon Example Rails Model
class Assignment < ActiveRecord::Base
# Relationships
belongs_to :user
belongs_to :project
# Scopes
scope :active, where('assignments.active = ?', true)
scope :inactive, where('assignments.active = ?', false)
scope :for_project, lambda { |project_id| where('project_id = ?', project_id) }
scope :for_user, lambda { |user_id| where('user_id = ?', user_id) }
{
"id": 7,
"tags": ["programming", "javascript"],
"users": [
{"first": "Homer", "last": "Simpson"},
[2, "Hank", "Hill", "Peter", "Griffin"]
],
"books": [
{"title": "JavaScript", "author": "Flanagan", "year": 2006},
[3, "Cascading Style Sheets", "Meyer", 2004]
@jonpaul
jonpaul / howto.rb
Created May 9, 2012 13:23
Writing image url's with fileutil and open-uri
require 'open-uri'
File.open('/Users/jon-paullussier/sandbox/mypicture.jpg', 'wb') {|f| f << open("http://i.imgur.com/0JEgI.jpg").read}
def update
@project = Project.find(params[:id])
@project.update_attributes!(params[:project])
respond_to do |format|
format.html do
if request.xhr?
# *** repond with the new value ***
render :text => params[:project].values.first
else
@jonpaul
jonpaul / wut
Created April 30, 2012 16:52
lol
gist without user
@jonpaul
jonpaul / oop.rb
Created April 18, 2012 20:47
objects can be bound to instance variables within an object
(rdb:1) self.margin_box
#<Prawn::Document::BoundingBox:0x10a0a17d8
@y=756.0,
@total_left_padding=0,
@width=356.0, @x=36,
@stretched_height=nil,
@document=#<PrawnHelper::JennerPdf:0x10a0a2b10 # <<<< Notice this object DOES NOT end before the comma, #<CLASS::MODULE::OBJECT:ID (no closing >)
@y=756.0,
@bounding_box=#<Prawn::Document::BoundingBox:0x10a0a17d8 ...>, # <<<< Notice this object ends before the comma, #<CLASS::MODULE::OBJECT:ID ...>
@page_number=1,
@jonpaul
jonpaul / model.rb
Created April 18, 2012 02:55
more initialized variables
class Model < ActiveRecord::Base
attr_accessor :an_array
WORDS = ['Some', 'Strings', 'In', 'Array']
def an_array=
an_array = words
end
end
@jonpaul
jonpaul / application_controller.rb
Created April 18, 2012 01:01
before_filter methods
class ApplicationController < ActionController::Base
protect_from_forgery
def is_a_help?
unless current_user.admin?
redirect_to :back, notice: 'Sorry.'
end
end
end