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 June 10, 2021 19:20
Use the attributes API to enforce types on store_accessor
class User < ActiveRecord::Base
  store :settings, accessors: [ :enable_notifications ]
  attribute :enable_notifications, :boolean
end
@stevepolitodesign
stevepolitodesign / example.md
Last active June 11, 2021 15:19
Overriding Named Route Parameters
# config/routes.rb
resources :plans, param: :uuid
edit_plan   GET   /plans/:uuid/edit(.:format)   plans#edit
     plan   GET   /plans/:uuid(.:format)        plans#show 
Class PlansController &lt; ApplicationController
@stevepolitodesign
stevepolitodesign / example.md
Last active June 22, 2021 19:03
When to Intentioanlly Raise and Error
require 'net/http'
require 'uri'

class ApiJob < ApplicationJob

  class Error < StandardError
  end

 queue_as :default
@stevepolitodesign
stevepolitodesign / example.md
Last active June 24, 2021 14:44
Export Database and Import into Render
pg_dump local_development_database_name -c -C -f local_development_database_backup.sql
# This worked, with errors.
psql $external_connection_string < local_development_database_backup.sql
# This might be better.
pg_restore $external_connection_string -c -f local_development_database_backup.sql
@stevepolitodesign
stevepolitodesign / example.md
Last active June 26, 2021 15:55
How to share a similar method between two models?
# The `title` methods in each model are so similar it feels like they should be able to be shared. 
# Normally this would be a good excuse to use a Concern, but is that even possible?

class Message < ApplicationRecord
  def title
    subject
  end
end
@stevepolitodesign
stevepolitodesign / example.md
Created July 3, 2021 16:49
Use devise controller in routes.rb
# config/routes.rb
Rails.application.routes.draw do
  
  # If you want to reference devise controllers in routes.rb
  # make sure to wrap your route inside the devise_scope block
  devise_scope :user do 
    root "devise/sessions#new"
  end
@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
@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
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 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