Skip to content

Instantly share code, notes, and snippets.

@madhums
madhums / blockcomment.rb
Created November 15, 2010 10:42
block comments in ruby
=begin
notice there is no space at the beginning of =begin
code or
#comment
.
.
.
=end
=begin
@madhums
madhums / omniauth_user_models_demo.rb
Created November 16, 2010 03:07
omniauth and user models (this is just for demo)
#app/models/user.rb
class User < ActiveRecord::Base
has_many :authorizations, :dependent => :destroy
end
#app/models/authorization.rb
class Authorization < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id, :uid, :provider
validates_uniqueness_of :uid, :scope => :provider
@madhums
madhums / authorizations_controller.rb
Created November 16, 2010 10:37
authorizations controller
#app/controllers/authorizations_controller.rb
class AuthorizationsController < ApplicationController
before_filter :require_user, :only => [:destroy]
def create
omniauth = request.env['rack.auth'] #this is where you get all the data from your provider through omniauth
@auth = Authorization.find_from_hash(omniauth)
if current_user
flash[:notice] = "Successfully added #{omniauth['provider']} authentication"
current_user.authorizations.create(:provider => omniauth['provider'], :uid => omniauth['uid']) #Add an auth to existing user
@madhums
madhums / user.rb
Created November 16, 2010 12:36
User model
#/app/models/user.rb
class User < ActiveRecord::Base
has_many :authorizations, :dependent => :destroy
def self.create_from_hash(hash)
create(:username => hash['user_info']['name'])
end
end
@madhums
madhums / authorization.rb
Created November 16, 2010 12:31
authorization model
#/app/models/authorization.rb
class Authorization < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id, :uid, :provider
validates_uniqueness_of :uid, :scope => :provider
def self.find_from_hash(hash)
find_by_provider_and_uid(hash['provider'], hash['uid'])
end
@madhums
madhums / sticky-header.js
Created March 26, 2011 16:44
Ever seen a sticky header that sticks to the top of window when scrolled?
var msie6 = $.browser == 'msie' && $.browser.version < 7;
if (!msie6) {
// 'bar' is the id of the box which needs to be fixed
var top = $('#bar').offset().top - parseFloat($('#bar').css('margin-top').replace(/auto/, 0));
$(window).scroll(function (event) {
var y = $(this).scrollTop();
if (y >= top) {
// if so, add the fixed class
$('#bar').addClass('fixed');
@madhums
madhums / change-default-text.js
Created March 28, 2011 07:49
Changing the default text value on focus with jQuery
$('.default-value').each(function() { // '.default-value' is the class for your fields having default values
var default_value = this.value;
$(this).css('color', '#999'); // this could be in the style sheet instead
$(this).focus(function() {
if(this.value == default_value) {
this.value = '';
$(this).css('color', '#333');
}
});
$(this).blur(function() {
@madhums
madhums / environment.rb
Created March 28, 2011 08:17
Upload images to paperclip with remote uri's
require 'open-uri'
@madhums
madhums / browseimage.html.erb
Created March 28, 2011 08:26
Using flickr image search api in rubyonrails
<form id="imagesearch" action="/modelname/imagesearch" method="post" >
Search for an image
<input type="text" name="query" class="floatleft" />
<input type="submit" name="submit" value="submit"/>
</form>
<center>
<div id="imageresults"></div>
</center>
@madhums
madhums / searchd
Created April 1, 2011 15:35
running searchd manually
searchd --pidfile --config config/development.sphinx.conf