Skip to content

Instantly share code, notes, and snippets.

@Bahanix
Bahanix / osx.sh
Created April 8, 2022 08:17
Ruby syntax 101
xcode-select --install
brew update
brew install git
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
source ~/.bash_profile
@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 / post-checkout
Created June 4, 2018 11:14
post-checkout hook to display if you had stashed something on this branch
#!/bin/sh
STASH_NAME="$(git stash list | grep `git branch | grep \* | cut -d ' ' -f2` | head -n1 | cut -d':' -f1)"
if [ -n "$STASH_NAME" ]
then
echo "Last stash on this branch:"
git --no-pager stash show "$STASH_NAME" -p
echo "To keep your stash list clean, consider using one of the followings:"
echo "git stash pop \"$STASH_NAME\""
echo "git stash drop \"$STASH_NAME\""
# Simple :
# params[:user][:tags] = ["tag1", "tag2"]
user.update!(user_params)
# Ou encore :
user.tags = ["tag1", "tag2"]
user.save!
# Beau
def user_params
add_column :users, :tags, :string, array: true, null: false, default: []
add_index :users, :tags, using: :gin
# Ceci ne fonctionnera pas :
User.where("users.tags @> ?", requested_tags)
# Faites ceci, même si requested_tags n'a qu'un item ou n'est pas un array.
User.where("users.tags @> ARRAY[?]", requested_tags)
# Si vous rencontrez cette erreur, faites ceci :
add_column :users, :tags, :string, array: true, default: []
@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
@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
@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 / 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