Skip to content

Instantly share code, notes, and snippets.

@Bahanix
Bahanix / ruby.rb
Last active August 15, 2024 11:27
Ruby syntax 101
# Everything after the '#' character is a comment
# Run these lines in an interactive ruby interpreter (irb) to "feel" them
# Maths
9/2 == 4
9/2.0 == 4.5 # Same as 9.0/2 or 9.0/2.0 or 9.to_f/2
2**8 == 256
@Bahanix
Bahanix / exercice.rb
Last active August 6, 2026 09:06
Ruby Operation pattern
class MyOperation
# Exo: remplir cette classe pour que la commande en bas de fichier retourne ce qu'elle est supposée retourner
end
class Test < MyOperation
step :set_result
step :display_result
private
scope :search_in_locale, -> (string, locale) {
where("id = ? OR (path ~ ?) OR UNACCENT(translations->?) ILIKE UNACCENT(?)", string.to_i, "*.#{Category.labelize(string)}*.*", locale, "%#{string}%")
}
scope :find_in_tree, -> (ids) {
where(
"path <@ (:labels) OR path @> (:labels)",
labels: Category.where(id: ids).select("ARRAY_AGG(path)")
)
}
@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, default: []
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 :
@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 / 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 / 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|