Skip to content

Instantly share code, notes, and snippets.

View StanBoyet's full-sized avatar

Stanislas Boyet StanBoyet

View GitHub Profile
# RSpec's subject method, both implicitly and explicitly set, is useful for
# declaratively setting up the context of the object under test. If you provide a
# class for your describe block, subject will implicitly be set to a new instance
# of this class (with no arguments passed to the constructor). If you want
# something more complex done, such as setting arguments, you can use the
# explicit subject setter, which takes a block.
describe Person do
context "born 19 years ago" do
subject { Person.new(:birthdate => 19.years.ago }
it { should be_eligible_to_vote }
@StanBoyet
StanBoyet / generation.rb
Created February 23, 2014 11:40
Generate form, helper etc for model
require 'rubygems'
require 'active_support/core_ext'
schema = File.read('db/schema.rb')
schema.scan(/create_table "(\w+)",.*?\n(.*?)\n end/m).each do |name, ddl|
puts "rails generate scaffold #{name.classify} " +
ddl.scan(/t\.(\w+)\s+"(\w+)"/).
reject {|type,name| %w(created_at updated_at).include? name}.
map {|type,name| "#{name}:#{type}"}.join(' ')
end
@StanBoyet
StanBoyet / nil_view.rb
Created March 20, 2014 16:34
Deal with nil variables in view
<% if @messages.each do |message| %>
<%# code or partial to display the message %>
<% end.empty? %>
You have no messages.
<% end %>
@StanBoyet
StanBoyet / heroku-test.sh
Created May 10, 2014 13:33
Precompile assets for heroku deployment
#!/bin/bash
echo 'Script start'
read -p "Precompile & Push on Test ? (y/n)?"
[ "$REPLY" == "y" ] || RAILS_ENV=production bundle exec rake assets:precompile; git add public/assets; git commit -m "vendor compiled assets"; git push heroku-test master
@StanBoyet
StanBoyet / erb2slim_convert_all_views
Last active August 29, 2015 14:05
Using erb2slim for *.erb in your view folder
gem install 'html2slim'
for file in app/views/**/*.erb; do erb2slim $file ${file%erb}slim && rm $file; done
@StanBoyet
StanBoyet / modal.slim.html
Created September 8, 2014 09:46
Boostrap Modal in Slim
/ Simple Slim translation of Bootstrap modal
.modal.fade.bs-example-modal-lg id="myModal" tabindex='-1' role='dialog' aria-labelledby="Modal Title" aria-hidden='true'
.modal-dialog.modal-lg
.modal-content
.modal-header
button.close type='button' data-dismiss='modal'
span aria-hidden='true' &times;
span.sr-only Close
h4.modal-title = "Modal Title"
@StanBoyet
StanBoyet / user_related_policy.rb
Created September 17, 2014 14:41
User related typical policy
# iTerm2, OS X
# 1. change 'your_app_production' to your application name
# 1a. Tune the colors by your taste
# 2. put these functions to your .bashrc, .zshrc
# or anywhere where it gets loaded for your iTerm session
# 3. restart iTerm or 'source ~/.zshrc' and use these functions
set_color() {
local HEX_FG=$1
local HEX_BG=$2
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
fr:
devise:
confirmations:
confirmed: 'Votre compte a été validé.'
send_instructions: 'Vous allez recevoir les instructions nécessaires à la confirmation de votre compte dans quelques minutes.'
send_paranoid_instructions: 'Si votre e-mail existe dans notre base de données, vous allez bientôt recevoir un e-mail contenant les instructions de confirmation de votre compte.'
failure:
already_authenticated: "Vous êtes déjà connecté"
@StanBoyet
StanBoyet / sort_with_jquery.md
Created November 18, 2014 18:37
Sort elements with jQuery

from http://trentrichardson.com/2013/12/16/sort-dom-elements-jquery/

I wanted to share a quick snippet that I worked up for a unique scenario. Occasionally you want to sort or display DOM elements in a certain order. For instance, what if you have a list of names. A feature of your site may be that you can sort the list by first name, or last name. There are a few ways you can approach this.

One approach may be using a javascript framework like Backbone or Angular, where you have a model and collection. Then simply sort the collection, magic happens, and your list is essentially regenerated.

The other could be that you’re not using a framework and not want to regenerate the entire list, but to move the nodes. It is actually rather simple. Lets say you have a list like the following: