Skip to content

Instantly share code, notes, and snippets.

View krzykamil's full-sized avatar
🤔

Krzysztof Piotrowski krzykamil

🤔
View GitHub Profile
@krzykamil
krzykamil / text.md
Created February 27, 2024 11:18
Capybara

Capybara

Capybara is good when it's good, and it's bad when it's bad. When you don't know how to use it, it can be tragic. When you use it for the wrong job, it is a pain.

Here us a quick, and absolutely subjective guide to using it correctly, which mostly means adapting it to the needs of your project, handling it limitations, and connecting it to RSpec.

Matchers

Main problem with asserting in RSpec and Capybara for me is that there is too many options. However RSpec makes it relatively easy to build your own. So instead of always googling and checking documentation while making sure you are looking at the right version of it, you can adapt those matchers to the needs of your projects.

@krzykamil
krzykamil / download_all_at_once_later.rb
Last active January 9, 2024 16:42
Deffer - dry-effects
# frozen_string_literal: true
require 'dry/effects'
class DownloadManager
include Dry::Effects.Defer
def initialize(number_of_files)
@downloaded_files = []
@number_of_files = number_of_files
end
@krzykamil
krzykamil / basic.rb
Created October 25, 2023 09:18
Interrupt - dry-effects error flow
# frozen_string_literal: true
require 'dry/effects'
class LoginManager
include Dry::Effects::Handler.Interrupt(:invalid_credentials, as: :catch_invalid_credentials)
def call
error, message = catch_invalid_credentials do
yield
end
@krzykamil
krzykamil / whole_app_dependencies.rb
Last active April 25, 2023 08:10
Resolve - dry-effects dependency injection
class ProviderMiddleware
include Dry::Effects::Handler.Resolve
def initialize(app, dependencies)
@app = app
@dependencies = dependencies
end
def call(env)
provide(@dependencies) { @app.(env) }
@krzykamil
krzykamil / console.sh
Created April 20, 2023 10:39
State (write) dry-effect
✓ ruby stack.rb
What do you want to do? (add, remove, see, count)
see
#<Plate:0x0000557c3f0598b0>
What do you want to do? (add, remove, see, count)
add
#<Plate:0x0000557c3f059c20>
#<Plate:0x0000557c3f0598b0>
#<Plate:0x0000557c3f890ce0>
What do you want to do? (add, remove, see, count)
@krzykamil
krzykamil / application_controller.rb
Last active April 18, 2023 08:47
Read dry-effect (component)
class ApplicationController < ActionController::Base
include Dry::Effects::Handler.Reader(:current_user)
around_action :set_current_user
private
def set_current_user
with_current_user(current_user) { yield }
end
end
@krzykamil
krzykamil / time.rb
Last active April 17, 2023 12:10
Current Time Algebraic Effect
require 'dry/effects'
class CurrentTimeMiddleware
include Dry::Effects::Handler.CurrentTime
def initialize(app)
@app = app
end
def call(env)
@krzykamil
krzykamil / drawing.erb
Created February 13, 2023 09:40
Drawing
<div data-controller="drawing" class="mt-4 p-6">
<h1>Drawing Lines</h1>
<button
class="my-4 mx-4 px-3 py-2 bg-red-600 font-semibold rounded"
data-action="drawing#drawLine"
data-drawing-length-param="300"
data-drawing-direction-param="horizontal"
data-drawing-x-param="100"
@krzykamil
krzykamil / clipboard.erb
Last active April 17, 2023 11:58
Clipboard
<div>
<div data-controller="clipboard" class="flex flex-row mt-4 p-6">
<input type="text" class="form-input text-black" value="SeCrEtKeY-42!" data-clipboard-target="source" readonly />
<button class="mx-4 flex px-3 py-2 bg-slate-900 font-semibold rounded" data-clipboard-target="button" data-action="clipboard#copy">
Copy
</button>
</div>
<div data-controller="clipboard" class="flex flex-row mt-4 p-6">
@krzykamil
krzykamil / checkbox_list_controller.js
Last active April 17, 2023 11:59
Simple as possible checklist with stmulus
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
static targets = ["checkbox"]
checkAll() {
this.setAllCheckboxes(true);
}
checkNone() {