Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍🔬

Luján Fernaud lujanfernaud

👨‍🔬
View GitHub Profile
@lujanfernaud
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.
Style/StringLiterals:
Enabled: false
@lujanfernaud
lujanfernaud / testing_image_attachments_in_rails.md
Last active Feb 22, 2018
Testing image attachments or uploads in Rails
View testing_image_attachments_in_rails.md

Testing Image Attachments/Uploads in Rails

Model

We need to use File.open(Rails.root.join("<image_path>")).

File.open(Rails.root.join("test/fixtures/files/sample.jpeg"))
@lujanfernaud
lujanfernaud / rails_pundit_custom_redirection_when_not_authorized.md
Last active Apr 30, 2018
Rails and Pundit: Custom Redirection When Not Authorized
View rails_pundit_custom_redirection_when_not_authorized.md

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.

Example:

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

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
RAILS_SERVE_STATIC_FILES=true
SECRET_KEY_BASE=production rails s
@lujanfernaud
lujanfernaud / removing_trailing_spaces.md
Last active May 31, 2018
Ruby and Nokogiri: Removing trailing spaces
View removing_trailing_spaces.md

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.

View rails_migrations.md

Rails: Migrations

Add reference:

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

Add index:

@lujanfernaud
lujanfernaud / rails_random_date_in_period.md
Last active Jun 19, 2018
Rails: Random Date in Period
View rails_random_date_in_period.md

Rails: Random Date in Period

def random_date_in_period(period)
  rand(period).seconds.ago
end

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
@lujanfernaud
lujanfernaud / rails_order_by_priority_and_date_with_limit_for_category.md
Created Jun 20, 2018
Rails: Order by Priority and Date with Limit for Category
View rails_order_by_priority_and_date_with_limit_for_category.md

Rails: Order by Priority and Date with Limit for Category

# group.rb

# When we pass NULL to LIMIT, Postgres treats it as LIMIT ALL (no limit).
# https://www.postgresql.org/docs/current/static/sql-select.html#SQL-LIMIT
def topics_prioritized(normal_topics_limit: nil)
  ids = topic_ids(normal_topics_limit)
@lujanfernaud
lujanfernaud / rails_dynamic_path_inside_partial.md
Last active Jul 2, 2018
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 / rails_update_comments_count_for_all_topics_in_a_group.md
Created Jul 17, 2018
Rails: Update Comments Count for All Topics in a Group
View rails_update_comments_count_for_all_topics_in_a_group.md

Rails: Update Comments Count for All Topics in a Group

def update_topics_comments_count
  begin
    ActiveRecord::Base.connection.execute <<~SQL
      UPDATE topics
         SET comments_count = (SELECT count(1)
                                 FROM topic_comments
                                WHERE topic_comments.topic_id = topics.id