Skip to content

Instantly share code, notes, and snippets.

View olivoil's full-sized avatar

Olivier Melcher olivoil

View GitHub Profile
# Example simple_form input builder to render tags from an Array field,
# for use with my fork of mongoid_taggable:
#
# https://github.com/ches/mongoid_taggable
#
# Once you've configured your app to load this, use it in forms thusly:
#
# <%= f.input :tags, :as => :tags %>
#
module SimpleForm
@olivoil
olivoil / mongodb_at_montreal_rb.js
Created February 16, 2011 04:15
MongoDB code examples for Montreal.rb presentation // in Javascript
// Create database - Just use it, it gets created on the fly if it doesn't exist yet
use blog_development;
// Create table - Collections are schema-less, no need to declare a structure.
// They are also created on the fly when you use them
show collections;
// Create index
// Indexing on _id is done automatically by mongoDB
// with the only exception of Capped Collections (more on this later...)
@olivoil
olivoil / mongodb_at_montreal.rb
Created February 16, 2011 04:15
MongoDB code examples for Montreal.rb presentation # in Ruby
## Geolocation [ruby]
# First, create the index on a field containing long-lat values:
articles.create_index([["location", Mongo::GEO2D]])
# Then get a list of the twenty locations nearest to the point 50, 50:
articles.find({"location" => {"$near" => [50, 50]}}, {:limit => 20}).to_a
# collection.index_information() will get you a list of indexes on a Collection
@olivoil
olivoil / persisting_mongoid_criteria.rb
Created March 15, 2011 16:18
Example model to persist Mongoid::Criteria as Searches
class Search
include Mongoid::Document
field :query, :type => Hash
field :collection
embedded_in :user
def to_criteria
Mongoid::Criteria.new( collection.titleize.constantize ).fuse( query )
//
// Backbone.Rails.js
//
// Makes Backbone.js play nicely with the default Rails setup, i.e.,
// no need to set
// ActiveRecord::Base.include_root_in_json = false
// and build all of your models directly from `params` rather than
// `params[:model]`.
//
// Load this file after backbone.js and before your application JS.
# Extend jQuery objects with Underscore collection methods.
#
# Each collection method comes in two flavors: one prefixed
# with _, which yields a bare DOM element, and one prefixed
# with $, which yields a jQuery-wrapped element.
#
# So if `this` is a jQuery object, instead of:
#
# _.max @, (el) -> $(el).height()
#
@olivoil
olivoil / _mixins.scss
Created May 5, 2011 17:08 — forked from garyharan/_mixins.scss
Useful scss mixins (rounded corners, gradients, text-field, button)
@mixin box-shadow($top, $left, $blur, $color, $inset: false) {
@if $inset {
-webkit-box-shadow:inset $top $left $blur $color;
-moz-box-shadow:inset $top $left $blur $color;
box-shadow:inset $top $left $blur $color;
} @else {
-webkit-box-shadow: $top $left $blur $color;
-moz-box-shadow: $top $left $blur $color;
box-shadow: $top $left $blur $color;
}
@olivoil
olivoil / pjax_get_forms.coffee
Created June 17, 2011 18:10
pjax on forms which use method=GET
# Source: https://github.com/nz/pjax-rails/commit/9306f6196053e1ae8fb546077e2dc009c5ab3c15
$ ->
$('a:not([data-remote]):not([data-behavior])').pjax('[data-pjax-container]')
$('form[method=get]:not([data-remote])').live 'submit', (event) ->
event.preventDefault()
$.pjax
container: '[data-pjax-container]'
url: this.action + '?' + $(this).serialize()
@olivoil
olivoil / vim7.3_mac_install.rb
Created August 29, 2011 04:32 — forked from sirupsen/vim7.3_mac_install.rb
Script to install Vim 7.3 with ruby support for Mac OS X Lion
# requires root permissions in /usr/bin/
star = String.new
8.times { star += "*" }
Star = "\n#{star * 3}\n"
def newblock string
puts "\n#{Star}#{string}#{Star}\n"
end
@olivoil
olivoil / coffeescript_converter.rb
Created September 9, 2011 20:54 — forked from phaer/coffeescript_converter.rb
A trivial CoffeeScript.org -> Javascript plugin for jekyll ( https://github.com/mojombo/jekyll ). Put this file in '_plugins/' and write a YAML header to your .coffee files (i.e. "---\n---\n")
module Jekyll
require 'coffee-script'
class CoffeeScriptConverter < Converter
safe true
priority :normal
def matches(ext)
ext =~ /coffee/i
end