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 February 1, 2022 14:52
Rails as_json API Example
class UsersController
  def index
    @users = User.all
    
    respond_to do |format|
      format.html
      format.json { render json: @users.as_json }
    end
 end
@stevepolitodesign
stevepolitodesign / example.md
Last active January 28, 2022 21:30
Creating a cascading foreign key when calling references
rails g model session user:references

Before

class CreateSessions < ActiveRecord::Migration[6.1]
  def change
 create_table :sessions do |t|
@stevepolitodesign
stevepolitodesign / example.md
Created January 28, 2022 04:35
Broken Heroku Builds on Rails 7 with Active Storage
 require_relative "boot"

 require "rails/all"

 # Require the gems listed in Gemfile, including any gems
 # you've limited to :test, :development, or :production.
 Bundler.require(*Rails.groups)

 module Redirect2Link
@stevepolitodesign
stevepolitodesign / example.md
Last active January 24, 2022 14:36
Handle unauthorized requests in Rails from scratch

Before

# app/controllers/concerns/authorizable.rb
module Authorizable
  extend ActiveSupport::Concern

  included do
    def authorize_request(from:, against:)
      if from != against
@stevepolitodesign
stevepolitodesign / example.md
Created January 22, 2022 14:31
Prevent the destruction of a record in Active Record
class Webpage < ApplicationRecord

  def self.delete_all
    raise ActiveRecord::DeleteRestrictionError
  end

  def delete
    destroy
  end
@stevepolitodesign
stevepolitodesign / example.md
Last active January 19, 2022 10:44
Association extensions Example

Association Extensions

An API for adding new finders, creators, and other factory-type methods that are only used as part of this association.

class AnonymousSession < ApplicationRecord
  has_many :webpages, as: :owner do
    def nullify_owners
      # records returns an array of webpages
 records.each(&amp;:nullify_owner)
@stevepolitodesign
stevepolitodesign / example.md
Created December 26, 2021 14:23
Fix Import Maps from Rails 7.0.0.alpha2 to Rails 7.0.0
  1. Make sure sprockets-rails is installed. This wasn't the case when using Rails 7.0.0.alpha2. I think this was the most important step.
# Gemfile
gem "sprockets-rails"
  1. Add preload: true to the necessary assets. I'm just copying the default Rails 7.0.0 config. This differs from the original Rails 7.0.0.alpha2 install.
@stevepolitodesign
stevepolitodesign / example.md
Last active December 19, 2021 11:20
Validate Active Storage Attachements
class Webpage < ApplicationRecord
  has_one_attached :open_graph_image

  validate :open_graph_image_content_type
  validate :open_graph_image_size

  private

  def open_graph_image_content_type
@stevepolitodesign
stevepolitodesign / example.md
Last active December 14, 2021 21:50
Use authenticate_by and not authenticate

Rails 7 introduces the authenticate_by class method which prevents timing-based enumeration attacks. This is an alternative to calling authenticate on an instance of a User.

class User < ActiveRecord::Base
  has_secure_password
end

Before

@stevepolitodesign
stevepolitodesign / example.md
Last active December 7, 2021 11:02
Should you return a status when calling render in a Controller?

Before

class PostsController < ApplicationController
  def create
    if @post.save(post_params)
      redirect_to @post
    else
      render :new
 end