Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍🔬

Luján Fernaud lujanfernaud

👨‍🔬
View GitHub Profile
View ruby_validate_url.md

Ruby: Validate URL

We can match against %r{^(http|https)://[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(/.*)?$}ix.

Non Rails version:

  VALID_URL_REGEXP = %r{^(http|https)://[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(/.*)?$}ix.freeze

  def valid_url?(url)
View ruby_validate_email.md

Ruby: Validate Email

We can match against URI::MailTo::EMAIL_REGEXP.

  def valid_email?(email)
    email && email.match?(URI::MailTo::EMAIL_REGEXP)
  end
@lujanfernaud
lujanfernaud / back_pain_stretches_with_stuart_mcgill.md
Last active Jan 11, 2021
Back Pain: Stretches with Stuart McGill
View back_pain_stretches_with_stuart_mcgill.md

Back Pain: Stretches with Stuart McGill

The Cat Camel

Get on all fours and slowly move back and forth from a downward spinal curve with the head looking up like a cat and then move into a rounded spine while the head looks down like a camel. Each cycle should take about three to four seconds. 7-8 cycles are all that is needed.

Gist: Only do 7-8 cycles. More than that could be bad.

Video: https://www.youtube.com/watch?t=96&v=bcbuhePZZj0

View rolify_remove_all_roles_for_user_in_resource.md

Rolify: Remove All Roles For User In a Specific Resource

We find all roles assigned to the user in the resource, a group in this case.

@user.roles.where(resource: @group)
=> [#<Role:0x00000004a80580
  id: 24,
@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 / log_interactor_execution.rb
Last active Aug 7, 2020
Debugging: LogInteractorExecution
View log_interactor_execution.rb
# Usage:
#
# class SomeInteractor
# include Interactor
# include LogInteractorExecution
#
# # ...
#
# private
#
@lujanfernaud
lujanfernaud / sidekiq_clear_specific_queue.md
Created Jul 27, 2020
Sidekiq: Clear a Specific Queue
View sidekiq_clear_specific_queue.md

Sidekiq: Clear a Specific Queue

Find the queue by name:

> queue = Sidekiq::Queue.all.detect { |queue| queue.name == 'compliance_requests' }
=> #<Sidekiq::Queue:0x00007fa5c4069778 @name="compliance_requests", @rname="queue:compliance_requests">

Clear the queue:

View rspec_add_metadata_based_on_path.md

RSpec: Add metadata based on path

RSpec.configure do |config|
  config.define_derived_metadata(file_path: %r{platforms/.*/spec/controllers/}) do |metadata|
    metadata[:type] = :controller
    metadata[:platform_controller] = true
  end

  config.include MainAppRoutesHelper, platform_controller: true
@lujanfernaud
lujanfernaud / rspec_include_based_on_type.md
Last active May 8, 2020
RSpec: Include based on type
View rspec_include_based_on_type.md

RSpec: Include based on type

# spec/support/user_support.rb
RSpec.configure do |config|
 config.include UserHelper, type: :user
 config.include_context "user_context", type: :use
end