Skip to content

Instantly share code, notes, and snippets.

View mcmire's full-sized avatar
🎯
Not a lot of time for side projects these days, so ping me if I'm unresponsive!

Elliot Winkler mcmire

🎯
Not a lot of time for side projects these days, so ping me if I'm unresponsive!
View GitHub Profile
@mcmire
mcmire / as_datetime_insanity.txt
Created October 21, 2010 20:45
ActiveSupport DateTime sanity
$ irb
>> require 'date'
=> true
>> {"time" => DateTime.new(2010)} == {"time" => "2010-01-01T00:00:00Z"}
=> false
>> require 'activesupport'
=> true
>> {"time" => DateTime.new(2010)} == {"time" => "2010-01-01T00:00:00Z"}
=> true
@mcmire
mcmire / current_controller_and_action.rb
Created December 31, 2010 08:54
Hacking Padrino to store current "controller" and "action"
# Put this in lib/current_controller_and_action.rb
module Padrino
module Rendering
module InstanceMethods
private
# Override render to store the current template being rendered so we can refer to it in the view
# This works with Padrino >= 0.9.10
def render(engine, data=nil, options={}, locals={}, &block)
# If engine is a hash then render data converted to json
@mcmire
mcmire / ppl.rb
Created January 2, 2011 05:33
Writing `pp` output to a Rails log
require 'pp'
module Kernel
class LoggerAppender
include Singleton
def <<(msg)
RAILS_DEFAULT_LOGGER.send(:buffer) << (msg || "")
RAILS_DEFAULT_LOGGER.send(:auto_flush)
msg
end
end
@mcmire
mcmire / application.rb
Created January 19, 2011 17:09
Integrating the Logging framework into your Rails 3 app
#-----------------------------------------------------------------------------------------------
# config/application.rb
#-----------------------------------------------------------------------------------------------
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env) if defined?(Bundler)
# Bring in the Railtie that will set Rails' various logger variables to
# instances of Logging::Logger instead of Rails' built-in ActiveSupport::BufferedLogger.
@mcmire
mcmire / db_related_rails_tasks.md
Created January 19, 2011 22:28
Useful database-related tasks in Rails
SimpleForm::Components::Wrapper.module_eval do
def input_html_classes
[input_type, required_class]
end
def wrapper_html_options
input_html_classes << SimpleForm.wrapper_class
if options[:wrapper_class]
input_html_classes += options[:wrapper_class].split(" ")
end
@mcmire
mcmire / date_time_formats.md
Created October 28, 2011 18:57
Formatting dates/times in Rails 3 vs Rails 2

In Rails 2, if you wanted to define a named date/time format, the conventional way was to make an initializer which would modify the DATE_FORMATS hash that ActiveSupport mixed into Date and Time:

# config/initializers/date_and_time_formats.rb
def military_hour_to_civil_hour(hour)
  mod = (hour % 12)
  mod + (12 * (mod > 0 ? 0 : 1))
end

Date::DATE_FORMATS[:std] = lambda {|d| "#{d.month}/#{d.day}/#{d.year}" }
@mcmire
mcmire / README.md
Created November 1, 2011 02:46
Replacing Compass with our own Sass init stuff

"Why replace Compass?" you ask. "Everyone likes Compass. Compass is nice! It provides helpful CSS3 mixins, and it makes life generally easier for the Sass developer."

True, but have you ever looked at the Compass source code? I dare you to figure out how Compass initializes itself, how it parses your configuration file, and how it ties into Rails. You will quickly find that it's an absolute nightmare. There's a Compass::Compiler class which wraps Sass::Plugin::Compiler. Why? I have no clue. There is really no need for Compass, except that it provides an abstraction around Sass for different frameworks. A lot of the stuff in Compass could be in Sass itself!

The other thing that's a little frustrating (and this is more about Sass than Compass) is that paths are too closely tied together. In Sass there are two arrays: the template location array, where you tell Sass where your .sass or .scss files are and where they should end up when they are compiled; and the **

@mcmire
mcmire / demo.coffee
Created November 3, 2011 03:31
webgl stuff
# Much of this was borrowed from:
# http://blogoben.wordpress.com/
mc = {}
gl = null
$canvas = null
_initGL = ->
canvas = $canvas[0]
@mcmire
mcmire / mustache_ext.rb
Created December 10, 2011 22:58
Add unnamed arguments to Mustache helpers
require 'mustache'
class Mustache
class Parser
# Override so that after we parse the initial name, we continue by checking for arguments.
#
# This allows you to not only write:
#
# {{foo}}
#