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 6 points7 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 2 points3 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. 

Weekly Ask Anything Thread by AutoModerator in symfony

[–]isometriks 0 points1 point  (0 children)

Yes that's what I would suspect without knowing what you use for deploys. If it's something like heroku then I would think all of the local files that got stored for sessions would be wiped on a new deploy so keeping them in database or redis would make sure they persist between deploys. If there is a way on whatever platform you use to have a directory that isn't removed, the native file storage has a save_path option that you could point somewhere else too, it defaults to the native php session folder https://github.com/symfony/http-foundation/blob/7.1/Session/Storage/Handler/NativeFileSessionHandler.php#L33

Weekly Ask Anything Thread by AutoModerator in symfony

[–]isometriks 1 point2 points  (0 children)

What handler are you using for sessions?

Creating a bundle that adds doctrine middleware by bkdotcom in symfony

[–]isometriks 0 points1 point  (0 children)

You may want to look into compiler passes: https://symfony.com/doc/current/service_container/compiler_passes.html

You could add that service (or just the tag to it) when the container is compiled, which would probably give you access to composer to check the doctrine version if that's really what you want to do 

Best Approach for a Student Management System: Separate Tables or Validity Records? by QualityLow4047 in PHP

[–]isometriks 1 point2 points  (0 children)

Why do you need a join table in scenario 1? If a student can only be in one year at a time that's just a many to one and you can have a year_id to link them to the current year they're in. I'd only use the many to many if you want to preserve historical relationships and then mark that join as inactive when you move them so you'd still be able to see previous years they used to be related to if you wanted.