Skip to content

Instantly share code, notes, and snippets.

View madhums's full-sized avatar

madhums madhums

View GitHub Profile
@madhums
madhums / minifind.rb
Created November 12, 2010 09:07
mini-find - this will save a few key strokes!
class << ActiveRecord::Base
alias_method :[], :find
end
@post = Post[5]
@post = Post[:first]
@post = Post[:all, :conditions => { :name => "first post" }]
@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.rb
Created November 15, 2010 10:49
OmniAuth configuration for rails 2.3.x
#config/initializers/omniauth.rb
require 'openid/store/filesystem'
ActionController::Dispatcher.middleware.use OmniAuth::Builder do #if you are using rails 2.3.x
#Rails.application.config.middleware.use OmniAuth::Builder do #comment out the above line and use this if you are using rails 3
provider :twitter, 'key', 'secret'
provider :facebook, 'app_id', 'secret'
provider :linked_in, 'key', 'secret'
provider :open_id, OpenID::Store::Filesystem.new('/tmp')
end
@madhums
madhums / authlogic-configuration.rb
Created November 15, 2010 12:33
authlogic configuration : If you want to have password and password confirmation required on signup form AND optional (i.e, leave it blank) in profile update, this is the configuration
class User < ActiveRecord::Base
attr_accessor :password_confirmation
acts_as_authentic do |c|
c.validates_length_of_login_field_options :within=>1..30 #username can be 1 to 30 characters long
c.validates_format_of_login_field_options = {:with => /^[a-zA-Z0-9_]+$/, :message => I18n.t('error_messages.login_invalid', :default => "should use only alphabets, numbers and underscores no other characters.")} #username can only contain alphabets, numbers and "_" no other characters permitted
#the below two would make password and password_confirmation optional i.e, you don't have to fill it.
c.ignore_blank_passwords = true #ignoring passwords
c.validate_password_field = false #ignoring validations for password fields
end
@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 / 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 / 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 / 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() {