Skip to content

Instantly share code, notes, and snippets.

require "time"
require "date"
class Date
def to_time
Time.local(year, month, day)
end
end
class Time
@ryanb
ryanb / development.rb
Created November 27, 2012 21:38
Some rack middleware to add headers to asset pipeline.
class AssetHeaders
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
response = @app.call(env)
if request.path =~ /^\/assets\//
# there maybe a better way to add headers
@ryanb
ryanb / index.js.erb
Created December 16, 2011 23:22
Infinite scrolling solution covered in revised episode #114: http://railscasts.com/episodes/114-endless-page-revised
$('#products').append('<%= j render(@products) %>');
<% if @products.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@products) %>');
<% else %>
$('.pagination').remove();
<% end %>
# Examples of Nesting Associated Attributes
# This is a look at the various ways to set associated attributes through
# nested parameters. For example, let's say Project has_many :tasks and
# we want to update the tasks the same time we edit a project. There are
# several ways this interface could be handled, and here are a few.
#
# For these examples, we're trying to create two new tasks and update an
# existing task (with id 3) through a project.
# Approach 1
@ryanb
ryanb / railscasts_episodes.rb
Created June 4, 2012 06:01
Download source code for all RailsCasts episodes. You may want to cd into an empty directory first.
require "rubygems"
require "octokit" # gem install octokit
1.upto(5) do |page|
Octokit.repositories("railscasts", page: page, per_page: 100).each do |repo|
system "git clone git://github.com/railscasts/#{repo.name}"
end
end
@ryanb
ryanb / rails_3_1_rc4_changes.md
Created May 6, 2011 01:10
The Changelogs for Rails 3.1 Beta 1

Railties 3.1 RC4

  • The new rake task assets:clean removes precompiled assets. [fxn]

  • Application and plugin generation run bundle install unless --skip-gemfile or --skip-bundle. [fxn]

  • Fixed database tasks for jdbc* adapters #jruby [Rashmi Yadav]

  • Template generation for jdbcpostgresql #jruby [Vishnu Atrai]

# Mimic Mac OS X Finder's sort by name.
class Array
def finder_sort
sort_by do |str|
punctuation = %w[` ^ _ - , ; ! ? ' " ( ) [ ] { } @ *] + ['\\'] + %w[& # % + < = > | ~ $]
str.to_s.gsub(/\d+|[^a-z\s]/i) do |match|
if punctuation.include? match
"1".rjust(200 - punctuation.index(match), "0")
elsif match =~ /^0+$/ # make an exception for zeros
"1".rjust(101, "0")
@ryanb
ryanb / shrine_storage.rb
Created January 31, 2018 20:25
Swap between file system and in-memory storage for Shrine file uploads with RSpec.
# This sets up both a memory store and file system store for Shrine file uploads.
# We use the memory store by default but in cases where you want to test actual
# file uploads you can enable it with `file_upload: true` on a per example basis
require "shrine/storage/memory"
require "shrine/storage/file_system"
# We use a delegator to swap out the storage dynamically
# since the storage hash is duplicated for each uploader
class Shrine::Storage::Dynamic < SimpleDelegator
def initialize(initial, storages)
@ryanb
ryanb / README.md
Created May 15, 2011 16:24 — forked from copiousfreetime/my-gh-issues.rb
My Github Issues

First install the required gems:

gem install octokit awesomeprint rainbow

Then run it to extract all of your open GitHub issues into files (with comments).

ruby my-gh-issues.rb
@ryanb
ryanb / favorite_gems.md
Created March 4, 2011 17:31
A list of my favorite gems for various tasks.