Skip to content

Instantly share code, notes, and snippets.

View brennovich's full-sized avatar
👋
Hey, there

Brenno Costa brennovich

👋
Hey, there
View GitHub Profile
@brennovich
brennovich / assets.rake
Created October 20, 2015 13:41 — forked from jparker/assets.rake
Rake task for compiling assets and uploading them to S3
# RAILS_ROOT/lib/tasks/assets.rake
namespace :assets do
desc 'Precompile assets and upload to S3'
task :upload, [:noop] => ['assets:clean', 'assets:precompile'] do |_, args|
args.with_defaults(noop: false)
Fog.credentials_path = "#{Rails.root}/config/fog_credentials.yml"
Dir.chdir("#{Rails.root}/public") do
assets = FileList['assets',"assets/**/*"].inject({}) do |hsh, path|
@brennovich
brennovich / example.rb
Created September 17, 2015 16:58
Quick and simple example of how output active menu class for Rails applications
# Example:
#
# Let's say we're are accessing a new form to create a Post, probably
# our page will be rendered by PostsController and new action:
class PostsController < ApplicationController
def new
@post = Post.new
render :new
end
@brennovich
brennovich / tracepoint.rb
Last active August 29, 2015 14:26
Snipet used by Avid in RubyTapas #327
require "rake"
include FileUtils
indent = 0
trace = TracePoint.new(:call, :c_call, :return, :c_return) do |tp|
if [:return, :c_return].include?(tp.event) && indent.nonzero?
indent -= 1
else
puts "#{' ' * indent}#{tp.inspect}"
@brennovich
brennovich / Gemfile
Last active August 29, 2015 14:26 — forked from teamon/Gemfile
Mount Grape API at rails root
source "http://rubygems.org"
gem "railties", '4.0.3', require: %w(action_controller rails)
gem "grape"
# config/initializers/instrumentation.rb
# Subscribe to grape request and log with Rails.logger
ActiveSupport::Notifications.subscribe('grape.request') do |name, starts, ends, notification_id, payload|
Rails.logger.info '[API] %s %s (%.3f ms) -> %s %s%s' % [
payload[:request_method],
payload[:request_path],
(ends-starts)*1000,
(payload[:response_status] || "error"),
payload[:x_organization] ? "| X-Org: #{payload[:x_organization]}" : "",

I was reading Practical Objected-Oriented Design With Ruby other day and Sandi Metz came up with a good and reasonable approach of how to deal with Roles (by role I mean Concerns and overall duck typing stuff) specs.

  • The Role itself should be tested with a test's built-in player. Ex.:
let(:mergeable_class) do
  Class.new do
    include Mergeable

 # ...
@brennovich
brennovich / benchmark.rb
Last active August 29, 2015 14:22
each_with_object_each vs tap+each vs tap+update
require 'benchmark/ips'
Benchmark.ips do |x|
x.report 'tap' do
{}.tap { |results| each { |bucket| bucket.ids_by_type.each{ |type,ids| (results[type]||=[]); results[type] += ids } } }
end
x.report 'each_with_object' do
flat_map(&:ids_by_type).each_with_object({}) do |results, ids_by_type|
<li class="list-group-item">
<a href="#">
<div class="company-logo company-logo-small">
<img src="https://ucarecdn.com/236d0f6e-77f6-4b56-8c48-c09ba303e0a5/-/resize/100x100/.jpg">
</div>
<div class="company-info-search">
<h5 class="h4">Companhia Paulista de Trens Metropolitanos - CPTM</h5>
<div class="static-rating-wrapper">
<div class="static-rating-container">
require 'benchmark/ips'
class Cocada
attr_accessor :string
def mocked_size
50
end
end
require 'benchmark/ips'
class String
def mocked_size
50
end
end
Benchmark.ips do |x|
x.time = 10