Skip to content

Instantly share code, notes, and snippets.

Avatar
🖤

James Lafa jameslafa

🖤
View GitHub Profile
@jameslafa
jameslafa / spec_helper.rb
Last active Aug 29, 2015
Rspec + capybara configuration
View spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/poltergeist'
require 'database_cleaner'
# Requires supporting ruby files with custom matchers and macros, etc,
@jameslafa
jameslafa / test-directive.coffee
Created Jun 5, 2014
AngularJS - Unit testing a directive using promises
View test-directive.coffee
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 / unit_test.coffee
Created Jun 11, 2014
AngularJS: serve different responses with $httpBackend inside the same test (auto retry for example)
View unit_test.coffee
# 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 / Install_rails-erd.txt
Last active Aug 29, 2015
Install rails-erd to generate models diagrams
View Install_rails-erd.txt
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
View Gemfile
# 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 / controller.rb
Created Apr 16, 2015
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…
View controller.rb
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 / user.rb
Created Dec 9, 2015
Transfert enumerator accessors to related model
View user.rb
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 / application_flashes.html.erb
Created Dec 16, 2015
Bootstrap 4 alerts with rails 4.2
View application_flashes.html.erb
<% 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 / application.rb
Created Jun 13, 2013
Use asset piple with SVG
View application.rb
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.svg)
@jameslafa
jameslafa / Gemfile
Created Jul 9, 2013
ActiveAdmin : Make model compliant with ActiveModel and StrongParameters Thanks to @daxter (twitter @seanlinsley) for his help : https://github.com/gregbell/active_admin/pull/2326
View Gemfile
# Rails 4.0 just went out and right now to make ActiveAdmin works, you need
# to add this in your Gemfile
gem 'devise', github: 'plataformatec/devise'
gem 'responders', github: 'plataformatec/responders'
gem 'inherited_resources', github: 'josevalim/inherited_resources'
gem 'ransack', github: 'ernie/ransack', branch: 'rails-4'
gem 'activeadmin', github: 'gregbell/active_admin', branch: 'rails4'
gem 'formtastic', github: 'justinfrench/formtastic'