Skip to content

Instantly share code, notes, and snippets.

View MiguelArgentina's full-sized avatar

Miguel Ricardo Gomez MiguelArgentina

View GitHub Profile
@MiguelArgentina
MiguelArgentina / csv_file_handler.rb
Created April 26, 2024 19:26
Rails CSV file handler
require "csv"
class CsvFileHandler
# Reads specific columns or all columns from a CSV file.
# column_indices is an array of integers representing the column numbers to read.
# If column_indices is empty or not provided, all columns are read.
BATCH_SIZE = 1000
def self.read_from_csv(file_path, column_indices = [], separator = ",")
data = []
CSV.foreach(file_path, headers: true, header_converters: :symbol, col_sep: separator) do |row|

Lock the DB for updating info:

ActiveRecord::Base.transaction do
  ActiveRecord::Base.connection.execute('LOCK scheduling_informations IN ACCESS EXCLUSIVE MODE')
  SchedulingInformation.delete_all
end
@MiguelArgentina
MiguelArgentina / dry_controller.rb
Last active November 6, 2023 10:45
Rails DRY controller using Concern
# This module must be included in controllers
# to get the functionality of the methods.
# Remember to call `super` in overridden methods.
# In views, the object `@resource` is available.
module DryController
extend ActiveSupport::Concern
included do
before_action :set_resource_class
before_action :set_index, only: :index
@MiguelArgentina
MiguelArgentina / _useful_annotations.md
Last active September 4, 2023 18:12
Annotations that can be handy on a daily basis

Heroku related

Heroku log tails:

  • heroku logs --app app-name --tail
  • heroku logs --app app-name -n 1500 (for the last 1500 lines)
  • heroku logs --app app-name --tail --ps worker (worker logs)

Heroku DB dump:

  • heroku pg:backups:capture -a app-name (create the dump)
  • heroku pg:backups:download -a app-name (download the dump)

Cheat Sheets are greate but they are not a substitute for learning the framework and reading the documentation as we most certainly have not covered every potential example here. Please refer to the Rails Command Line Docs for more information.

Command Line Generator Info

Reference

You can get all of this information on the command line.

rails generate with no generator name will output a list of all available generators and some information about global options. rails generate GENERATOR --help will list the options that can be passed to the specified generator.

@MiguelArgentina
MiguelArgentina / alias_matchers.md
Created February 23, 2021 17:22 — forked from JunichiIto/alias_matchers.md
List of alias matchers in RSpec 3

This list is based on aliases_spec.rb.

You can see also Module: RSpec::Matchers API.

matcher aliased to description
a_truthy_value be_truthy a truthy value
a_falsey_value be_falsey a falsey value
be_falsy be_falsey be falsy
a_falsy_value be_falsey a falsy value