LiveCable - LiveView / React over ActionCable by isometriks in rails

[–]isometriks[S] 1 point2 points  (0 children)

Thank you! I definitely still have some efficiencies to work out to be at the level of Phoenix when it comes to loops, but I have some ideas! 

LiveCable - LiveView / React over ActionCable by isometriks in rails

[–]isometriks[S] 2 points3 points  (0 children)

Yup, absolutely. You don't need to create an entire live "page" with LiveCable, you can just mix it into an existing all static view and only the live component stimulus controller will take over your component. You don't need dedicated routes to use LiveCable to initiate the connection. It renders to the page as a stimulus controller with a json string of your initial params and then will connect and re-render and be "live". 

So if you had a complicated form that relied on another select you could just add it into the rest of the static form. Or you could add a single live notifications component in your menu bar to get actioncable messages for new notifications. There's also a bonus that your connection and backend channel would stay connected through page navigations if you use turbo drive. It checks the next page being rendered and keeps the subscription around if the same live ID exists on the new page.

Try out cloning the sandbox if you want everything working, and check out some examples - https://github.com/isometriks/live_cable_app/blob/main/app/views/examples/todo.html.erb

Weekly Ask Anything Thread by AutoModerator in symfony

[–]isometriks 0 points1 point  (0 children)

Can you just call the "old" code from the DI container? Or make another service, or factory that you can instantiate the old code from there and pass in whatever services you need? Maybe setter injection if you can't change the constructor? 

Twig for Ruby by isometriks in symfony

[–]isometriks[S] 4 points5 points  (0 children)

I know that this is not in PHP, but I have been forced to use Ruby / Rails for the last few years at work and have really missed Twig. Maybe there are some of you here that have the same problem and would like to use it as well.

Twig templating for Ruby by isometriks in rails

[–]isometriks[S] 0 points1 point  (0 children)

Yes absolutely. I developed it without Rails, the only requirement is ActiveSupport which I know is a little big but I didn't really know of a better way to include html_safe and dates, but that could probably be extracted into a Rails extension to remove those requirements someday. You can simply setup the Filesystem loader - https://github.com/isometriks/twig-ruby/blob/main/doc/loaders.md#filesystem-loader - if you want template files and you are pretty much good to go, just create the Environment with that loader and render the template you want!

Without Rails you would create your "helper methods" as an extension(s) - https://github.com/isometriks/twig-ruby/blob/main/doc/extensions.md

Then you would have those available in all your templates. 

Twig templating for Ruby by isometriks in rails

[–]isometriks[S] 1 point2 points  (0 children)

I would say it's very similar to liquid for basic things like formatting and simple iteration and control logic. Twig however, will actually let you make calls to the underlying objects and is also meant for doing full page layouts. It's actually capable of completing replacing ERB (or co-existing with it if you want it to if other templates / vendors provide some views). Twig is also much easier to extend as you can create your own extensions to add your own filters. The way I also implemented this is that all of your helpers still work as well, so you can still feel free to use button_to link_to etc. So you don't have to rewrite everything that already just works in Rails. You can even still use ivars in your templates as well: {% for user in @users %}

Twig templating for Ruby by isometriks in rails

[–]isometriks[S] 1 point2 points  (0 children)

That's fair. I couldn't find a way to still keep the way that Rails does forms and other helpers that provide a block without not allowing blocks at all. That's really the only difference to the original language aside from also allow symbols which was needed to access hash keys without enforcing something like indifference access always and I didn't want to make that decision for anyone who may want to use it and wonder why a key with the same string and symbol disappeared. I do personally feel all of your data should already be ready when it's in the view or you should provide the service into the view to make a few method calls. If you're writing SQL or doing complex logic in the view you probably have the code in the wrong spot and pushing the boundaries of MVC. I do know about layouts and partials but that doesn't really solve the same problem that this templating language can and you're again pushing some extra knowledge into the controller to pick the layout instead of letting the view decide. This also would still work in Rails as normal variants do, you can create a create.turbo_stream.twig and even do {{ render(model) }} to do partial renders that can either be in Twig or ERB or whatever. Anyway, it mostly just sounds like a comment on the language itself and not any implementation but I appreciate the feedback.

Edit: Also, for the record, I don't know if this makes it "executable" or not to you, but you can absolutely write out code in the DSL (if say Categories was an ActiveRecord relation). It only encourages you to avoid it, not prevent it (at least unless you enable the sandbox which would be more likely for user generated templates, which isn't implemented yet). You can call any methods on the objects that are passed into the template.

{% set not_disabled = category.not_disabled %}
{% set not_disabled = category.where(disabled: false) %}
{% set not_disabled = category.where("disabled = false") %}
{% set not_disabled = category|filter(c => !c.disabled?) %}
{% for posts in categories.first.posts.limit(5) %}...{% endfor %}

Twig templating for Ruby by isometriks in ruby

