Skip to content

Instantly share code, notes, and snippets.

@Solnse
Created October 21, 2011 22:12
Show Gist options
  • Save Solnse/1305113 to your computer and use it in GitHub Desktop.
Save Solnse/1305113 to your computer and use it in GitHub Desktop.
!!!
%html
%head
= stylesheet_link_tag "application"
= javascript_include_tag "application"
= csrf_meta_tag
%body{:class => controller.controller_name}
#banner
= form_tag root_path, class: 'locale' do
= select_tag 'set_locale', options_for_select(LANGUAGES, I18n.locale.to_s), onchange: 'this.form.submit()'
= submit_tag 'submit'
= javascript_tag "$('.locale input').hide()"
= title
#columns
#side
#cart
- if @cart
= hidden_div_if(@cart.line_items.empty?, id: 'cart') { render @cart }
%ul
%li
= link_to 'Home', main_index_path
%li
= link_to 'The Hayloft Grill', main_hayloft_path
%li
= link_to 'General Store', store_path
%li
= link_to 'Events Calendar', main_entertainment_path
%li
= link_to 'About Us', main_about_path
- if session[:user_id]
%ul
%li
= link_to 'Orders', orders_path
%li
= link_to 'Products', products_path
%li
= link_to 'Users', users_path
= button_to 'Logout', logout_path, method: :delete
#main
= yield
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_filter :set_i18n_locale_from_params
before_filter :authorize
protect_from_forgery
def hayloft
@title = "The Hayloft Grill"
end
def entertainment
@title = "Entertainment and Events"
end
def about
@title = "About Us/Contact"
end
def store
@title = "General Store"
end
protected
def set_i18n_locale_from_params
if params[:locale]
if I18n.available_locales.include?(params[:locale].to_sym)
I18n.locale = params[:locale]
else
flash.now[:notice] = "#{params[:locale]} translation not available"
logger.error flash.now[:notice]
end
end
end
def default_url_options
{ locale: I18n.locale }
end
def authorize
unless User.find_by_id(session[:user_id])
redirect_to login_url, notice: "Please log in"
end
end
def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
end
#app/helpers/application_helper.rb
module ApplicationHelper
# Return a title on a per-page basis.
def title
base_title = "The Maverick Saloon"
if @title.nil?
base_title
else
"#{base_title} | #{@title}"
end
end
# hide shopping cart if empty
def hidden_div_if(condition, attributes = {}, &block)
if condition
attributes["style"] = "display: none"
end
content_tag("div", attributes, &block)
end
end
#config/routes.rb
Mavericksaloon::Application.routes.draw do
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
scope '(:locale)' do
resources :users
resources :orders
resources :line_items
resources :carts
resources :products do
get :who_bought, on: :member
end
end
get "main/index"
get "main/hayloft"
get "main/merchandise"
get "main/entertainment"
get "main/about"
get "products/index"
get 'admin' => 'admin#index'
get "store/index", as: 'store'
post "store/index", as: 'store'
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => 'main#index'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment