Skip to content

Instantly share code, notes, and snippets.

View armandofox's full-sized avatar

Armando Fox armandofox

View GitHub Profile
@armandofox
armandofox / reviews_controller.rb
Created February 16, 2016 01:12
reviews_controller.rb
class ReviewsController < ApplicationController
before_filter :has_moviegoer_and_movie, :only => [:new, :create]
protected
def has_moviegoer_and_movie
unless @current_user
flash[:warning] = 'You must be logged in to create a review.'
redirect_to '/auth/twitter'
end
unless (@movie = Movie.find_by_id(params[:movie_id]))
flash[:warning] = 'Review must be for an existing movie.'
@armandofox
armandofox / moviegoer_owns_review.rb
Created February 16, 2016 01:12
moviegoer_owns_review.rb
class ReviewsController < ApplicationController
before_filter :moviegoer_owns_review, :only => [:edit, :update]
def moviegoer_owns_review
unless Review.find_by_id(params[:id]).try(:moviegoer) == @current_user
flash[:warning] = 'You can only edit your own reviews.'
redirect_to movies_path
end
end
end
@armandofox
armandofox / omniauth_example.rb
Created February 16, 2016 01:12
omniauth_example.rb
class SessionsController < ApplicationController
def create
@user = User.find_or_create_from_auth_hash(auth_hash)
self.current_user = @user
redirect_to '/'
end
protected
def auth_hash
request.env['omniauth.auth']
end
@armandofox
armandofox / association1.rb
Created February 16, 2016 01:12
association1.rb
# it would be nice if we could do this:
inception = Movie.find_by_title('Inception')
alice,bob = Moviegoer.find(alice_id, bob_id)
# alice likes Inception, bob hates it
alice_review = Review.new(:potatoes => 5)
bob_review = Review.new(:potatoes => 2)
# a movie has many reviews:
inception.reviews = [alice_review, bob_review]
# a moviegoer has many reviews:
alice.reviews << alice_review
@armandofox
armandofox / before_save_example.rb
Created February 16, 2016 01:12
before_save_example.rb
class Movie < ActiveRecord::Base
before_save :capitalize_title
def capitalize_title
self.title = self.title.split(/\s+/).map(&:downcase).
map(&:capitalize).join(' ')
end
end
# now try in console:
m = Movie.create!(:title => 'STAR wars', :release_date => '27-5-1977', :rating => 'PG')
m.title # => "Star Wars"
@armandofox
armandofox / create_migration.txt
Created February 16, 2016 01:12
create_migration.txt
rails generate model Moviegoer name:string provider:string uid:string
@armandofox
armandofox / has_many_through_example.rb
Created February 16, 2016 01:12
has_many_through_example.rb
# in moviegoer.rb:
class Moviegoer
has_many :reviews
has_many :movies, :through => :reviews
# ...other moviegoer model code
end
alice = Moviegoer.find_by_name('Alice')
alice_movies = alice.movies
# MAY work, but a bad idea - see caption:
alice.movies << Movie.find_by_name('Inception') # Don't do this!
@armandofox
armandofox / movie_with_associations.rb
Created February 16, 2016 01:12
movie_with_associations.rb
# place a copy of the following line anywhere inside the Movie class
# AND inside the Moviegoer class (idiomatically, it should go right
# after 'class Movie' or 'class Moviegoer'):
has_many :reviews
@armandofox
armandofox / moviegoer_auth.rb
Created February 16, 2016 01:12
moviegoer_auth.rb
# Edit app/models/moviegoer.rb to look like this:
class Moviegoer < ActiveRecord::Base
attr_accessible :uid, :provider, :name # see text for explanation
def self.create_with_omniauth(auth)
Moviegoer.create!(
:provider => auth["provider"],
:uid => auth["uid"],
:name => auth["info"]["name"])
end
end
@armandofox
armandofox / review.rb
Created February 16, 2016 01:12
review.rb
class Review < ActiveRecord::Base
belongs_to :movie
belongs_to :moviegoer
end