Simplify your VIEW COMPONENTS with dry-initializer

Simplify your VIEW COMPONENTS with dry-initializer

Rapid Ruby

1 год назад

1,278 Просмотров

I’m going to show you a neat little library from the dry-rb project, dry-initializer. It can be really useful to help reduce boilerplate code in ViewComponent’s and service objects.

Check out the dry-initializer docs:
https://dry-rb.org/gems/dry-initializer/3.0/

Source code available here:
https://github.com/phawk/rapid_dry_initializer_view_component

P.s. Want access to next week's video right now? Head on over and sign up on rapidruby.com now!

---

Has this video helped you out? Consider buying me a coffee to say thanks and support future content like this, thanks!
https://app.payhere.co/rapid-ruby/buy-me-a-coffee-aa4618e4-45d1-4727-81b9-2d081449d61d

You can also find more content like this, including premium courses and exclusive community and mentorship to help you level-up! Head over to https://rapidruby.com and use the code RRYOUTUBE for a discount!

Тэги:

#Dry-rb #Dry-initializer #Dry_initializer #ViewComponent #ViewComponent_Rails #Rails #Ruby_on_Rails #Rails_screencasts #Web_development_in_Rails #Web_development_in_Ruby
Ссылки и html тэги не поддерживаются


Комментарии:

Tom Branson
Tom Branson - 09.03.2023 08:59

I think that you've jumped the shark in this video suggestion. Personally, I believe that no additional libraries should be installed until all proper solutions have been exhausted. In this example, it sort of missed the point a little bit about the definition of Web Components (ie View Components in this case), a "Thumbnail" is a separate component to a Card, a thumbnail can be reused in other places, thus a Thumbnail component should be created, and in the initializer, place nil as the default, and in the call method put the image tag with @thumbnail.presence || "default title". Bam you're done. So, when the Card component gets created just use the thumbnail attribute or eliminate it, since it defaults to nil (optional), hits that part in the partial, just placing a render Thumbnail.new(thumbnail) and it will either have the thumbnail or nil and it will figure out what it should do, and you're just placing the one line in the Card partial without the if/else block. Or possibly to use the slot feature within the card component and point to the Thumbnail component.

I enjoy your tips and suggestions and am a subscriber. This comment is being a little critical, since if I were a code reviewer and the developer thought that solving something by importing a new gem or library to solve something that is easily solvable using the current gem because all the avenues of the solution weren't examined, I would offer this as the simpler solution. Github (the creator of the View Component gem) has already thought of these scenarios for us, just use the solutions. And if it's not available and seems like it should be there, offer up a pull request.

Ответить
Bob Maerten
Bob Maerten - 08.03.2023 12:02

Seems weird to me that an "option" is required by default :/
Great tooling though.

Ответить
Daniel Beggan
Daniel Beggan - 07.03.2023 22:07

Great tip

Ответить
Dennis Njoroge
Dennis Njoroge - 07.03.2023 20:19

Quality tutorials.

Give us more.

Thank you please

Ответить
Tomas Valent
Tomas Valent - 07.03.2023 18:58

I was wondering for ages why this isn't a thing in vanilla Ruby 🤔

Ответить
Janko Marohnić
Janko Marohnić - 07.03.2023 18:18

Been using dry-initializer with View Component in a previous company, works wonderfully 👌

Ответить
stpaquet
stpaquet - 07.03.2023 17:41

very useful. I need to delve a bit more into view_component.

Ответить