Skip to content

Instantly share code, notes, and snippets.

View Amitesh's full-sized avatar

Amitesh Kumar Amitesh

View GitHub Profile
@Amitesh
Amitesh / gist:1117971
Created August 1, 2011 11:28
Upload file from ruby on rails program with paperclip gem
# MyModel Model
class MyModel < ActiveRecord::Base
has_attached_file :my_image,
:styles => { :small_thumb => "50x50",
:medium_thumb => "100x100"
},
:default_url => "/images/missing/prints/:style.png"
end
@Amitesh
Amitesh / gist:1124605
Created August 4, 2011 06:37
Regex with problem
1) Following regex in ruby is throwing server start (compile) time error on Mac Book Pro while on Ubuntu it is working.
# Clean all new line character
string.gsub!(/(?<!\n)\n(?!\n)/, '')
Error :
undefined (?...) sequence: /(?<!\n)\n(?!\n)/ (SyntaxError)
Solution :
string.gsub!(/\r/,"")
string.gsub!(/\n/," ")
@Amitesh
Amitesh / gist:1128275
Created August 5, 2011 19:14
Undefined entity &nbsp; while XML parsing error in firefox
XML parsing does not support &nbsp; entity. &nbsp; is XHTML entity. If we parse the html or xml or html as xml then you get "Undefined entity" error in firefox error console. To make it working use unicode of &nbsp; (non-breakibg space character) &#160; or simply put space (" " or &#32;).
for example :
<div> &nbsp; hello I am going to break. If you are going to use xml parser :( </div>
Corrected as :
<div> &#160; hello I am going to break. If you are going to use xml parser :( </div>
@Amitesh
Amitesh / gist:1134115
Created August 9, 2011 14:00
svn command
svn checkout https://server.example.com/srv/svn/agaric myworkingcopyfolder
svn status
svn update
svn add yournewfilehere
svn add yournewdirectoryincludingfiles/
svn commit -m "I made changes. Woohoo." files1 files2
Use 'add' for only new file.
http://www.linuxfromscratch.org/blfs/edguide/chapter03.html#ch03-add
@Amitesh
Amitesh / gist:1136323
Created August 10, 2011 07:45
Get next previous date
//using jquery date picker
function getPreviousDay(event){
event.preventDefault();
var currDate = $('#date_box').val();
currDate = $.datepicker.parseDate('mm/dd', currDate);
var d = currDate.getDate();
var m = currDate.getMonth();
var y = currDate.getFullYear();
@Amitesh
Amitesh / gist:1151083
Created August 17, 2011 08:21
Url validator
http://mathiasbynens.be/demo/url-regex
@Amitesh
Amitesh / gist:1158827
Created August 20, 2011 08:00
Install sphinx for Rails 3
Install sphinx for Rails 3
=================================
http://freelancing-god.github.com/ts/en/installing_sphinx.html
1) Download : http://sphinxsearch.com/files/sphinx-0.9.9.tar.gz
2) ./configure (It will configure with mysql default)
3) make
4) sudo make install
5) goto (cd) rails project directory
6) bundle exec rake ts:index RAILS_ENV=staging
@Amitesh
Amitesh / gist:1160287
Created August 21, 2011 07:24
Word wrap in Ruby
# It works for pure text only. It will fail for html and url type of text.
# http://henrik.nyh.se/2007/03/ruby-wordwrap-method
def wrap_long_string(text,max_width = 20)
(text.length < max_width) ?
text :
text.scan(/.{1,#{max_width}}/).join("<wbr>")
end
@Amitesh
Amitesh / gist:1160428
Created August 21, 2011 10:10
Paperclip file name cleanup or rename
#http://blog.wyeworks.com/2009/7/13/paperclip-file-rename
Paperclip.interpolates :default_image_type do |attachment, style|
attachment.instance.get_user_default_profile_image
end
Paperclip.interpolates :normalized_avatar_file_name do |attachment, style|
attachment.instance.normalized_avatar_file_name
end
@Amitesh
Amitesh / unzip-folder.rb
Created September 28, 2011 07:26
unzip folder in ruby
require 'rubygems'
require 'zip/zip'
def unzip_file (file, destination)
Zip::ZipFile.open(file) { |zip_file|
zip_file.each { |f|
f_path=File.join(destination, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
}