Skip to content

Instantly share code, notes, and snippets.

View krzykamil's full-sized avatar
🤔

Krzysztof Piotrowski krzykamil

🤔
View GitHub Profile
@krzykamil
krzykamil / decorations.rb
Created October 7, 2022 09:04
rspec matcher for decorator
# spec/support/matchers/decorations.rb
module Support
module Matchers
module Decorations
extend RSpec::Matchers::DSL
def decorated_object_attributes(model, klass: nil, user: nil, context: nil)
context ||= {}
attrs = { object: model }
@krzykamil
krzykamil / services.rb
Created October 7, 2022 09:24
RSpec DSL for stubbing services
module Support
module Services
class HaveBeenCalledMatcher
include RSpec::Mocks::Matchers::Matcher
NotCalledYet = Class.new(StandardError)
def initialize(&block)
@matcher = RSpec::Mocks::Matchers::HaveReceived.new(:call, &block)
end
@krzykamil
krzykamil / with_every_combination.rb
Created October 7, 2022 09:37
RSpec DSL for combo testing
module Support
module WithEveryCombination
# Takes an hash of variable names to all possible values, and creates RSpec contexts for
# every possible combination of values
#
# For example:
# with_every_combination(
# assignment: [Assignments::Models::Assignment.new, nil],
# start_date: [Date.current - rand(1..10).months, nil],
# end_date: [Date.current + rand(1..10).months, nil]
@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() {
@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 / 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 / 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 / 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 / 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 / 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) }