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 / create_message.rake
Last active March 24, 2021 01:24
Prevent Active Record Callbacks from creating new records
namespace :create_message do
desc "Creates a Message but prevents Notifications from being created on the after_create Callback"
task :perform, [:message_content] => :environment do |task, args|
# https://api.rubyonrails.org/classes/ActiveRecord/Suppressor.html
Notification.suppress do
Message.create(content: args.message_content)
end
end
end
@stevepolitodesign
stevepolitodesign / rails_guest_user_example.rb
Last active April 19, 2021 11:37
Create a guest user in Rails
# [1]
class AddGuestToUsers < ActiveRecord::Migration[6.0]
def change
add_column :users, :guest, :boolean, default: false, null: false
end
end
# [2]
module GuestUser
extend ActiveSupport::Concern
@stevepolitodesign
stevepolitodesign / filter_sensative_data_in_vcr.md
Last active May 13, 2021 10:31
Filter sensative data when using VCR
# test/test_helper.rb
VCR.configure do |config|
  config.filter_sensitive_data('<BROWSERLESS_PRIVATE_KEY>') do |filter|
    Rails.application.credentials.dig(:browserless, :private_key)
  end
end
@stevepolitodesign
stevepolitodesign / posts_controller.rb
Last active May 18, 2021 18:17
Authorize Teams with Pundit
# app/controllers/posts_controller.rb
def show
raise Pundit::NotAuthorizedError unless PostPolicy.new(current_team, @post).show?
end
@stevepolitodesign
stevepolitodesign / post.rb
Last active May 29, 2021 19:27
Avoid scopes that use find_by
# BAD
# This scope will return an ActiveRecord::Relation if the first query returns nil
class Post < ApplicationRecord
scope :featured, -> { find_by(featured: true) }
end
# => Post.featured
# => Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."featured" = ? LIMIT ? [["featured", 1], ["LIMIT", 1]]
# => Post Load (0.1ms) SELECT "posts".* FROM "posts" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
# => #<ActiveRecord::Relation [...]>
@stevepolitodesign
stevepolitodesign / example.md
Created May 29, 2021 22:48
Use Pundit as a Feature Flag System
# app/models/user.rb
class User < ApplicationRecord  
  FEATURES = %i[enable_post_meta_description].freeze
  store :features, accessors: User::FEATURES
end
# app/policies/feature/enable_post_meta_description_policy.rb
@stevepolitodesign
stevepolitodesign / example.md
Last active May 30, 2021 16:10
Turbo broadcast_action_to Example
class Screenshot < ApplicationRecord
  belongs_to :webpage

  after_create_commit :broadcast_later

  private

    def broadcast_later
      # We could use `broadcast_action_to` but using `broadcast_action_later_to`
@stevepolitodesign
stevepolitodesign / example.md
Last active June 2, 2021 20:04
Preview a new layout with params
class PostsController < ApplicationController
  ...
  def show
    respond_to do |format|
      # If v2=enabled is in the URL (https://www.example.com/posts/1?v2=enabled) render the new layout.
      if params[:v2] == "enabled"
        format.html { render template: 'posts/show_v2' }
      # Otherwise load the existing layout.
 else
@stevepolitodesign
stevepolitodesign / example.md
Last active June 8, 2021 00:38
Polymorphic Table VS Join Table

has_many :through Association

A Post can have many Tags and a Tag can belong to many Posts. A has_many :through association may seem like a good approach, but what if a new model is introduced that also needs to have many Tags?

clas Post < ApplicationRecord
  has_many :post_tags
  has_many :tags, through: :post_tags
end
@stevepolitodesign
stevepolitodesign / example.md
Last active June 9, 2021 19:09
Call current_user in a Integration Test
require "test_helper"

class UserFlowsTest < ActionDispatch::IntegrationTest
  setup do
    @user = users(:one)
  end

  test "some test"
 sign_in @user