Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍🔬

Luján Fernaud lujanfernaud

👨‍🔬
View GitHub Profile
@lujanfernaud
lujanfernaud / rails_order_by_priority_and_date_with_limit_for_category.md
Created Jun 20, 2018
Rails: Order by Priority and Date with Limit for Category
View rails_order_by_priority_and_date_with_limit_for_category.md

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_dynamic_path_inside_partial.md
Last active Jul 2, 2018
Rails: Dynamic Path Inside Partial
View rails_dynamic_path_inside_partial.md

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_update_comments_count_for_all_topics_in_a_group.md
Created Jul 17, 2018
Rails: Update Comments Count for All Topics in a Group
View rails_update_comments_count_for_all_topics_in_a_group.md

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 / minitest_and_database_cleaner.md
Last active Oct 20, 2020
Minitest and Database Cleaner
View minitest_and_database_cleaner.md

Minitest and Database Cleaner

test_helper.rb

require 'database_cleaner'
require 'database_cleaner_support'

DatabaseCleaner.clean_with :truncation
DatabaseCleaner.strategy = :transaction
@lujanfernaud
lujanfernaud / rails_default_timestamp_for_datetime_column.md
Created Jul 23, 2018
Rails: Setting a Default Timestamp for a Datetime Column
View rails_default_timestamp_for_datetime_column.md

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:

View rails_simple_decorator.rb
# 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 / sublime_text_accented_characters.md
Last active Jul 30, 2018
Sublime Text: Add Missing Accented Characters (á, å, ä)
View sublime_text_accented_characters.md

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 / strict_queries.rb
Last active Aug 10, 2018
Rails: Strict Queries
View strict_queries.rb
# 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
View javascript_utilities.md

JavaScript Utilities

Note: Using StandardJS rules.

Range

function * range(start, end, step = 1) {
  while (start <= end) {
    yield start
View git_un-add_file_from_last_commit.md