David Kimura PRO
Joined 7/18/2015
Drifting Ruby Owner
David Kimura PRO said over 5 years ago on The Stimulus 2.0 Tutorial :
  I believe the main benefits of Stimulus is isolation, reloading, minimization, and ease of use.

Isolation - The controllers don't leak over into other pages. If the data attributes are present for that controller, it will work, but is deconstructed after the page refreshes or navigates away where those data attributes are no longer present. This makes working with complex front ends really nice. There is a caveat though with something like getusermedia where the use of a webcam or audio source could leak from one page to another and must be handled properly.

Reloading - Whether you're loading the controller on a new page refresh, adding in controllers via UJS, or even rending a partial sent over websockets, the controller will initialize and work. This use to be a pain in situations where UJS wouldn't initialize a datepicker and you had to manually initialize it again.

Minimization - You get a lot of functionality from such a tiny library. Even then, you don't have to write a bunch of Javascript to get highly interactive front ends. This combined with Turbolinks/Turbo makes for a super powerful tool.

Ease of Use - By following the conventions, you can make your javascript more readable and easier to maintain. Write less javascript and get more functionality. Bringing in additional libraries is easy with webpacker. This will be interesting to see how things change if we revert back to sprockets, but for now, webpacker is a very viable solution.

I recommend that you check out some of the other videos like Google Maps API,  Video Chat with WebRTC, FullCalendar, AutoComplete, Drag and Drop, Audio Recording, QR Codes and Cropping images. All of these are accomplished using StimulusJS.



https://www.driftingruby.com/episodes?tag=stimulusjs

David Kimura PRO said over 5 years ago on 3D Secure 2 Credit Cards and Stripe Checkout :
  You want to be careful with your plan_type. You are accepting the user input. They could type anything in the plan parameter and cause problems in your app. The PLAN array is just a list of the plans that you have created over in Stripe. If the plan_type doesn't find a matching plan, you should catch that and redirect the user.

In this episode, we update the user's attribute subscription_id. So, you could check this subscription_id if the user is subscribed.

def subscribed?
  subscription_id.present?
end

You would of course need to handle the subscription cancelled in a similar fashion via the webhooks and clear the subscription_id.

I think that there is a follow up episode warranted to explore handling webhooks more. For example, on Drifting Ruby, we handle a few different events. 

  • When the user subscription is successful
  • When a subscribed user is charged
  • When a subscribed user payment fails
  • When a subscribed user cancels

The last two are the trickiest. If a user payment fails, what should happen? This basically means that they have a subscription, but are not current with their payments. On Drifting Ruby, they still have their subscription where they can update their credit card information, but have been locked out of viewing the Pro Videos.

When a user cancels their subscription, no refund is given, but the subscription_id is also not removed. Instead, the expires_at attribute is updated to the end of their billing cycle which then will remove their subscription and access to the Pro videos.

David Kimura PRO said over 5 years ago on The Stimulus 2.0 Tutorial :
In those kinds of situations, you have a few different paths. 

If you use button_to for the button with remote: true, you can make an AJAX call via RailsUJS to your Rails application which would be a JS format. Within the <action>.js.erb, you could render a partial and insert it onto the page. If that partial has a stimulus controller within it, the controller would initialize and "just work"

If you use a stimulus controller for the button, when clicked, you can make the AJAX request back to the Rails application in a similar fashion and then render a partial within the Rails controller action and then insert this into the page.

David Kimura PRO said over 5 years ago on Purge Orphaned Active Storage Records :
within iTerm, it is the default font. Within VS Code, I'm using Jetbrains Mono https://www.jetbrains.com/lp/mono/ 


David Kimura PRO said over 5 years ago on Referencing Stimulus controllers in a gem :
I think that publishing them as an NPM project would be more appropriate for Rails apps with webpacker. https://www.driftingruby.com/episodes/creating-and-publishing-a-stimulus-controller-library. You could also do it with a gem similar to how Hotwire does it with its helpers and module types.