Skip to content

Instantly share code, notes, and snippets.

View lujanfernaud's full-sized avatar
🦪
Working from home

Luján Fernaud lujanfernaud

🦪
Working from home
View GitHub Profile
@lujanfernaud
lujanfernaud / javascript_utilities.md
Last active August 18, 2018 08:02
JavaScript Utilities

JavaScript Utilities

Note: Using StandardJS rules.

Range

function * range(start, end, step = 1) {
  while (start <= end) {
 yield start
@lujanfernaud
lujanfernaud / strict_queries.rb
Last active August 10, 2018 07:28
Rails: Strict Queries
# frozen_string_literal: true
# Alerts of SQL queries made within views.
#
# https://www.driftingruby.com/episodes/improving-partial-loading-performance
module StrictQueries
class SQLWithViewError < StandardError; end
module Concern
extend ActiveSupport::Concern
@lujanfernaud
lujanfernaud / sublime_text_accented_characters.md
Last active July 30, 2018 11:20
Sublime Text: Add Missing Accented Characters (á, å, ä)

Sublime Text: Add Missing Accented Characters (á, å, ä)

Put inside user's key bindings.

[
  // Missing accented characters fix.

  // a, [á, å, ä]
  { "keys": ["´","a"], "command": "insert", "args": {"characters": "á"}},
@lujanfernaud
lujanfernaud / rails_simple_decorator.rb
Created July 26, 2018 08:55
Rails: Simple Decorator
# frozen_string_literal: true
class ObjectNameDecorator < SimpleDelegator
delegate :class, :is_a?, to: :__getobj__
def self.collection(objects)
objects.map { |group| ObjectNameDecorator.new(object) }
end
private
@lujanfernaud
lujanfernaud / rails_default_timestamp_for_datetime_column.md
Created July 23, 2018 07:49
Rails: Setting a Default Timestamp for a Datetime Column

Rails: Setting a Default Timestamp for a Datetime Column

In Rails 5 we can set a default timestamp for a datetime column using a lambda with 'CURRENT_TIMESTAMP'.

default: -> { 'CURRENT_TIMESTAMP' }

Creating a table:

@lujanfernaud
lujanfernaud / minitest_and_database_cleaner.md
Last active December 28, 2023 17:37
Minitest and Database Cleaner

Minitest and Database Cleaner

test_helper.rb

require 'database_cleaner'
require 'database_cleaner_support'

DatabaseCleaner.clean_with :truncation
DatabaseCleaner.strategy = :transaction
@lujanfernaud
lujanfernaud / rails_update_comments_count_for_all_topics_in_a_group.md
Created July 17, 2018 07:44
Rails: Update Comments Count for All Topics in a Group

Rails: Update Comments Count for All Topics in a Group

def update_topics_comments_count
  begin
    ActiveRecord::Base.connection.execute <<~SQL
      UPDATE topics
         SET comments_count = (SELECT count(1)
                                 FROM topic_comments
 WHERE topic_comments.topic_id = topics.id
@lujanfernaud
lujanfernaud / rails_dynamic_path_inside_partial.md
Last active April 5, 2022 11:54
Rails: Dynamic Path Inside Partial

Rails: Dynamic Path Inside Partial

We have a _user partial that we want to render from two different resources, passing a collection. The only thing that needs to change in the partial is the path inside the link_to helper.

We can use self.send("path", object) inside link_to to achieve this.

groups/_user.html.erb

@lujanfernaud
lujanfernaud / rails_order_by_priority_and_date_with_limit_for_category.md
Created June 20, 2018 07:15
Rails: Order by Priority and Date with Limit for Category

Rails: Order by Priority and Date with Limit for Category

# group.rb

# When we pass NULL to LIMIT, Postgres treats it as LIMIT ALL (no limit).
# https://www.postgresql.org/docs/current/static/sql-select.html#SQL-LIMIT
def topics_prioritized(normal_topics_limit: nil)
  ids = topic_ids(normal_topics_limit)
@lujanfernaud
lujanfernaud / rails_random_date_in_period.md
Last active June 19, 2018 16:55
Rails: Random Date in Period

Rails: Random Date in Period

def random_date_in_period(period)
  rand(period).seconds.ago
end

random_date_in_period 1.year #=> Tue, 22 May 2018 04:55:54 UTC +00:00
random_date_in_period 1.year #=> Mon, 06 Nov 2017 19:50:07 UTC +00:00