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 EmailList | |
attr_reader :mailer | |
delegate :send_email, :to => :mailer | |
def initialize(mailer_type) | |
@mailer = mailer_type.new | |
end | |
end | |
# in RottenPotatoes EmailListController: | |
def advertise_discount_for_movie | |
moviegoers = Moviegoer.interested_in params[:movie_id] |
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
# This example is adapted from Dan Manges's blog, dcmanges.com | |
class Wallet ; attr_accessor :credit_balance ; end | |
class Moviegoer | |
attr_accessor :wallet | |
def initialize | |
# ...setup wallet attribute with correct credit balance | |
end | |
end | |
class MovieTheater | |
def collect_money(moviegoer, due_amount) |
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
# Better: delegate credit_balance so MovieTheater only accesses Moviegoer | |
class Moviegoer | |
def credit_balance | |
self.wallet.credit_balance # delegation | |
end | |
end | |
class MovieTheater | |
def collect_money(moviegoer,due_amount) | |
if moviegoer.credit_balance >= due_amount | |
moviegoer.credit_balance -= due_amount |
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
<h1>Details about <%= @movie.title %></h1> | |
<div id="metadata"> | |
<ul id="details"> | |
<li> Rating: <%= @movie.rating %> </li> | |
<li> Released on: <%= @movie.release_date.strftime('%F') %> </li> | |
</ul> | |
</div> | |
<div id="description"> |
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 MoviesController < ApplicationController | |
# 'index' and 'show' methods from Section 4.4 omitted for clarity | |
def new | |
@movie = Movie.new | |
end | |
def create | |
if (@movie = Movie.create(movie_params)) | |
redirect_to movies_path, :notice => "#{@movie.title} created." | |
else | |
flash[:alert] = "Movie #{@movie.title} could not be created: " + |
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 | |
def self.all_ratings ; %w[G PG PG-13 R NC-17] ; end # shortcut: array of strings | |
validates :title, :presence => true | |
validates :release_date, :presence => true | |
validate :released_1930_or_later # uses custom validator below | |
validates :rating, :inclusion => {:in => Movie.all_ratings}, | |
:unless => :grandfathered? | |
def released_1930_or_later | |
errors.add(:release_date, 'must be 1930 or later') if | |
release_date && release_date < Date.parse('1 Jan 1930') |
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
<h1> New Review for <%= @movie.title %> </h1> | |
<%= form_tag movie_review_path(@movie), class: 'form' do %> | |
<label class="col-form-label"> How many potatoes:</label> | |
<%= select_tag 'review[potatoes]', options_for_select(1..5), class: 'form-control' %> | |
<%= submit_tag 'Create Review', :class => 'btn btn-success' %> | |
<% 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.where(:title => 'Inception') | |
alice,bob = Moviegoer.find(alice_id, bob_id) | |
# alice likes Inception, bob less so | |
alice_review = Review.new(:potatoes => 5) | |
bob_review = Review.new(:potatoes => 3) | |
# 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
# in moviegoer.rb: | |
class Moviegoer | |
has_many :reviews | |
has_many :movies, :through => :reviews | |
# ...other moviegoer model code | |
end | |
alice = Moviegoer.where(:name => 'Alice') | |
alice_movies = alice.movies | |
# MAY work, but a bad idea - see caption: | |
alice.movies << Movie.where(:title => '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
require "stripe" # makes gem's definitions available within this file | |
Stripe.api_key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc" | |
Stripe::Charge.create({ | |
:amount => 2000, | |
:currency => "usd", | |
:source => "tok_mastercard", # obtained with Stripe.js | |
:description => "Charge for [email protected]" | |
}) |