Skip to content

Instantly share code, notes, and snippets.

View adamcrown's full-sized avatar

Adam Crownoble adamcrown

View GitHub Profile
@maxschulze
maxschulze / html_aware_truncate_helper.rb
Created June 5, 2011 19:55
HTML Aware truncate for Ruby / Rails using nokogiri
# Source:
# http://blog.madebydna.com/all/code/2010/06/04/ruby-helper-to-cleanly-truncate-html.html
require "rubygems"
require "nokogiri"
module TextHelper
def truncate_html(text, max_length, ellipsis = "...")
ellipsis_length = ellipsis.length
@adamcrown
adamcrown / active_record_sessions_batch_delete.rb
Last active December 11, 2015 00:18
Deleting a large batch of session records can really overload the DB (at least MySQL). This script paces it out a bit.
def batch_session_prune(keep_after = 1.month.ago, batch_by = 1.day, pause_time = 10)
time = ActiveRecord::SessionStore::Session.order(:updated_at).first[:updated_at]
while time < keep_after
time = time + batch_by
puts "Deleting sessions from #{time.to_s(:short)}"
ActiveRecord::SessionStore::Session.where('updated_at < ?', time).delete_all
sleep pause_time
@rsierra
rsierra / 1ST README.md
Last active December 11, 2015 21:38
Rails < 2.3 patch for CVE-2013-0333 vulnerability

Rails < 2.3 patch for CVE-2013-0333 vulnerability:

  • Add CVE-2013-0333_patch.rb in '/config/initializers' directory.
  • Add okjson.rb in '/lib' directory.

To test the parser, try to decode with a bad formatted json: (I don't know if it's the best test, but you check if you are using the json parser in the rails 2.3 official patch)

  • In console, before patch:
@adamcrown
adamcrown / upay.css
Last active December 16, 2015 21:59
Bootstrap style uPay CSS
* { margin: 0; }
html, body { height: 100%; font-family: Arial, Helvetica, sans-serif; }
body { background: #f5f5f5; }
img { border-style: none; font-weight: normal; }
a:link, a:visited { color: #009; font-weight: bold; text-decoration: none; }
a:hover, a:active { color: #6cb33f; font-weight: bold; text-decoration: none; outline: none; }
a:focus, a:active { outline: none; }
:focus { outline-style: none; }
.clear { clear: both; margin: 0px; padding: 0px; height: 1px; }
p { color: #333; margin: 0px 0px 5px; font-size: 0.8em; display: block; width: 100%; }
@hiasinho
hiasinho / factory_girl_strategy_find_or_create.rb
Last active May 27, 2020 18:31
FactoryGirl strategy for adding find & find_or_create
## http://stackoverflow.com/questions/7145256
module FactoryGirl
module Strategy
class Find
def association(runner)
runner.run
end
def result(evaluation)
build_class(evaluation).where(get_overrides(evaluation)).first
@rjz
rjz / cs-jq-plugin-template.coffee
Created September 3, 2012 17:01
Coffeescript jQuery Plugin Class Template
# A class-based template for jQuery plugins in Coffeescript
#
# $('.target').myPlugin({ paramA: 'not-foo' });
# $('.target').myPlugin('myMethod', 'Hello, world');
#
# Check out Alan Hogan's original jQuery plugin template:
# https://github.com/alanhogan/Coffeescript-jQuery-Plugin-Template
#
(($, window) ->
@dhh
dhh / gist:981520
Created May 19, 2011 19:27
bulk api
# Bulk API design
#
# resources :posts
class PostsController < ActiveController::Base
# GET /posts/1,4,50,90
# post_url([ @post, @post ])
def show_many
@posts = Post.find(params[:ids])
end
@aresnick
aresnick / index.html
Last active October 22, 2021 12:58
Google Drive Export Client Side Export Example
<html>
<head>
<script type="text/javascript">
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
var CLIENT_ID = '436763072331-rod105o2ppkpjrcdpsmrvmn2st3qg9el.apps.googleusercontent.com';
var SCOPES = ['https://www.googleapis.com/auth/drive.readonly'];
@skojin
skojin / irb_require_without_bundler_hack.rb
Created January 25, 2011 13:33
workaround to load irb specific gem (loaded in .irbrc) in bundler environment, like rails3 console
# Add all gems in the global gemset to the $LOAD_PATH so they can be used in rails3 console with bundler
if defined?(::Bundler)
$LOAD_PATH.concat Dir.glob("#{ENV['rvm_path']}/gems/#{ENV['rvm_ruby_string']}@global/gems/*/lib")
end
@ryanb
ryanb / github_tree_slider.js
Created December 6, 2010 17:23
This is how GitHub's new AJAX file browser works.
GitHub.TreeSlider = function () {
if (window.history && window.history.pushState) {
function a() {
if (e.sliding) {
e.sliding = false;
$(".frame-right").hide();
$(".frame-loading:visible").removeClass("frame-loading")
}
}
if (!($("#slider").length == 0 || !GitHub.shouldSlide)) if (!navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {