Skip to content

Instantly share code, notes, and snippets.

View stevepolitodesign's full-sized avatar
💎
return profile.status;

Steve Polito stevepolitodesign

💎
return profile.status;
View GitHub Profile
@stevepolitodesign
stevepolitodesign / example.md
Last active December 3, 2021 21:41
Require an individual parameter.

Require an individual parameter in Rails.

When editing your account, you are required to provide your current password. However, you can't use the password field in a form becuase that field is needed if you ever want to update the current password.

class User < ApplicationRecord
  # This is a non-database-backed attribute needed in forms and controllers.
  attr_accessor :current_password
end
@stevepolitodesign
stevepolitodesign / example.md
Last active October 31, 2021 16:03
Poor Man's Turbo with Remote Forms and Stimulus JS
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="form"
export default class extends Controller {
  static targets = [ "error" ]
  static values = { target: String,  action: String }

  connect() {
    this.target = this.hasTargetValue && document.querySelector(this.targetValue);
@stevepolitodesign
stevepolitodesign / example.md
Last active October 25, 2021 15:00
assert_in_delta example

Using assert_equal to compare dates will fail becuase they'll be off my a few miliseconds.

date = 1.month.from_now
travel_to date

@post.update(title: "Updated Title")

assert_equal date, @post.updated_at
# =&gt; No visible difference in the ActiveSupport::TimeWithZone#inspect output

Using Symbols To Generate Links in Rails

Apparently, you can pass link_to a Symbol instead of a path and it will still work.

Example

# These will both evaluate to the same thing.
<%= link_to "New Post", :new_post %>
<%= link_to "New Post", new_post_path %>
@stevepolitodesign
stevepolitodesign / example.md
Last active October 5, 2021 20:38
Combine joins and where for better queries

Problem

Query for all courses bookmarked by the current user.

Before

This works, but it requires two queries.

@bookmarks = current_user.bookmarks
@stevepolitodesign
stevepolitodesign / example.md
Last active September 2, 2021 17:59
Query Action Text for Links
# app/models/user.rb
class User < ApplicationRecord
  has_rich_text :bio
end
# Get all users who have a bio
User.joins(:rich_text_bio)
@stevepolitodesign
stevepolitodesign / example.md
Last active August 18, 2021 17:20
Prevent a Mailer Delivery in Rails
class NotificationsMailer < ApplicationMailer
  # https://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-callbacks  
  after_action :prevent_delivery

  def unread
    @user = params[:user]
    mail(to: @user.email, subject: 'Here's what you missed...')
  end
@stevepolitodesign
stevepolitodesign / example.md
Created July 30, 2021 18:08
Howe to avoid writing redundant methods
class User < ApplicationRecord

  ... # Lot's of methods making it hard to scan this class at a glance.

  # Orignal method created earlier in this project's life
  def formatted_name
    self.first_name + " " + self.last_name
  end
 
@stevepolitodesign
stevepolitodesign / example.md
Last active July 16, 2021 19:12
Lock Down Staging Environment in Rails
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  
  # Will prompt the user for a username and password if on the staging environment.
  # This helps keep the app locked down and prevents bots from crawling the site.
  before_action :http_basic_authentication, if: :http_basic_authentication_enabled?
  
  private
  
@stevepolitodesign
stevepolitodesign / example.md
Last active July 8, 2021 18:37
Use different layouts for the same mailer
class NotificationsMailer < ApplicationMailer
  
  def notify
    @user = params[:user]
    mail(to: @user.email, subject: "You have a new notification") do |format|
      if @user.admin?
        # This method will use app/views/layouts/mailer.html+custom_admin_layout.erb
        format.html { render layout: 'mailer', variants: :custom_admin_layout }
 else