Skip to content

Instantly share code, notes, and snippets.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
= simple_form_for(@account) do |f|
= f.error_notification
= f.input :type, collection:['iban', 'rib'], include_blank: false
#iban
.inputs
= f.input :iban, :include_blank => false
= f.input :bic, :include_blank => false
@Bahanix
Bahanix / git.sh
Last active August 29, 2015 14:20
Commandes de base pour git
### Configurer git et vim
git config --global core.editor "vim"
vim ~/.vimrc
syn on
set tabstop=2
set shiftwidth=2
set expandtab
match Todo /\s\+$/
@Bahanix
Bahanix / home.html.erb
Created April 25, 2016 14:27
OpenClassRooms Ruby on Rails logout
# app/views/users/home.html.erb
<% if @current_user %>
<h1>Bienvenue <%= @current_user.name %> !</h1>
<%= form_tag "/users/login", method: :delete do %>
<input type="submit" value="Déconnexion" />
<% end %>
<% else %>
<h1>Bienvenue visiteur anonyme !</h1>
<a href="/users/login">S'identifier</a>
<% end %>
@Bahanix
Bahanix / index.html.erb
Last active June 2, 2016 09:21
Ruby on Rails authorization exemple
# app/views/users/index.html.erb
<h1>Liste des utilisateurs</h1>
<ul>
<% @users.each do |user| %>
<li><%= user.id %></li>
<% end %>
</ul>
@Bahanix
Bahanix / add_locale_to_users.rb
Last active February 11, 2018 19:42
Ruby on Rails i18n: using user locale
# db/migrates/*_add_locale_to_users.rb
class AddLocaleToUsers < ActiveRecord::Migration
def change
add_column :users, :locale, :string, default: "fr"
end
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_locale
private
def set_locale
if session[:locale]
I18n.locale = session[:locale]
@Bahanix
Bahanix / n_plus_one.rb
Last active March 22, 2017 12:06
N+1 queries example
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
end
# It will hit 11 times your database, that's bad!
User.limit(10).each do |user|
@Bahanix
Bahanix / scoped_n_plus_one.rb
Last active March 25, 2017 22:26
Avoid N+1 queries when scopes are involved
class User < ActiveRecord::Base
has_many :posts
has_many :published_posts, -> { published }, class_name: "Post"
end
class Post < ActiveRecord::Base
belongs_to :user
scope :published, -> { where(status: :published) }
end
@Bahanix
Bahanix / composed_preloading.rb
Last active March 25, 2017 22:27
Avoid N+1 queries with compose option
class User < ActiveRecord::Base
# Only this class changed
has_many :posts, compose: :published
end
class Post < ActiveRecord::Base
belongs_to :user
scope :published, -> { where(status: :published) }
end
@Bahanix
Bahanix / sortable.rb
Created September 27, 2017 16:29
JSONAPI
# app/models/concerns/sortable.rb
module Sortable
extend ActiveSupport::Concern
included do
# Model.smart_sort("attribute_a,+attribute_b,-attribute_c")
# is equivalent to
# Model.order(attribute_a).order(attribute_b: :asc).order(attribute_c: :desc)
scope :smart_sort, -> (sort) {
scope = self.all