Skip to content

Instantly share code, notes, and snippets.

@aiwilliams
aiwilliams / api_helpers_warden.rb
Last active December 15, 2015 00:39
Using Warden and Grape? You'll need some code to configure the Warden::Manager and install the Warden::Proxy in the Rack env. For Rails, this is typically done using Devise or rails_warden, and then you'll need some helper methods in your Grape::API, similar to those found in http://github.com/hassox/rails_warden/blob/master/lib/rails_warden/con…
# Provide access to the Warden::Proxy in the Rack env by including this module in your Grape::API:
#
# helpers Api::Helpers::Warden
#
# These methods require that something has configured the Warden::Manager, and
# the upstream middleware is in place to make the Warden::Proxy exist in the
# env! In a Rails app, this is typically done by Devise or rails_warden.
#
module Api::Helpers::Warden
@aiwilliams
aiwilliams / gist:4072840
Created November 14, 2012 15:40
SQL JOIN multiple has_many through without duplicates
sqlite3 test.db
> create table events(_id smallint, name varchar(10));
create table speakers(_id smallint, name varchar(10));
create table terms(_id smallint, name varchar(10));
create table events_speakers(event_id smallint, speaker_id smallint);
create table events_terms(event_id smallint, term_id smallint);
insert into events values(1, 'Soccer');
insert into events values(2, 'Baseball');
insert into events values(3, 'Football');
@aiwilliams
aiwilliams / README.md
Created May 16, 2012 13:52
Storyboard in RubyMotion 1.4

Start XCode and create a new Storyboard file. I closed all my other XCode projects. When you choose the location of the created file, it should be your RubyMotion project's resources directory. Add a UIViewController, and set it's identifier property to "Start". Add some UI elements so you can see it working.

When you run rake in your RubyMotion project, it will compile the .storyboard file. You could auto-load the Storyboard using a plist configuration, but you'll see code can do it too.

@aiwilliams
aiwilliams / models.coffee
Created June 22, 2011 20:59
A glimpse at how I'm doing validations.
define ['backbone', 'validate/validators'], (Backbone, Validators) ->
Validators.extend Backbone.Model
Child: class Child extends Backbone.Model
defaults:
name:''
email:''
invited:false
# Setting values on what I know are single objects in the DOM...
child = @child
@$('input[name=name]').each -> @value = child.name
@$('input[name=invite]').each -> @checked = child.invite
# Reading them in tests...
it 'should include the name in a text field', ->
input = view.$('input[name=name]')[0]
expect(input.value).toBe('Bobby')
@aiwilliams
aiwilliams / config.rb
Created May 2, 2011 21:31
Sprockets producing Handlebars templates from Haml
http_path = "/"
css_dir = "public/stylesheets"
sass_dir = "sass"
images_dir = "public/images"
http_images_dir = "images"
javascripts_dir = "public/javascripts"
fonts_dir = "public/fonts"
http_fonts_dir = "fonts"
@aiwilliams
aiwilliams / date_select.js.coffee
Created April 19, 2011 18:57
date_select Ruby vs. CoffeeScript
months = [null,'January','February','March','April','May','June','July','August','September','October','November','December']
mops = dops = yops = ''
for n in [1..12]
mops += "<option value='#{n}'>#{months[n]}</option>"
for n in [1..31]
dops += "<option value='#{n}'>#{n}</option>"
y = (new Date()).get('Year')
for n in [(y-80)..y]
yops += "<option value='#{n}'>#{n}</option>"
"<select name='born_on_month'>#{mops}</select>" +
api/v1/stop_dates.json
-----
[{"created_at":"2011-01-26T16:14:42Z","id":1,"stops_on":"2011-01-26","updated_at":"2011-01-26T16:14:42Z","truck_stops":[{"created_at":"2011-01-26T16:14:42Z","id":1,"notes":"Get your warm, delicious cupcakes in the Barnes & Noble parking lot.","stop_date_id":1,"stop_location_id":1,"stops_at":"2000-01-01T11:00:00Z","updated_at":"2011-01-26T16:14:42Z","stop_location":{"address":"201 Summit Boulevard Suite 100 Birmingham, AL","created_at":"2011-01-26T16:14:42Z","id":1,"title":"Barnes & Noble, The Summit","updated_at":"2011-01-26T16:14:42Z"}}]},{"created_at":"2011-01-26T16:14:42Z","id":2,"stops_on":"2011-01-27","updated_at":"2011-01-26T16:14:42Z","truck_stops":[{"created_at":"2011-01-26T16:14:42Z","id":2,"notes":"Yumm occurring, right in front!","stop_date_id":2,"stop_location_id":2,"stops_at":"2000-01-01T12:00:00Z","updated_at":"2011-01-26T16:14:42Z","stop_location":{"address":"4133 White Oak Drive Birmingham, AL","created_at":"2011-01-26T16:14:42Z","id":2,"title":"Primavera Coffee","
common_formats = {
:at => lambda do |time|
fmt = time.strftime("%I:%M%p").downcase.sub(":00", "")
fmt =~ /^0/ ? fmt.sub("0", "") : fmt
end
}
common_formats.each do |name, format|
Date::DATE_FORMATS[name] = format
Time::DATE_FORMATS[name] = format
@aiwilliams
aiwilliams / reload_hack.rb
Created January 7, 2011 02:23
config/initializers/reload_hack.rb
if Rails.env == "development"
lib_reloader = ActiveSupport::FileUpdateChecker.new(Dir["app/grape/api/**/*"], true) do
Rails.application.reload_routes! # or do something better here
end
ActionDispatch::Callbacks.to_prepare do
lib_reloader.execute_if_updated
end
end