Skip to content

Instantly share code, notes, and snippets.

Luján Fernaud lujanfernaud

Block or report user

Report or block lujanfernaud

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
lujanfernaud / .rubocop.yml
Created Aug 2, 2017
Prevent RuboCop from checking the use of quotes
View .rubocop.yml
inherit_from: ../.rubocop.yml
# Checks if uses of quotes match the configured preference.
Enabled: false
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::BossaNova.artist, Faker::Book.title]
54.times do |n|
start_date = Faker::Date.between(, 6.months.from_now)
end_date = start_date +
Event.create!(title: titles.sample + " ##{n}",
lujanfernaud /
Last active Sep 8, 2017
Rails: Test datetime_select using Capybara

Rails: Test datetime_select using Capybara

# test_helper.rb

def select_date_and_time(date, **options)
  return nil unless date
  field = options[:from]
lujanfernaud /
Last active Feb 22, 2018
Testing image attachments or uploads in Rails

Testing Image Attachments/Uploads in Rails


We need to use"<image_path>"))."test/fixtures/files/sample.jpeg"))

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 /
Last active Apr 30, 2018
Rails and Pundit: Custom Redirection When Not Authorized

Rails and Pundit: Custom Redirection When Not Authorized

To use a custom redirection when a resource is not authorized we only need to add an user_not_authorized method to the controller, and define the redirection.


class Groups::MembershipRequestsController < ApplicationController
  def new
    @membership_request =
lujanfernaud /
Last active May 24, 2018
Running a Rails 5 App in Production Locally

Running a Rails 5 App in Production Locally

This is what we need to do:

export RAILS_ENV=production
rake db:reset
rake assets:precompile
SECRET_KEY_BASE=production rails s
lujanfernaud /
Last active May 31, 2018
Ruby and Nokogiri: Removing trailing spaces

Ruby and Nokogiri: Removing trailing spaces

sample_input = "<div>Hi&nbsp; &nbsp; &nbsp;</div>"
parsed_input = Nokogiri::HTML.parse(sample_input).text #=> "Hi   "

parsed_input.gsub(/\p{Space}*\z/, "") #=> "Hi"

\p{Space} catches any whitespace character.


Rails: Migrations

Add reference:

# add_reference :table, :column_name, foreign_key: boolean
add_reference :bookings, :flight, foreign_key: true

Add index:

lujanfernaud /
Last active Jun 19, 2018
Rails: Random Date in Period

Rails: Random Date in Period

def random_date_in_period(period)

random_date_in_period 1.year #=> Tue, 22 May 2018 04:55:54 UTC +00:00
random_date_in_period 1.year #=> Mon, 06 Nov 2017 19:50:07 UTC +00:00
You can’t perform that action at this time.