Skip to content

Instantly share code, notes, and snippets.

Ben Simpson bsimpson

Block or report user

Report or block bsimpson

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bsimpson
bsimpson / gist:4530464
Created Jan 14, 2013
Ternary in CoffeeScript
View gist:4530464

if foo then 'bar' else 'baz'

// Generated by CoffeeScript 1.3.3

if (foo) {
  'bar';

} else {
  'baz';
@bsimpson
bsimpson / gist:4286706
Created Dec 14, 2012
I18n translation with Integers for map keys
View gist:4286706

Introduction

I18n uses YAML files to store translations, however when using an integer as a map key, the translation is then missing when referenced. This may be unexpected at first, but below describes the translation process, and why it is happening.

Sample YAML file

en:
  1:
    foo: 'bar'

Invokation

@bsimpson
bsimpson / gist:4088286
Created Nov 16, 2012
Rails and Javascript UTC timestamps
View gist:4088286

#Rails:#

ActiveSupport.use_standard_json_time_format #=> true

http://apidock.com/rails/DateTime/to_json "Returns a JSON string representing the datetime. If ActiveSupport.use_standard_json_time_format is set to true, the ISO 8601 format is used."

http://en.wikipedia.org/wiki/ISO_8601 "If the time is in UTC, add a Z directly after the time without a space. Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or "144515Z"."

View test.rb
module Foo
def say(text)
puts "Foo::say #{text}"
end
end
class Craig
include Foo
def say(text)
super
@bsimpson
bsimpson / gist:4001391
Created Nov 2, 2012
Moving Paperclip S3 attachments
View gist:4001391

Intro

The class below has a Paperclip attachment that is stored on S3. The attachment has a custom processor that takes dimensions and crops the image from user defined points. There is a need to move this post-processed attachment from one contact to another, and the copy_avatar_to methods accomplishes this without reprocessing the attachment. Reprocessing would result in the cropping points being lost, and the original image source being copied over, and resized according to the styles.

Code Example

#  avatar_content_type :string(255)
#  avatar_file_name    :string(255)
#  avatar_file_size    :integer
#  avatar_updated_at   :datetime
View gist:3974262
class Foo
end
def test_class(foo)
case foo.class
when Foo
puts "foo"
else
puts "other"
end
@bsimpson
bsimpson / gist:3893334
Created Oct 15, 2012
Using class methods instead of scopes
View gist:3893334
class Foo < ActiveRecord::Base
def self.foo
where("1=1")
end
def self.bar
where("2=2")
end
end
@bsimpson
bsimpson / gist:3716869
Created Sep 13, 2012
Validating within a Transaction
View gist:3716869
# == Schema Information
# bar :integer
class Foo < ActiveRecord::Base
validates_uniqueness_of :bar
end
a=Foo.new(bar: 1)
a.valid? # => true
b=Foo.new(bar: 1)
@bsimpson
bsimpson / gist:3659939
Created Sep 6, 2012
Alternative Extending Mongoose.js Schemas with functions declared elsewhere
View gist:3659939
// models/player.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var Player= new Schema({
name: String
});
Player.statics = {
foo: function() {
@bsimpson
bsimpson / gist:3659771
Created Sep 6, 2012
Extending Mongoose.js Schemas with functions declared elsewhere
View gist:3659771
// models/player.js
var Player = {
statics: {
foo: function() {
console.log('foo');
}
},
methods: {
bar: function() {
You can’t perform that action at this time.