z, ? | toggle help (this) |
space, → | next slide |
shift-space, ← | previous slide |
d | toggle debug mode |
## <ret> | go to slide # |
c, t | table of contents (vi) |
f | toggle footer |
g | toggle follow |
r | reload slides |
n | toggle notes |
p | run preshow |
P | toggle pause |
s | choose style |
class MoneyValues
def initialize(object)
@object = object
end
def self.from_placement(placement)
new(placement)
end
def values
some logic
end
def format_to_currency
end
end
class OutcomeJob < Job
data[:value] = ClaimValues.from_placement(placement).value
end
class JrrrDate
include Comparable
def ranking
# calculate ranking
end
def <=>(other)
other.ranking <=> ranking
end
end
class UserAuthenticator
def initialize(user)
@user = user
end
def authenticate(unencrypted_password)
return false unless @user
if BCrypt::Password.new(@user.password_digest) == unencrypted_password
@user
else
false
end
end
end
class SessionsController < ApplicationController
def create
user = User.where(email: params[:email]).first
if UserAuthenticator.new(user).authenticate(params[:password])
self.current_user = user
redirect_to dashboard_path
else
flash[:alert] = "Login failed."
render "new"
end
end
end
class City
include Virtus.model
attribute :name, String
end
class Address
include Virtus.model
attribute :street, String
attribute :zipcode, String
attribute :city, City
end
class User
include Virtus.model
attribute :name, String
attribute :address, Address
end
user = User.new(:address => {
:street => 'Street 1/2', :zipcode => '12345', :city => { :name => 'NYC' } })
class User
include Virtus.model
include ActiveModel::Validations
validates :name, present: true
attribute :name, String
attribute :address, Address
def persist
if valid?
...
end
end
end
class Signup
include Virtus
include ActiveModel::Validations
attribute :name, String
attribute :company_name, String
attribute :email, String
validates :email, presence: true
def save
if valid?
persist!
true
else
false
end
end
private
def persist!
@company = Company.create!(name: company_name)
@user = @company.users.create!(name: name, email: email)
end
end
class SignupsController < ApplicationController
def create
@signup = Signup.new(params[:signup])
if @signup.save
redirect_to dashboard_path
else
render "new"
end
end
end
class GeoLocation
include Virtus.value_object
values do
attribute :latitude, Float
attribute :longitude, Float
end
end
class Venue
include Virtus.value_object
values do
attribute :name, String
attribute :location, GeoLocation
end
end
venue = Venue.new(
:name => 'Pub',
:location => { :latitude => 37.160317, :longitude => -98.437500 })
p venue.location.latitude # 37.160317
class AbandonedTrialQuery
def initialize(relation = Account.scoped)
@relation = relation
end
def find_each(&block)
@relation.
where(plan: nil, invites_count: 0).
find_each(&block)
end
end
AbandonedTrialQuery.new.find_each do |account|
account.send_offer_for_support
end
old_accounts = Account.where("created_at < ?", 1.month.ago)
old_abandoned_trials = AbandonedTrialQuery.new(old_accounts)
class Pokemon < ActiveRecord::Base
scope :with_skill, -> (skill){ joins(:pokemon_skills).
where("pokemon_skills.name = ?", skill) }
scope :is_available, -> (date){ joins(:schedules).
where('schedules.day_of_week = ?', time(date).wday) }-
scope :with_weakness, -> (weakness){ joins(:pokemon_weakness).
where("pokemon_weakness.name = ?", weakness) }
end
class PokemonQuery
def initialize(relation = Pokemon.all)
@relation = relation.extending(Scopes)
end
def search
@relation
end
module Scopes
def with_skill(skill)
joins(:pokemon_skills).where("pokemon_skills.name = ?", skill)
end
def is_available(date)
joins(:schedules).where('schedules.day_of_week = ?', time(date).wday)
end
def with_weakness(weakness)
joins(pokemon_weakness).where("pokemon_weakness.name = ?", weakness)
end
end
end
PokemonQuery.new.search.with_skill("lightning").with_weakness("water")
class DonutChart
def initialize(snapshot)
@snapshot = snapshot
end
def cache_key
@snapshot.id.to_s
end
def data
# pull data from @snapshot and turn it into a JSON structure
end
end
class ActiveUserPolicy
def initialize(user)
@user = user
end
def active?
@user.email_confirmed? &&
@user.last_login_at > 14.days.ago
end
end
class FacebookCommentNotifier
def initialize(comment)
@comment = comment
end
def save
@comment.save && post_to_wall
end
private
def post_to_wall
Facebook.post(title: @comment.title, user: @comment.author)
end
end
class CommentsController < ApplicationController
def create
@comment = FacebookCommentNotifier.new(Comment.new(params[:comment]))
if @comment.save
redirect_to blog_path, notice: "Your comment was posted."
else
render "new"
end
end
end
class TripReservationsController < ApplicationController
def create
reservation = TripReservation.new(params[:trip_reservation])
trip = Trip.find_by_id(reservation.trip_id)
agency = trip.agency
payment_adapter = PaymentAdapter.new(buyer: current_user)
unless current_user.can_book_from?(agency)
redirect_to trip_reservations_page, notice: "You're not allowed to book fro\
m this agency."
end
unless trip.has_free_tickets?
redirect_to trip_reservations_page, notice: "No free tickets available"
end
begin
receipt = payment_adapter.pay(trip.price)
reservation.receipt_id = receipt.uuid
unless reservation.save
logger.info "Failed to save reservation: #{reservation.errors.inspect}"
redirect_to trip_reservations_page, notice: "Reservation error."
end
redirect_to trip_reservations_page(reservation), notice: "Thank your for yo\
ur reservation!"
rescue PaymentError
logger.info "User #{current_user.name} failed to pay for a trip #{trip.name\
Extracting a service object 34
}: #{$!.message}"
redirect_to trip_reservations_page, notice: "Payment error."
end
end
end
class TripReservationsController < ApplicationController
def create
TripReservationService.new(self).execute
end
end
class TripReservationService < SimpleDelegator
def initialize(controller)
super(controller)
end
def execute
reservation = TripReservation.new(params[:trip_reservation])
trip = Trip.find_by_id(reservation.trip_id)
agency = trip.agency
payment_adapter = PaymentAdapter.new(buyer: current_user)
...
end
end