Skip to content

Instantly share code, notes, and snippets.

@jaimerson
jaimerson / active_record_sql_approach.rb
Created August 1, 2016 20:36
ActiveRecord + Sql approach for bikes problem
class BikesReport::ActiveRecordSqlApproach
def to_a
Rental.joins(:bike)
.select(query)
.group('bikes.id')
.order('times_rented DESC')
.as_json(except: :id)
.map { |r| r.symbolize_keys!.merge!(model: MODELS.fetch(r[:model])) }
end
Benchmark.bm { |x| x.report { BikesReport::NaiveApproach.new.to_a } }
# user system total real
# 9.670000 0.650000 10.320000 ( 10.540757)
@jaimerson
jaimerson / naive_approach.rb
Last active August 1, 2016 20:33
Naïve approach for bikes tutorial
class BikesReport::NaiveApproach
def to_a
rentals.group_by { |r| r.bike_id }.reduce([]) do |result, (id, rentals)|
rental = rentals.first
result << {
name: rental.bike.name,
model: rental.bike.model,
times_rented: rentals.count,
revenue: rental.bike.price * total_interval(rentals)
}
@jaimerson
jaimerson / a_bikes_report_approach.rb
Last active August 1, 2016 20:33
Bikes report test for tutorial
require 'rails_helper'
shared_examples 'a bikes report approach' do
subject(:approach) { described_class.new }
describe '#to_a' do
let(:cheap_bike) { create(:bike, price: 3.14) }
let(:expensive_bike) { create(:bike, price: 210) }
let!(:cheap_rentals) do
@jaimerson
jaimerson / .vimrc
Created October 8, 2014 00:21
Minimal vimrc
" Set vim-specific functions available, breaking compatibility with vi
set nocompatible
" Line Number
set number
" Syntax highlighting options
syntax on
set t_Co=256
" Search Options
set incsearch
set hlsearch
@jaimerson
jaimerson / .gitconfig
Created August 14, 2014 18:01
Git aliases
[alias]
# lazy commit
friday-night = !git commit -am 'Finish stuff' && git push origin `git symbolic-ref --short -q HEAD`
# delete merged branches
delete-merged = !git branch --merged | grep -v \* | xargs git branch -d