# 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/posts_controller.rb | |
def show | |
raise Pundit::NotAuthorizedError unless PostPolicy.new(current_team, @post).show? | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 [...]> |
# 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
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`
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
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
require "test_helper"
class UserFlowsTest < ActionDispatch::IntegrationTest
setup do
@user = users(:one)
end
test "some test"
sign_in @user
OlderNewer