Skip to content

Instantly share code, notes, and snippets.

View nacengineer's full-sized avatar

David Southard nacengineer

  • UW Madison, General Library System
  • Madison, WI
  • 13:18 (UTC -05:00)
View GitHub Profile
@nacengineer
nacengineer / user_login.rb
Last active December 11, 2015 05:19
user login objects
<% @user.login_list.each do |n| %>
<li><%= n %></li>
<% end %>
# chain this onto the object
def self.last_five(id)
where(:id => id).limit(5).sort('descending')
end
@nacengineer
nacengineer / iterate.rb
Created January 16, 2013 19:36
quick iteration
a = []
@user.user_logins.each do |x|
a << x.created_at
end
# now a should have all the entries you need
a.inspect
@nacengineer
nacengineer / holiday.rb
Last active December 10, 2015 15:29
Simple Object to return Christmas and Thanksgiving as Date and query if its the Christmas holiday season
# also check out this sweet gem! https://github.com/alexdunae/holidays
class Christmas
attr_reader :christmastime, :christmas, :usa_thanksgiving
def initialize
@christmastime = is_christmastime?
end
def is_christmastime?(seed_date = Date.today, padding = 0)
@nacengineer
nacengineer / flay.rb
Last active December 10, 2015 00:49
A simple method to add to test helpers for displaying error messages
# assumptions: rails, object responds to errors
# in your test use this line where object is the object being validated
# :msg => flay_the_errors(__method__).call( object.errors)
def flay_the_errors( method )
Proc.new do |x|
y = "\n### oh noes #{method} failed! ###\n"
y << "Full Inspect:\n"
y << x.inspect << "\n"
x.each {|k,v| y << "- #{k} #{v}\n"}
y
@nacengineer
nacengineer / setup-statsd.sh
Created August 2, 2012 23:25 — forked from collegeman/setup-statsd.sh
Turn an Ubuntu 10.04 linode into a StatsD/Graphite server
# install git
sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core
# download the Node source, compile and install it
git clone https://github.com/joyent/node.git
cd node
./configure
make
sudo make install
# install the Node package manager for later use
@nacengineer
nacengineer / gsub.rb
Created June 26, 2012 20:25
gsub regex for whitespace
some_string.gsub(/\s+/, "")
@nacengineer
nacengineer / bootstrap_modal.rb
Created June 18, 2012 01:56
Add a Twitter Bootstrap Modal picture array to your view. (HAML, RAILS 3.2, Google Picasa)
# This is how I leveraged twitter bootstrap modals http://twitter.github.com/bootstrap/javascript.html#modals
# in Rails 3.2 with bootstrap-sass working. This is HAML syntax. With Twitter Bootstrap > 2.0.3.1. The pictures are
# pulled from Google Picasa Web Albums and are the url to the thumbnail image. This also explains the .gsub in the
# helper as I swap the thumbnail out out with a larger image. This will give you a thumbnail array similar to the
# "more examples" one on this page http://twitter.github.com/bootstrap/components.html#thumbnails
# in your helper file.
require 'securerandom'
def modal_page( image )
@nacengineer
nacengineer / checkbox.rb
Created June 4, 2012 16:00
Example Twitter Bootstrap / Simple Form Check box
= form.input :your_checkbox_variable, :label => false, :require => false do
= form.check_box :your_checkbox_variable
My Awesome Checkbox
@nacengineer
nacengineer / application_helper.rb
Created April 29, 2012 20:53
Railscast #272 with Pygments.rb and Redcloth 2
# Authored by Braulio Carreno on railscasts site. Put here mostly as a reference for me.
# also remember to replace albino gem with pygments.rb gem
module ApplicationHelper
class HTMLwithPygments < Redcarpet::Render::HTML
def block_code(code, language)
Pygments.highlight(code, :lexer => language)
end
end
def markdown(text)
@nacengineer
nacengineer / application_helper.rb
Created April 29, 2012 20:36
Railscast #272 with Redcarpet 2.0 helper code
def markdown(text)
options = {hard_wrap: true, filter_html: true, autolink: true, no_intraemphasis: true, fenced_code: true, gh_blockcode: true}
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML, extensions = options)
syntax_highlighter( md.render(text) ).html_safe
end
def syntax_highlighter(html)
doc = Nokogiri::HTML(html)
doc.search("//pre[@lang]").each do |pre|
pre.replace Albino.colorize(pre.text.rstrip, pre[:lang])