Skip to content

Instantly share code, notes, and snippets.

View shamil614's full-sized avatar

scott hamilton shamil614

View GitHub Profile
@shamil614
shamil614 / twitter_links.rb
Created July 11, 2011 20:44
Twitter Link Helper
@shamil614
shamil614 / active_link_helper.rb
Created July 26, 2011 17:12
Set active or selected links based on request path and link path
@shamil614
shamil614 / time_ago_helper.rb
Created July 26, 2011 17:15
Twitter like relatvie time stamps (2 seconds ago, 2 minutes ago, etc) *** requires time_diff gem
#requires time_diff gem
def time_ago(time)
parsed_time = Time.parse(time)
time_diff = Time.diff(parsed_time, Time.now)
formated_time = ""
if time_diff[:day] > 0 || time_diff[:month] > 0 || time_diff[:year] > 0
formated_time += "#{parsed_time.day}" if time_diff[:day] > 0
formated_time += " #{parsed_time.strftime("%b")}" if time_diff[:month] > 0
formated_time += " #{parsed_time.strftime("%y")}" if time_diff[:year] > 0
else
@shamil614
shamil614 / imagemagick-resize-crop.sh
Created October 20, 2011 21:16
Imagemagick - Resize and crop
convert original.jpg -resize 200x200^ -gravity Center -crop 200x100+0+0 +repage thumbs.jpg
@shamil614
shamil614 / array_chunk.rb
Created November 3, 2011 21:36
Method to split up / divide an array into other arrays.
class Array
def chunk(pieces=2)
len = self.length;
mid = (len/pieces)
chunks = []
start = 0
1.upto(pieces) do |i|
last = start+mid
last = last-1 unless len%pieces >= i
chunks << self[start..last] || []
@shamil614
shamil614 / split_paragraphs.rb
Created November 22, 2011 19:54
Split paragraph helper
def split_paragraphs(text,limit,wrapper_id_short=nil,wrapper_id_long=nil)
results = text.scan(/<p>.*?<\/p>/)
i = 0
length = 0
short_p = ""
while length < limit && i <= results.count - 1
short_p << results[i]
length += results[i].length - 7
i+= 1
end
@shamil614
shamil614 / split_string_on_spaces.rb
Created November 22, 2011 20:49
Split string on spaces helper
def split_on_spaces(text,limit,wrapper_id_short=nil,wrapper_id_long=nil)
results = text.split(' ')
i = 0
short_p = ""
long_p = ""
while short_p.length < limit && i <= results.count - 1
short_p << results[i] + ' '
i+= 1
end
rem = results[i..results.count-1]
@shamil614
shamil614 / escape_single_quotes.rb
Created December 15, 2011 19:10
Escape single quotes
class String
def escape_single_quotes
self.gsub(/[']/, '\\\\\'')
end
end
@shamil614
shamil614 / bootstrap_form_builder.rb
Created February 20, 2012 22:15 — forked from cbmeeks/bootstrap_form_builder.rb
Twitter Bootstrap 2.0 Form Builder & Devise
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
delegate :capture, :content_tag, :tag, to: :@template
%w[text_field text_area password_field collection_select email_field].each do |method_name|
define_method(method_name) do |name, *args|
errors = object.errors[name].any?? " error" : ""
error_msg = object.errors[name].any?? content_tag(:span, object.errors[name].join(","), class: "help-inline") : ""
content_tag :div, class: "control-group#{errors}" do
@shamil614
shamil614 / paginate_linkedin_results.rb
Created April 2, 2012 22:48
Paginate LinkedIn Gem Search Results
def paginate_linkedin_results(object,per_page=20)
# start = if params[:start] then params[:start] else count end
@total = object.total
@count = if object._count then object._count else per_page end
@start = if object._start then object._start else 0 end
@total_pages = (@total/per_page.to_f).ceil
url = if params[:start] then request.url else request.url + '&start=0' end
if @start <= 0
html_prev = content_tag("span", " << Prev ", :class => 'prev_page disabled')