Skip to content

Instantly share code, notes, and snippets.

View kvirani's full-sized avatar

Khurram Virani kvirani

View GitHub Profile
@kvirani
kvirani / searchable.rb
Created December 19, 2011 15:09
Searchability mixin for ActiveRecord
module S6
module Searchable
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def make_searchable(opts={})
@kvirani
kvirani / general_helpers.rb
Created December 19, 2011 18:20
GeneralHelpers used in Span6 (and other projects) as Mixin for most ActiveRecord models
# To extend AR models with general instance and class methods that are needed through the Span6 project
# Simply call the class method #general_helpers in the AR model
module S6
module GeneralHelpers
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def url_valid
url = URI.parse(get_linkedin_profile)
unless %w( http https ).include?(url.scheme) && (url.hostname.include? "linkedin") && !url.path.blank?
errors.add(:detail, "Please Enter a Valid Linkedin Profile URL")
end
rescue URI::InvalidURIError => e
errors.add(:detail, "Please Enter a Valid Linkedin Profile URL")
end
#This method works fine but How can I better handle invalid string values like linkedin_url = ">>>>>" ?
# The controller method for the same is as below
def update_additional_details
@user = current_user
@user.profile.display_education_details(show_details_for_profile?(:education))
@user.profile.display_experience_details(show_details_for_profile?(:experience))
@user.profile.display_linkedin_details(show_details_for_profile?(:linkedin_profile))
@user.profile.detail[:linkedin_profile][:data] = params[:linkedin_profile_data]
if @user.profile.save
redirect_to additional_details_user_path(@user), :notice => _("successfully_updated")
# http://stackoverflow.com/questions/6312448/how-to-disable-logging-of-asset-pipeline-sprockets-messages-in-rails-3-1
# https://github.com/rails/rails/pull/4501
# https://github.com/rails/rails/pull/4512
if Rails.env.development?
#Rails.application.assets.logger = false #Logger.new('/dev/null')
Rails::Rack::Logger.class_eval do
def call_with_quiet_assets(env)
previous_level = Rails.logger.level
Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0
@kvirani
kvirani / application_controller.rb
Created September 5, 2012 18:55
require ssl without apache on localhost
def require_ssl
if request.protocol != SECURE_PROTOCOL
redirect_to :protocol => SECURE_PROTOCOL
else # development or test
logger.info "redirected to ssl"
end
end
COMMANDS I KNOW/USE AND YOU WILL LIKELY WANT TO KNOW
tail with -n option
more, less, and cat
grep
curl
which
ps
top
kill
# Lambdas, Proc.new, and Blocks - Bitmaker breakout session
# Friday March 8, 2013
## LAMBDAS
# Basic Usage:
# Think of lambdas as methods that have no name and instead they can be assigned to a variable and thus be passed around.
l = lambda {"do or do not"}
puts l.call # returns => do or do not
class Hangman
LIST = ["canada", "england", "australia", "japan"]
attr_accessor :word, :chances, :board, :list, :guesses, :answer
class InvalidGuessException < Exception
end
def initialize()
@chances = 8
@guesses = []
@kvirani
kvirani / hellobot.rb
Created June 7, 2013 17:04
Hellobot IRC Bot that we covered in class5
require "socket"
@channel = "#bitmaker"
@greeting_prefix = "privmsg #bitmaker :"
@greeting = "hello"
def connect_to_server
server = "chat.freenode.net"
port = "6667"
nick = "HelloBot"