Skip to content

Instantly share code, notes, and snippets.

View lucashungaro's full-sized avatar

Lucas Húngaro lucashungaro

View GitHub Profile
@lucashungaro
lucashungaro / form_helper.rb
Last active January 27, 2017 10:14
Triggering an autocomplete select box with Capybara and Turnip
# on spec/helpers
module FormHelper
def fill_in_autocomplete(selector, value)
page.execute_script %Q{$('#{selector}').focus().val('#{value}').keydown()}
end
def choose_autocomplete(text)
find('ul.ui-autocomplete').should have_content(text)
page.execute_script("$('.ui-menu-item:contains(\"#{text}\")').find('a').trigger('mouseenter').click()")
end

"Why You Don't Get Mock Objects" by Gregory Moeck

RubyConf 2011 | 2011-09-29 | Gregory Moeck (@gregmoeck) | Slides

  • Recommended as the best book on mocks: Growing Object-Oriented Software, Guided by Tests by Steve Freeman & Nat Pryce
  • Common arguments against mocks
    • They duplicate implementation
    • They lead to brittle tests
  • Mock objects + procedural programming = bad idea
  • If you're doing traditional Rails development (which tends to follow more of a "procedural", do-this-and-then-do-that style), mock objects probably aren't for you
@lucashungaro
lucashungaro / gist:02c40b4d618b93f3e44971889e7c33f3
Created April 8, 2016 18:04
Fix Homebrew issues after upgrading to El Capitan (errors such as /usr/local is not writable or cannot load such file --mach)
sudo chown -R $(whoami):admin /usr/local
cd /usr/local
git reset --hard
git clean -df
brew update
Profit!
@lucashungaro
lucashungaro / gist:2690990
Created May 14, 2012 00:51
SRP snippet 3
class Game < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :category_id, :description,
:price, :platform, :year
def price
price = read_attribute(:price)
unless price.present?
update_attribute(:price, GamePriceService.new(self).get_price)
read_attribute(:price)
@lucashungaro
lucashungaro / gist:742790
Created December 15, 2010 23:43
Split MKV files
To install mkvtoolnix:
$ brew install mkvtoolnix
To split a file:
$ mkvmerge --split size:4050m /path/to/file.mkv -o /path/to/output_file.mkv
@lucashungaro
lucashungaro / gist:3784008
Created September 25, 2012 19:46
removing conditional and working with provided callbacks
class UserController < ApplicationController
def create
@user_creation = UserCreation.new(self)
@user_creation.on_success { redirect_to users_path }
@user_creation.on_failure { render :new }
@user_creation.please_create_an_user_as_admin(params[:user])
end
end
@lucashungaro
lucashungaro / gist:3001641
Created June 27, 2012 05:16
DIP snippet 4
# WARNING: pseudo code
class Authenticator
def initialize(user_repository = User)
@user_repository = user_repository
end
def authenticate(identifier, hashed_password)
@user_repository.find(:username => identifier, :password => hashed_password).present?
end
@lucashungaro
lucashungaro / gist:3001511
Created June 27, 2012 04:44
DIP snippet 3
class Game < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :category_id, :description,
:price, :platform, :year
end
 
class GamePriceService
attr_accessor :game
 
# we could use a config file
@lucashungaro
lucashungaro / gist:3001505
Created June 27, 2012 04:42
DIP snippet 2
class Game < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :category_id, :description,
:price, :platform, :year
end
 
class GamePriceService
attr_accessor :game, :json_parser
 
# we could use a config file
@lucashungaro
lucashungaro / gist:3001503
Created June 27, 2012 04:40
DIP snippet 1
class Game < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :category_id, :description,
:price, :platform, :year
end
 
class GamePriceService
attr_accessor :game
 
# we could use a config file