Skip to content

Instantly share code, notes, and snippets.

@matenia
matenia / bootstrap_topbar_list.rb
Created March 2, 2012 14:49 — forked from tmaier/bootstrap_topbar_list.rb
BootstrapTopbarList for simple-navigation and Twitter Bootstrap integration
# Renders an ItemContainer as a <ul> element and its containing items as <li> elements.
# Prepared to use inside the topbar of Twitter Bootstrap http://twitter.github.com/bootstrap/#navigation
#
# Register the renderer and use following code in your view:
# render_navigation(level: 1..2, renderer: :bootstrap_topbar_list, expand_all: true)
class BootstrapTopbarList < SimpleNavigation::Renderer::Base
def render(item_container)
if options[:is_subnavigation]
ul_class = "dropdown-menu"
@matenia
matenia / page.rb
Created February 25, 2012 14:35
Route Constraints, mapping Friendly Id 4.x to /:id of any model
# app/models/page.rb
class Page < ActiveRecord::Base
extend FriendlyId
friendly_id :title, :use => [:slugged, :history]
end
@matenia
matenia / slug_table_access.rb slug_table_access.rb
Created February 25, 2012 13:27
Hack for FriendlyId 4.x - Access Slug table within the app
# loaded into config/initializers/slug_table_access.rb
### HACK TO LOAD SLUG TABLE ACCESS
# required for use with path constraints in routes
autoload :Slug, 'friendly_id/slug'
# Access Slug table via FriendlyId::Slug
# NOTE: In FriendlyId 3.x the Slug model is available within your app
# This is a hack for FriendlyId 4.x
@matenia
matenia / deploy.rb
Created December 30, 2011 02:36
Deploy from local project via capistrano
require "bundler/capistrano"
require 'capistrano/ext/multistage'
require 'new_relic/recipes'
set :application, "sampleapp"
set :branch, "master"
# deploy from local
set :scm, :git
set :repository, ".git"
set :deploy_via, :copy
@matenia
matenia / config-initializers-page_constraint.rb
Created December 28, 2011 07:23
Dynamic page routes with friendly_id
#config/initializers/page_constraint.rb
class PageConstraint
def initialize
@page_paths = Page.all.map { |p| "/#{p.friendly_id}"}
end
def matches?(request)
@page_paths.include?(request.path)
end
@matenia
matenia / template.rb
Created December 27, 2011 14:08
Rails 3.1 template
# Standard Init Script for New Custom Rails 3.1.3 app
RailsVersion = "3.1.3"
instructions =<<-END
Running Standard Init Script for Rails 3.1.3
--------------------------------------------------
During installation a number of questions will arise and
@matenia
matenia / awesome_nested_set_optimization_helper.rb
Created September 5, 2010 01:25 — forked from lancejpollard/awesome_nested_set_optimization_helper.rb
Converts awesome_nested_set model into a flat array (one sql query!)
def simple_nested_set(clazz) # Post for example
stack = [] # Post for example
result = []
clazz.all(:order => "lft").each do |node|
if stack.empty?
stack.push({:node => node, :children => []})
result << stack.last
next
end
if stack.last[:node].lft < node.lft && node.lft < stack.last[:node].rgt