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
View gist:1fabe407f6c2ccf2e07e
A hungry bloke staggering through the desert finds two Bedouins, and asks if he can share bread with them.
The tall Bedouin has 5 loaves, the short Bedouin has 3 loaves of bread.
They welcome the visitor, and the three of them consume the 8 loaves of bread.
The stranger thanks the Bedouins for their hospitality, and leaves them with 8 gold coins.
The tall Bedouin takes 5 coins, leaving three.
But the short Bedouin says they both shared their bread, and therefore should split the 8 coins four and four.
Question:
Which if either of the two Bedouins is right?
@bsimpson
bsimpson / gist:2312760
Created Apr 5, 2012
Error message display
View gist:2312760
# Introduction
Error messages are currently not handled in a consistent manner in Batchdeux's forms. I have modified the `field_error_proc` method which serves as a global way to change the behavior of form errors. By modifying this, I have given us the option to display errors in several ways:
# Options
Options can be passed on an individual `:input` basis.
* `suppress_error` - Do not show any inline errors
* `error_before` - Show the label tag before the :input tag
@bsimpson
bsimpson / gist:2312761
Created Apr 5, 2012
Error message display
View gist:2312761

Introduction

Error messages are currently not handled in a consistent manner in Batchdeux's forms. I have modified the field_error_proc method which serves as a global way to change the behavior of form errors. By modifying this, I have given us the option to display errors in several ways:

Demo

git fetch
git checkout bls/23176299_data_validation_errors

Be sure to restart your webserver, as changes have been made to the config/ directory

@bsimpson
bsimpson / gist:3019629
Created Jun 29, 2012
How to configure settings on an AR model
View gist:3019629

Intro

I have a Ruby model that needs to have a configuration option passed to it i.e. config/initializers/contact_import.rb define options so that:

ContactImport.import_mapping # => {'csv' => ..., 'csv_row' => ..., etc}

Option 1

Works in production environment, but not in development since classes reload

# config/initializers/contact_import.rb
@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() {
@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: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: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
View gist:3974262
class Foo
end
def test_class(foo)
case foo.class
when Foo
puts "foo"
else
puts "other"
end
@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
You can’t perform that action at this time.