# In your Gemfile
gem "localhost"
# Then, depending on your desired server
gem "falcon"
gem "puma"
In programming languages, literals are textual representations of values in the source code. This is a syntactical concept.
Some examples:
7 # integer literal
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
users_file = Rails.root.join('storage', 'users.yml.erb') | |
if File.exist? users_file | |
users = YAML.load(ERB.new(File.read(users_file)).result) | |
User.insert_all( | |
users.map { |user| user.except("password") }, | |
unique_by: :username | |
) | |
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
ass ApplicationController < ActionController::Base | |
before_action :authenticate! | |
helper_method :current_user | |
helper_method :user_signed_in? | |
private | |
def authenticate | |
@current_user = nil |
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
# Unnecessary indexes slows down writes and consumes additional storage and memory. | |
# Just paste this snippet in your Rails console (bundle exec rails c). | |
# And it will print all redundant indexes that are already covered by another index on the table: | |
# Table `pages`: index `site_idx` (site_id) already covered by `site_slug_idx` (site_id,slug) | |
# Table `optins`: index `list_idx` (list_id) already covered by `list_active_idx` (list_id,active) | |
ActiveRecord::Base.connection.tables.map do |table| | |
indexes = ActiveRecord::Base.connection.indexes(table).select(&:valid).reject(&:where) |
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
frozen_string_literal: true | |
# A leaky bucket rate limiter for Ruby | |
# | |
# @see https://www.mikeperham.com/2020/11/09/the-leaky-bucket-rate-limiter/ | |
# @see https://en.wikipedia.org/wiki/Leaky_bucket | |
class RateLimit | |
class Error < StandardError | |
attr_accessor :retry_in |
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
module SessionHelper | |
extend ActiveSupport::Concern | |
class ::SessionsBypassController < ActionController::Base | |
def show | |
session[:user_id] = params[:user_id] | |
session[:team_id] = params[:team_id] | |
session[:tenant] = Apartment::Tenant.current |
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
require 'digest' | |
class ConsistentHashing | |
def initialize(nodes) | |
nodes.map { |node| add_node(node) } | |
end | |
def find_cache(key) | |
puts | |
hash = hash_value(key) |
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
# spec/system/support/login_helpers.rb | |
# See this blog post for setup guide: https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing | |
module LoginHelpers | |
def login_as(user) | |
# Craft session cookie to make request authenticated (to pass even routing constraints) | |
# Compilation of these: | |
# - https://dev.to/nejremeslnici/migrating-selenium-system-tests-to-cuprite-42ah#faster-signin-in-tests | |
# - https://turriate.com/articles/2011/feb/how-to-generate-signed-rails-session-cookie | |
# - https://github.com/rails/rails/blob/43e29f0f5d54294ed61c31ddecdf76c2e1a474f7/actionpack/test/dispatch/cookies_test.rb#L350 |
NewerOlder