Skip to content

Instantly share code, notes, and snippets.

@madhums
madhums / minifind.rb
Created Nov 12, 2010
mini-find - this will save a few key strokes!
View minifind.rb
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 Nov 15, 2010
block comments in ruby
View blockcomment.rb
=begin
notice there is no space at the beginning of =begin
code or
#comment
.
.
.
=end
=begin
@madhums
madhums / omniauth.rb
Created Nov 15, 2010
OmniAuth configuration for rails 2.3.x
View omniauth.rb
#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 Nov 15, 2010
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
View authlogic-configuration.rb
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 Nov 16, 2010
omniauth and user models (this is just for demo)
View omniauth_user_models_demo.rb
#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
View authorizations_controller.rb
#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 Nov 16, 2010
authorization model
View authorization.rb
#/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
View user.rb
#/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 Mar 26, 2011
Ever seen a sticky header that sticks to the top of window when scrolled?
View sticky-header.js
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 Mar 28, 2011
Changing the default text value on focus with jQuery
View change-default-text.js
$('.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() {