Skip to content

Instantly share code, notes, and snippets.

👨‍🔬

Luján Fernaud lujanfernaud

👨‍🔬
View GitHub Profile
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
View rails_tables_finder.rb
# Example:
#
# > TablesFinder.call(column_name: 'site_id')
# => ['events', 'users']
class TablesFinder
VALID_ATTRIBUTES = %w[
COLUMN_NAME
].freeze
def self.call(args)
@lujanfernaud
lujanfernaud / rake_tasks_helper.rb
Last active Mar 27, 2020
RSpec: Rake Tasks Helper
View rake_tasks_helper.rb
# Example usage =============
#
# describe :namespace do
# include RakeTasksHelper
#
# describe ':task' do
# it 'works' do
# invoke_task('namespace:task')
# end
# end
@lujanfernaud
lujanfernaud / seeds-faker-carrierwave.rb
Created Sep 7, 2017
Rails: Seed database with remote Faker images and CarrierWave
View seeds-faker-carrierwave.rb
# We need to use 'remote_<object>_url' as attribute instead of '<object>'.
# Having Image, we would use 'remote_image_url: <url>'.
titles = [Faker::RockBand.name, Faker::BossaNova.artist, Faker::Book.title]
54.times do |n|
start_date = Faker::Date.between(1.day.from_now, 6.months.from_now)
end_date = start_date + 1.day
Event.create!(title: titles.sample + " ##{n}",
@lujanfernaud
lujanfernaud / rubocop_autocorrect_specific_cops.md
Last active Feb 27, 2020
RuboCop: Autocorrect Using Specific Cops
View rubocop_autocorrect_specific_cops.md

RuboCop: Autocorrect Using Specific Cops

We can autocorrect using only some cops by passing --only CopType/CopName.

rubocop --only Style/HashTransformKeys

First run with --safe-auto-correct to see the affected changes:

View rails_url_helpers.md

Rails: URL Helpers

Access path helpers from outside controllers.

Rails.application.routes.url_helpers
Rails.application.routes.url_helpers.dashboard_path
Rails.application.routes.url_helpers.users_path
Rails.application.routes.url_helpers.pages_path
@lujanfernaud
lujanfernaud / rspec_only_failures.md
Last active Jan 28, 2020
RSpec: Run Only Failures
View rspec_only_failures.md

RSpec: Run Only Failures

In spec/spec_helper.rb:

RSpec.configure do |config|
  # This setting allows us to only re-run examples that had failures.
  # https://relishapp.com/rspec/rspec-core/docs/command-line/only-failures
  #
  # Usage:
@lujanfernaud
lujanfernaud / rofi_alt_tab_window_switching.md
Last active Dec 27, 2019
Rofi: Alt+Tab Window Switching
View rofi_alt_tab_window_switching.md

Rofi: Alt+Tab (Super+Tab) Window Switching

This is using “Super” instead of “Alt”. If you want to use the latter you only need to replace all “Super” instances with “Tab”.

Bash Script

#!/bin/bash

# Related discussion:
@lujanfernaud
lujanfernaud / minitest_and_database_cleaner.md
Last active Dec 16, 2019
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
You can’t perform that action at this time.