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
  • 14:43 (UTC -05:00)
View GitHub Profile
@nacengineer
nacengineer / sea_monkeys.rb
Created March 9, 2012 16:21
Monkeypatch to Rails titleize
module Monkeys
# Here there be monkeys! This is for english langauge.
# essentially the regex doesn't titleize 1, 2, & 3 letter words
# needs improvement IMO. Maybe an array of black list words.
# eg. of an a the
module ActiveSupport::Inflector
def titleize(word)
humanize(underscore(word)).gsub(/\b('?[a-z]{3})/) { $1.capitalize }
@nacengineer
nacengineer / breadcumbs.rb
Created March 9, 2012 20:06
Cool Breadcrumb Method (rails, haml, and memcached?)
def active_site_links
# Possible memcache candidate
c = %w{editions forms questions question_answers}
h = c.inject({}) {|a,x| a[x.to_sym] = {:name => x}; a }
if h.has_key? controller.controller_name.to_sym
h[controller.controller_name.to_sym][:active] = true
end
h
end
@nacengineer
nacengineer / bitmask.rb
Created March 9, 2012 20:09
Bitmask Example
# model code
property :paths_mask, Integer
NODE_PATH = %w(path_1 path_2 path_3 path_4)
def paths=(paths)
self.paths_mask = (paths & NODE_PATH).map { |r| 2**NODE_PATH.index(r) }.sum
end
def paths
@nacengineer
nacengineer / rvm.rb
Created March 31, 2012 20:48
RVM requires for Capistrano
set :rvm_ruby_string, '<interpreter>@<gemset>'
# Load RVM's capistrano plugin.
require "rvm/capistrano"
set :rvm_path, "$HOME/.rvm"
@nacengineer
nacengineer / explain_errors.rb
Last active October 2, 2015 18:58
A little snippet to add to application_helper to handle errors in a rails 3.2 form. (requires HAML 4). This allows you to replace the generated error explanation at the top of your _form.haml partials
# create this in helpers/feedback_helper.rb
# use version 1 for haml (buggy) or version 2 for erb
# version 1 via haml
require 'haml'
module FeedbackHelper
def self.included(base)
base.class_eval do
include Haml::Helpers
@nacengineer
nacengineer / prepared_statements.rb
Created April 20, 2012 19:53
Solution found for Prepared Statements Error in Rails 3.1
# needed to fix prepared statements error in Rails 3.1
after_fork do | server, worker |
ActiveRecord:: Base.connection.reconnect!
end
@nacengineer
nacengineer / application_helper.rb
Last active October 3, 2015 22:17
Flash Messages with Bootstrap 2 and Rails 3.2.3
module ApplicationHelper
include FeedbackHelper
# Your methods here
end
@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])
@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 / 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