Skip to content

Instantly share code, notes, and snippets.

View amiel's full-sized avatar

Amiel Martin amiel

View GitHub Profile
class Foo < ActiveRecord::Base
extend TimeScopes
# ...
end
{
"auto_complete_commit_on_tab": false,
"color_scheme": "Packages/Tomorrow-Theme/Tomorrow-Night-Eighties.tmTheme",
"ensure_newline_at_eof_on_save": true,
"font_face": "Inconsolata-dz",
"font_size": 10.0,
"highlight_line": true,
"rulers":
[
80,
@amiel
amiel / README.md
Created July 27, 2012 18:40
Looking for feedback on a change to ActiveModel

Hello, I'm looking for feedback on a possible pull-request to rails.

Here's the idea: right now any time an ActiveModel::Name is needed, it is accessed through model.class.model_name.

In other words, currently, it's something like this:

  # Somewhere in rails
  model.class.model_name
@amiel
amiel / controller.rb
Created July 12, 2012 06:13
A super simple inline object to render a form
class SessionsController < ApplicationController
def new
@user = OpenStruct.new.tap do |model|
def model.class
OpenStruct.new(name: 'User').tap do |klass|
klass.model_name = ActiveModel::Name.new(klass)
end
end
end
@amiel
amiel / benchmarker.rb
Created June 26, 2012 19:02
Delegate all methods to the decorated class and benchmark each call.
require 'benchmark'
class Benchmarker < SimpleDelegator
def method_missing(method, *args, &block)
__log__ "Running #{ method }(#{ args.inspect.gsub(/^\[|\]$/, '') })"
result = nil
__benchmark__ { result = super }
@amiel
amiel / gist:2871121
Created June 4, 2012 22:11
google maps api v3 custom icons hack for Internet Explorer
var point = xxx; // set from geolocation
var image = new google.maps.MarkerImage(
map_icon,
new google.maps.Size(18,29),
new google.maps.Point(0,0),
new google.maps.Point(0,29)
);
var marker = new google.maps.Marker({
@amiel
amiel / my_uploader.rb
Created May 22, 2012 20:51
Simple watermarking with Thumbkit and CarrierWave
class MyUploader < CarrierWave::Uploader::Base
include Thumbkit::Adapters::CarrierWave
# Watermark should always be processed after thumbkit, ensuring that we always
# have a valid image and we don't need to change the extension
def watermark(watermark_image, options = {})
cache_stored_file! if !cached?
Watermarker.new(current_path, watermark_image).watermark!(options)
end
@amiel
amiel / gist:2714762
Created May 16, 2012 23:06
Fairly fast tests, but there's one that's obviously the slowest
Running: spec/views/api/show.json.jbuilder_spec.rb
.
Finished in 0.64406 seconds
1 example, 0 failures
Randomized with seed 55796
@amiel
amiel / gist:2705150
Created May 15, 2012 21:07
Which is better?
t = Collection.arel_table
where(t[:public].eq(true).or(t[:user_id].eq(user.id)))
# or
where('public = ? OR user_id = ?', true, user.id)
@amiel
amiel / description.md
Created May 14, 2012 23:43
A snippet to help with debugging

puts-snippet

When adding quick debugging lines I like to include the file, line number, and method name so that I can easily find them again. This snippet makes that really easy.

It gives you something like this:

puts "#{__FILE__}:#{__LINE__}:(in full_filename) debug infos"