Skip to content

Instantly share code, notes, and snippets.

Julien Bahanix

  • France
Block or report user

Report or block Bahanix

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Bahanix
Bahanix / post-checkout
Created Jun 4, 2018
post-checkout hook to display if you had stashed something on this branch
View post-checkout
#!/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\""
View psql_array_strong_parameters.rb
# Simple :
# params[:user][:tags] = ["tag1", "tag2"]
user.update!(user_params)
# Ou encore :
user.tags = ["tag1", "tag2"]
user.save!
# Beau
def user_params
View array_migration.rb
add_column :users, :tags, :string, array: true, default: []
View psql_array_query.rb
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 :
View sortable.rb
# 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 Mar 25, 2017
Avoid N+1 queries with compose option
View composed_preloading.rb
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 Mar 25, 2017
Avoid N+1 queries when scopes are involved
View scoped_n_plus_one.rb
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 Mar 22, 2017
N+1 queries example
View n_plus_one.rb
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|
View application_controller.rb
# 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 / add_locale_to_users.rb
Last active Feb 11, 2018
Ruby on Rails i18n: using user locale
View add_locale_to_users.rb
# db/migrates/*_add_locale_to_users.rb
class AddLocaleToUsers < ActiveRecord::Migration
def change
add_column :users, :locale, :string, default: "fr"
end
end
You can’t perform that action at this time.