Skip to content

Instantly share code, notes, and snippets.

View lucatironi's full-sized avatar

Luca Tironi lucatironi

View GitHub Profile
class ApplicationController < ActionController::Base
before_filter :set_locale
protect_from_forgery
def set_locale
logger.debug "* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}"
I18n.locale = current_user ? current_user.locale : (params[:locale] || extract_locale_from_accept_language_header || I18n.default_locale)
logger.debug "* Locale set to '#{I18n.locale}'"
end
@lucatironi
lucatironi / gist:3899430
Created October 16, 2012 13:55
Fixed scrolling sidebar
// Keeps the order recap box fixed on scroll, a la Apple Store
// http://jqueryfordesigners.com/fixed-floating-elements
$(function () {
var msie6 = $.browser == 'msie' && $.browser.version < 7;
if (!msie6 && $('#sidebar').length > 0 && false) {
var top = $('#logo').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/, 0)),
order_recap_height = $('#sidebar').height(),
order_form_wrapper_height = $('#main-content').height();
@lucatironi
lucatironi / order.rb
Created December 2, 2011 16:28
Order Placement Wizard for a services e-commerce
class Order < ActiveRecord::Base
extend FriendlyId
belongs_to :customer
belongs_to :service
belongs_to :request_address, :foreign_key => "request_address_id", :class_name => "Address"
belongs_to :billing_address, :foreign_key => "billing_address_id", :class_name => "Address"
has_many :options, :through => :choices, :autosave => true
has_many :choices, :dependent => :destroy
@lucatironi
lucatironi / categories_routing_spec.rb
Created December 2, 2011 15:22
Github's Style Routes
require 'spec_helper'
describe CategoriesController do
describe "routing" do
it '/solutions to Category#index' do
path = categories_path
path.should == '/solutions'
{ :get => path }.should route_to(
:controller => 'categories',
:action => 'index'
@lucatironi
lucatironi / index.xml.builder
Created December 2, 2011 15:05
Sitemap.xml for a Rails App for Google Webmasters Tools
# app/views/sitemap/index.xml.builder
xml.instruct!
xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
xml.url do
xml.loc root_url
xml.priority 1.0
end
xml.url do
@lucatironi
lucatironi / api.rb
Created December 1, 2011 16:12
Snippets for an API with grape
# Server App
# This file must be in lib/myapp/api.rb
module MyApp
module Entities
class Products < Grape::Entity
expose :id, :code, :name, :short_description
expose :description, :unless => { :collection => true }
expose (:category) { |model, options| model.category.name }
expose (:brand) { |model, options| model.brand.name }
end
@lucatironi
lucatironi / gist:1339779
Created November 4, 2011 16:33
state_machine
# GemFile
gem 'state_machine'
# offerta.rb
scope :nuove, Offerta.where(:stato => "nuova")
STATI = %w(nuova inviata accettata rifiutata scaduta annullata)
state_machine :stato, :initial => :nuova do
before_transition any => :inviata do |offerta, transition|