Skip to content

Instantly share code, notes, and snippets.

View timriley's full-sized avatar
🇺🇦
Standing with Ukraine

Tim Riley timriley

🇺🇦
Standing with Ukraine
View GitHub Profile
@timriley
timriley / gist:454635
Created June 27, 2010 04:27
Backup a photoset from flickr
#!/usr/bin/env ruby
require 'rubygems'
require 'flickraw'
require 'fileutils'
require 'open-uri'
YOUR_API_KEY = 'xxx'
SET_TO_BACKUP = 'yyy'
@timriley
timriley / README.md
Created May 5, 2010 03:57
Handy little class for attaching arbitrary media files

Handy little class for attaching arbitrary media files (images, audio, video, etc.) to events in a timeline application. It does a couple of nifty things for supporting the varying media types:

The before_post_process method ensures we attempt to generate thumbnails only for images.

The named scopes along with the set_item_type callback gives us a way to pull out media items of a certain type, without having to go down the often more-trouble-than-it's-worth STI path.

//parent-fact/entry/pods/item[@id=$current-id]/preceding-sibling::item[position() = 1]/text()
coffee app/scripts/*.coffee -wc -o public/javascripts

Is it just me, or is it crazy that directly saving a model created via #build or #new on a has_many :through relationship does not create a join model?

See the migrations.rb, models.rb & test_irb_session.txt files below for a concise example of the problem.

This kind of technique is used commonly on nested controllers, and it would work fine for a plain belongs_to or has_many associations. In fact, this behaviour would stop me from using any RESTful controller helpers, like resource_controller.

Is there are way to make it work? Or do I need to look for alternatives, like the below?

s = Subject.first

a = Article.new

helpers do
def mobile_request?
[ /AppleWebKit.*Mobile/, /Android.*AppleWebKit/ ].any? {|r| request.env['HTTP_USER_AGENT'] =~ r}
end
def mobile_file(name)
File.exist?("#{options.views}/#{name}#{@mobile}.haml") ? :"#{name}#{@mobile}" : name.to_sym
end
def partial(name)
haml mobile_file("_#{name}"), :layout => false
end
not_found do
render_template_if_exists!
end
def render_template_if_exists!
name = File.basename(request.path)
Dir["#{Sinatra::Application.root}/views/#{name}.*"].each do |match|
@_format = File.extname(match).sub(/^./, '')
@_template = File.basename(match, File.extname(match))
end
@timriley
timriley / .gitconfig
Created January 14, 2010 04:48 — forked from kneath/._what.md
[alias]
up = !sh -c 'git pull && git log --pretty=format:\"%Cred%ae %Creset- %C(yellow)%s %Creset(%ar)\" HEAD@{1}..'