Skip to content

Instantly share code, notes, and snippets.

View armandofox's full-sized avatar

Armando Fox armandofox

View GitHub Profile
@armandofox
armandofox / CSS1
Created July 11, 2011 23:10
CSS trivial example
h1, #topHeadline, p.bigheader {
color: red;
font-size: 125%;
text-align: center;
border: 1px solid #d0d0c0;
}
@armandofox
armandofox / vaguefeature.rb
Created February 16, 2016 01:12
vaguefeature.rb
Feature: User can search for a movie (vague)
Feature: User can search for a movie by title (specific)
@armandofox
armandofox / associations_migration.rb
Created February 16, 2016 01:12
associations_migration.rb
# Run 'rails generate migration create_reviews' and then
# edit db/migrate/*_create_reviews.rb to look like this:
class CreateReviews < ActiveRecord::Migration
def up
create_table 'reviews' do |t|
t.integer 'potatoes'
t.text 'comments'
t.references 'moviegoer'
t.references 'movie'
end
@armandofox
armandofox / application_controller.rb
Created February 16, 2016 01:12
application_controller.rb
class ApplicationController < ActionController::Base
before_filter :set_current_user
protected # prevents method from being invoked by a route
def set_current_user
# we exploit the fact that find_by_id(nil) returns nil
@current_user ||= Moviegoer.find_by_id(session[:user_id])
redirect_to login_path and return unless @current_user
end
end
@armandofox
armandofox / login_form.html.haml
Created February 16, 2016 01:12
login_form.html.haml
#login
- if @current_user
%p.welcome Welcome, #{@current_user.name}!
= link_to 'Log Out', logout_path
- else
%p.login= link_to 'Log in with your Twitter account', '/auth/twitter'
@armandofox
armandofox / routes.rb
Created February 16, 2016 01:12
routes.rb
get 'auth/:provider/callback' => 'sessions#create'
post 'logout' => 'sessions#destroy'
get 'auth/failure' => 'sessions#failure'
@armandofox
armandofox / sessions_controller.rb
Created February 16, 2016 01:12
sessions_controller.rb
class SessionsController < ApplicationController
# user shouldn't have to be logged in before logging in!
skip_before_filter :set_current_user
def create
auth=request.env["omniauth.auth"]
user=Moviegoer.find_by_provider_and_uid(auth["provider"],auth["uid"]) ||
Moviegoer.create_with_omniauth(auth)
session[:user_id] = user.id
redirect_to movies_path
end
@armandofox
armandofox / around_filter_example.rb
Created February 16, 2016 01:12
around_filter_example.rb
# somewhat contrived example of an around-filter
around_filter :only => ['withdraw_money', 'transfer_money'] do
# log who is trying to move money around
start = Time.now
yield # do the action
# note how long it took
logger.info params
logger.info (Time.now - start)
end
@armandofox
armandofox / before_filter_login_example.rb
Created February 16, 2016 01:12
before_filter_login_example.rb
class UsersController < ApplicationController
before_filter :logged_in?, :except => [:login, :process_login]
private # subsequent methods are private to this class, until 'public' given
def logged_in?
redirect_to login_path and return unless
(@current_user = get_logged_in_user)
end
public
# ...controller actions go here
end
@armandofox
armandofox / controller_with_validation.rb
Created February 16, 2016 01:12
controller_with_validation.rb
# replaces the 'create' method in controller:
def create
@movie = Movie.new(params[:movie])
if @movie.save
flash[:notice] = "#{@movie.title} was successfully created."
redirect_to movies_path
else
render 'new' # note, 'new' template can access @movie's field values!
end
end