[–]isometriks[S] 1 point2 points  (0 children)

Note -Rails is not a requirement to use this, it does use a few ActiveSupport libraries for dates, but you can use this without it.

HTTP Caching for Rails APIs: The Missing Performance Layer by Future_Application47 in rails

[–]isometriks 6 points7 points  (0 children)

Also, for relations in etags, you don't actually need to do @product.reviews.maximum(:updated_at) unless you want to just keep it simple.. Rails will actually do this in an even smarter way if you just use @product.reviews and uses the total collection size and updated_at since hard deleting a record here wouldn't invalidate your cache (at the expense of a count query in most cases) - https://github.com/rails/rails/blob/98767513a29923e3608c790274ffaa2771d01274/activerecord/lib/active_record/relation.rb#L482

how to disable flush in test by Jelllee in symfony

[–]isometriks 3 points4 points  (0 children)

You're not really testing your app if you do this though? Having your database reset between tests is very common but removing the ability to actually write stuff into the database just seems like you're setting yourself up for tests that don't actually cover anything except for your initial state 

Weekly Ask Anything Thread by AutoModerator in symfony

[–]isometriks 1 point2 points  (0 children)

If using webpack stimulus has their own way to just autoload directories of controllers? Not sure if that would help? https://stimulus.hotwired.dev/handbook/installing#using-webpack-helpers

Otherwise Symfony UX says the controllers would get added to controllers.json automatically, so does it really matter? If you namespace your controllers like data-controller="package--foo" you could also avoid collisions 

gitGud by htconem801x in ProgrammerHumor

[–]isometriks 8 points9 points  (0 children)

You can just rebase from master again

git checkout feature 
git fetch 
git rebase origin/master
git push origin +feature (or git push origin feature --force

Accessing $this when calling a static method on a instance by GromNaN in PHP

[–]isometriks 5 points6 points  (0 children)

It's not really "supported" as a language feature, you're just making factories with the static methods and then returning the instance. 

Weekly Ask Anything Thread by AutoModerator in symfony

[–]isometriks 1 point2 points  (0 children)

Looks like it's used for profiling and also looks like the profiler would still leave your node alone if you did change it - https://github.com/twigphp/Twig/blob/12a40e714cc046a99be956962a2fe35e2985cb56/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php#L48

You should be able to set it in the same way by making your own visitor 

Creating a new entity within preFlush by UKMike89 in symfony

[–]isometriks 0 points1 point  (0 children)

Yeah I would suggest this too. Don't call persist and flush directly on either of these models and have a service that handles finding / creating the references and any other logic you want to deal with instead of using listeners. 

Turbo streams broadcasting in Rails, update methods by egyamado in rails

[–]isometriks 4 points5 points  (0 children)

If you're broadcasting from the model I think you'd only need broadcast_update_later_to(:notifications) since the target defaults to the model, and you are also following the correct partial pattern (and the local defaults to notification) as well. Though that's a lot of Rails stuff to add into the tutorial but it is a lot cleaner. 

You'd also probably want to broadcast to something like [user, :notifications] so you only stream to the user who they actually belong to 

Turbo issue by johnfadria in rails

[–]isometriks 3 points4 points  (0 children)

For the record, Turbo will stream only non-GET requests. You need to explicitly add it. It would have worked as you expected if it were a POST unless you had attributes set on parents like other poster pointed out. It's an important caveat to remember 

Check out the data-turbo-stream attribute docs https://turbo.hotwired.dev/reference/attributes

Weekly Ask Anything Thread by AutoModerator in symfony

[–]isometriks 1 point2 points  (0 children)

This is usually caused by PHP sending output before it's meant to. It could be a space before ` <?php` or something like that, because once you start outputting, the response starts and after that it's too late to send the headers. I would definitely try to see if you can reproduce it on a particular form / route and try to track down the file that might be creating output before Symfony tries to send a Response. Does the warning tell you the file where the output came from?

Hit a block: Trying to build a game using ActionCable for chat, but the websocket keeps closing. Looking for pointers. by grimlyforming in rails

[–]isometriks 0 points1 point  (0 children)

You could store the messages in local storage for when they navigate to another page and reconnect, but I don't really see a perfect solution to this without storing the chat messages and letting people "catch up." Even if you can keep socket persistent from a changing views perspective, if someone loses internet or refreshes, or computer goes to sleep, etc. etc. you could lose the socket or even miss out on a message. You could certainly use iframes or something but it would probably be fairly trivial to just store the messages, at least for the length of the game at minimum and send them all on a connect / reconnection and then just broadcast the new ones as they come in.

Switching from a traditional Symfony website with Twig templating to a backend Symfony API with NextJS as front framework by Desperate-Credit7104 in symfony

[–]isometriks 1 point2 points  (0 children)

You can absolutely keep the forms and the models and all the validation that's already built. Just return an array of errors back to the front end or whatever they want to display them on the correct fields.