Skip to content

Instantly share code, notes, and snippets.

Avatar
🦪
Working from home

Luján Fernaud lujanfernaud

🦪
Working from home
View GitHub Profile
@lujanfernaud
lujanfernaud / rails_dynamic_path_inside_partial.md
Last active Apr 5, 2022
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 / html_spellcheck_feature.md
Last active Oct 12, 2021
HTML: Spellcheck Feature
View html_spellcheck_feature.md
@lujanfernaud
lujanfernaud / ruby_immutable_value_object_with_struct.md
Last active Jul 16, 2021
Ruby: Immutable Value Object with Struct
View ruby_immutable_value_object_with_struct.md

Ruby: Immutable Value Object with Struct

Freezing the initializer after passing the args to super makes the object immutable.

# frozen_string_literal: true

# Freezing the initializer after passing the args to `super` makes the object immutable.
#
# Example:
@lujanfernaud
lujanfernaud / heroku_reduce_slug_size_with_slugignore.md
Created May 12, 2021
Heroku: Reduce Slug Size With .slugignore
View heroku_reduce_slug_size_with_slugignore.md

Heroku: Reduce Slug Size With .slugignore

Adding a .slugignore file to the root folder of the project will make Heroku to remove the files and folders referenced after we push code to Heroku and the buildpack runs.

What is the Slug?

A compressed copy of the application comprising all files in the git repo along with packages installed during deployment.

Sources

View rails_format_names.md
@lujanfernaud
lujanfernaud / rails_permitting_arrays_in_strong_parameters.md
Created May 5, 2021
Rails: Permitting Arrays in Strong Parameters
View rails_permitting_arrays_in_strong_parameters.md

Rails: Permitting Arrays in Strong Parameters

We can do so by adding an array value to the param:

params.permit(:email, :name, sites: [])

We can even use nested arrays:

@lujanfernaud
lujanfernaud / rails_datetime_select_capybara.md
Last active Apr 20, 2021
Rails: Test datetime_select using Capybara
View rails_datetime_select_capybara.md

Rails: Test datetime_select using Capybara

# test_helper.rb

def select_date_and_time(date, **options)
  return nil unless date
  
 field = options[:from]
@lujanfernaud
lujanfernaud / ruby_require_all_files_within_a_directory.md
Created Mar 19, 2021
Ruby: Require All Files Within a Directory
View ruby_require_all_files_within_a_directory.md

Ruby: Require All Files Within a Directory

Dir[File.expand_path('../config/routes/*.rb', __dir__)].each do |file|
  require_relative file
end
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