Skip to content

Instantly share code, notes, and snippets.

View jameslafa's full-sized avatar
🖤

James Lafa jameslafa

🖤
View GitHub Profile
@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 / application_flashes.html.erb
Created December 16, 2015 15:52
Bootstrap 4 alerts with rails 4.2
<% if flash.any? %>
<div class="row flashes">
<div class="col-md-8 col-md-offset-2">
<% flash.each do |msg_type, message| %>
<%= flash_message(msg_type, message) %>
<% end %>
</div>
</div>
<% end %>
@jameslafa
jameslafa / user.rb
Created December 9, 2015 16:09
Transfert enumerator accessors to related model
class User < ActiveRecord::Base
belongs_to :organization
Organization.categories.keys.each do |category|
define_method("#{category}?") do
organization.send("#{category}?")
end
end
end
@jameslafa
jameslafa / controller.rb
Created April 16, 2015 05:34
FactoryGirl attribute_for does not create associations for obvious performance reason. However, sometime it is useful to get all attributes with ids of association to test a POST#create request for example. To enable, just create a macro in your spec/support folder with the following content. Then call attributes_with_foreign_keys instead of att…
module ControllerMacros
def attributes_with_foreign_keys(*args)
FactoryGirl.build(*args).attributes.delete_if do |k, v|
["id", "type", "created_at", "updated_at"].member?(k)
end
end
end
RSpec.configure do |config|
config.include ControllerMacros, :type => :controller
@jameslafa
jameslafa / Gemfile
Created March 31, 2015 12:46
Useful gems
# Debugging in the browser
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
gem 'quiet_assets' # Remove useless logs
# Deploying
gem 'capistrano'
gem 'capistrano-rails'
gem 'capistrano-bundler' # Run bundle install while deploying
@jameslafa
jameslafa / Install_rails-erd.txt
Last active August 29, 2015 14:05
Install rails-erd to generate models diagrams
1. Install xquartz : https://xquartz.macosforge.org (no need to restart)
2. brew install cairo pango
3. brew install graphviz --with-pangocairo (it's important to install graphviz after cairo and pango)
4. bundle exec erd
@jameslafa
jameslafa / unit_test.coffee
Created June 11, 2014 13:03
AngularJS: serve different responses with $httpBackend inside the same test (auto retry for example)
# More details on https://blog.james-lafa.fr/angularjs-how-to-test-a-retry-after-http-error-with-httpbackend/
sessionCalls = 0
$httpBackend.when('POST', '/sessions').respond( (method, url, data, headers) ->
if sessionCalls == 0
sessionCalls++
return [200, {"response": 1}, {}]
else
sessionCalls++
return [200, {"response":0,"auth_token":"HK7i6DRncWdBmq"}, {}]
@jameslafa
jameslafa / test-directive.coffee
Created June 5, 2014 16:58
AngularJS - Unit testing a directive using promises
describe "UT: MyDirective", ->
$scope = undefined
$element = undefined
$q = undefined
html = '<div my-directive></div>'
# Compile the directive to generate the html code and get the scope
compileDirective = () ->
@jameslafa
jameslafa / _footer.html.erb
Created May 27, 2014 12:55
Rails 4 - Google analytics with Turbolinks
@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