Skip to content

Instantly share code, notes, and snippets.

@krzykamil
Last active April 18, 2023 08:47
Show Gist options
  • Save krzykamil/c3145fcfefd3fbd51b7fc23cb36d0098 to your computer and use it in GitHub Desktop.
Save krzykamil/c3145fcfefd3fbd51b7fc23cb36d0098 to your computer and use it in GitHub Desktop.
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
##Any sort of rspec config file that would be loaded in the spec above
RSpec.configure do |rspec|
rspec.include Dry::Effects::Handler.Reader(:current_user), type: :component
end
<h1>
<%= logged_in_user_name %>
</h1>
<%= current_user.id %>
class UserProfileComponent < ViewComponent::Base
include Dry::Effects.Reader(:current_user, default: nil)
def logged_in_user_name
'You are logged in as: ' + current_user.name
end
end
require "rails_helper"
RSpec.describe UserProfileComponent, type: :component do
let(:current_user) { User.new(name: 'Dr. Brule') }
before { with_current_user(current_user) { render_inline(component) } }
it { is_expected.to have_text 'You are logged in as: Dr. Brule' }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment