Last active
April 18, 2023 08:47
-
-
Save krzykamil/c3145fcfefd3fbd51b7fc23cb36d0098 to your computer and use it in GitHub Desktop.
Read dry-effect (component)
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
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 |
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
##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 |
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
<h1> | |
<%= logged_in_user_name %> | |
</h1> | |
<%= current_user.id %> |
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
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 |
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 "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