Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 / 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 / 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 / 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"
def edit
@profile = Profile.find(params[:username])
what = params[:what]
if not what.nil?
if ["basics", "location", "details", "photos", "interests"].member?(what)
render :action => "edit_#{what}"
else
render :action => "edit_basics"
end
@matenia
matenia / DateTime.txt
Created March 3, 2012 11:52
strftime options rails:
Format Meaning
%a The abbreviated weekday name (“Sun’’)
%A The full weekday name (“Sunday’’)
%b The abbreviated month name (“Jan’’)
%B The full month name (“January’’)
%c The preferred local date and time representation
%d Day of the month (01..31)
%H Hour of the day, 24-hour clock (00..23)
%I Hour of the day, 12-hour clock (01..12)
%j Day of the year (001..366)
@matenia
matenia / post.rb
Created May 10, 2012 01:29
Use custom method for FriendlyId (Example)
class Post < ActiveRecord::Base
# for SO: http://stackoverflow.com/questions/10481389/friendly-id-with-two-parameters
# EXAMPLE ASSUMES YOU ARE USING FRIENDLYID 4.X
# AND THAT YOU HAVE A SLUG:STRING COLUMN ON THE MODEL
# ...
extend FriendlyId
friendly_id :generate_custom_slug, :use => :slugged
# ...