This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rails generate model Moviegoer name:string provider:string uid:string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Review < ActiveRecord::Base | |
belongs_to :movie | |
belongs_to :moviegoer | |
end |