Skip to content

Instantly share code, notes, and snippets.

View armandofox's full-sized avatar

Armando Fox armandofox

View GitHub Profile
@armandofox
armandofox / dip_example_2.rb
Created May 11, 2021 16:39
dip_example_2.rb
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]
@armandofox
armandofox / demeter_example.rb
Created May 11, 2021 16:39
demeter_example.rb
# 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)
@armandofox
armandofox / demeter_delegation_1.rb
Created May 11, 2021 16:39
demeter_delegation_1.rb
# 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
@armandofox
armandofox / show.html.erb
Created February 22, 2021 00:59
show.html.erb
<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">
@armandofox
armandofox / controller_forms_methods.rb
Created February 22, 2021 00:59
controller_forms_methods.rb
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: " +
@armandofox
armandofox / validation_example.rb
Created February 22, 2021 00:59
validation_example.rb
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')
@armandofox
armandofox / new_review.html.erb
Created February 22, 2021 00:59
new_review.html.erb
<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 %>
@armandofox
armandofox / association1.rb
Created February 22, 2021 00:59
association1.rb
# 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
@armandofox
armandofox / has_many_through_example.rb
Created February 22, 2021 00:59
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.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!
@armandofox
armandofox / gem_example.rb
Created December 10, 2020 00:33
gem_example.rb
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]"
})