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 => 4) | |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title> RottenPotatoes! </title> | |
<link rel="stylesheet" href="https://getbootstrap.com/docs/4.0/dist/css/bootstrap.min.css"> | |
<%= javascript_include_tag :application %> | |
<%= csrf_meta_tags %> | |
</head> | |
<body> | |
<div class="container"> |
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 => 4) | |
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
class MoviesController < ApplicationController | |
def index | |
@movies = Movie.all | |
@top_5 = Movie.joins(:reviews).group('movie_id'). | |
order("AVG(potatoes) DESC").limit(5) | |
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
<!-- a cacheable partial for top movies --> | |
<%- cache('top_moviegoers') do %> | |
<ul id="topmovies"> | |
<%- @top_5.each do |movie| %> | |
<li> <%= movie.name %> </li> | |
<% end %> | |
</ul> | |
<% 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 Moviegoer | |
attr_accessor :name, :street, :phone_number, :zipcode | |
validates :phone_number, # ... | |
validates :zipcode, # ... | |
def format_phone_number ; ... ; end | |
def check_zipcode ; ... ; end | |
def format_address(street, phone_number, zipcode) # data clump | |
# do formatting, calling format_phone_number and check_zipcode | |
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 Config | |
def self.emailer | |
if email_disabled? then NullMailer else | |
if has_amiko? then Amiko else MailerMonkey end | |
end | |
end | |
end | |
class NullMailer | |
def initialize ; end | |
def send_email_to(*args) ; true ; 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
# LSP-compliant solution: replace inheritance with delegation | |
# Ruby's duck typing still lets you use a square in most places where | |
# rectangle would be used - but no longer a subclass in LSP sense. | |
class Square | |
attr_accessor :rect | |
def initialize(side, top_left) | |
@rect = Rectangle.new(side, side, top_left) | |
end | |
def area ; rect.area ; end | |
def perimeter ; rect.perimeter ; 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 Rectangle | |
attr_accessor :width, :height, :top_left | |
def initialize(width,height,top_left) ... ; end | |
def area ... ; end | |
def perimeter ... ; end | |
end | |
# A square is just a special case of rectangle...right? | |
class Square < Rectangle | |
# ooops...a square has to have width and height equal | |
attr_reader :width, :height, :side |
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 AmikoAdapter | |
def initialize ; @mailer = Amiko.new(...) ; end | |
def send_email | |
@mailer.authenticate(...) | |
@mailer.send_message(...) | |
end | |
end | |
# Change the controller method to use the adapter: | |
def advertise_discount_for_movie | |
moviegoers = Moviegoer.interested_in params[:movie_id] |