Skip to content

Instantly share code, notes, and snippets.

View jameslafa's full-sized avatar
🖤

James Lafa jameslafa

🖤
View GitHub Profile
@jameslafa
jameslafa / matchers.rb
Created August 16, 2016 09:21
RSpec::Matchers have_same_attributes_as
RSpec::Matchers.define :have_same_attributes_as do |expected|
ignored_attributes = ['id', 'updated_at', 'created_at']
match do |actual|
actual.attributes.except(*ignored_attributes) == expected.attributes.except(*ignored_attributes)
end
chain :except do |*attributes|
ignored_attributes += attributes.map {|a| a.to_s}
end
@jameslafa
jameslafa / to_boolean.rb
Created April 26, 2016 14:34
Rails: convert anything to boolean
# config/initializers/to_boolean.rb
module ToBoolean
def to_bool
return true if ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(self)
return false
end
end
class NilClass; include ToBoolean; end
@jameslafa
jameslafa / my_controller_spec.rb
Created July 17, 2016 10:50
Expect Job being enqueued with args
RSpec.describe MyController, type: :controller do
include ActiveJob::TestHelper
describe "..." do
it "enqueues the job with the right arguments" do
expect {
@bookmark = post :create, {:bookmark => valid_attributes}
}.to have_enqueued_job(SlackNotifierJob).with { |args|
expect(args).to eq(["new_bookmark", @bookmark])
}
@jameslafa
jameslafa / _footer.html.erb
Created May 27, 2014 12:55
Rails 4 - Google analytics with Turbolinks
@jameslafa
jameslafa / test_spec.rb
Created January 30, 2016 08:09
Include Money-rails Helper in the feature tests
context 'when changing the selected materials', :js => true, focus: true do
include MoneyRails::ActionViewExtension
it 'updates the offers' do
# bla bla
end
end
@jameslafa
jameslafa / database_cleaner.rb
Created January 30, 2016 08:07
Database cleaner with Rspec and a javascript engive
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
@jameslafa
jameslafa / mixpanel.coffee
Last active December 21, 2015 06:38
AngularJS MixPanel mock for testing
/*
* author: @jameslafa
* More details on http://blog.james-lafa.fr/angularjs-how-to-mock-mixpanel-inside-for-your-tests/
*/
class MixpanelMock
debug = false
log: (event, data) ->
if @debug
@jameslafa
jameslafa / active_admin.rb
Created July 29, 2013 13:59 — forked from fred/active_admin.rb
Show pretty booleans in active_admin
# It extends activeadmin to show pretty boolean values
#
# config/initializers/active_admin.rb
module ActiveAdmin
module Views
class TableFor
def bool_column(attribute)
column(attribute){ |model| model[attribute] ? '✔'.html_safe : '✗'.html_safe }
end
@jameslafa
jameslafa / global_macros.rb
Created July 26, 2013 08:37
Counting elements using capybara and rspec
RSpec::Matchers.define :match_exactly do |expected_match_count, selector|
match do |context|
matching = context.all(selector)
@matched = matching.size
@matched == expected_match_count
end
failure_message_for_should do
"expected '#{selector}' to match exactly #{expected_match_count} elements, but matched #{@matched}"
end
@jameslafa
jameslafa / bash.sh
Created July 10, 2013 19:21
Rebuild schema.rb from scratch using every migrations
# Rebuild database from schema.rb
rake db:reset
# But this rebuild database from all migrations
rake db:drop db:create db:migrate db:seed