-
Notifications
You must be signed in to change notification settings - Fork 991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generator for utils/helpers directory? #299
Comments
I like this idea! |
@AryanJ-NYC Are you up for the task? The core team might have their hands full btw I loved your last livestream |
Sure! I'd love to take a crack at it. I may not be able to start until tomorrow or so but would love to be assigned. One question: I understand it was just meant to be an example but any preference with respect to a default export vs. named export? I'm imagining util files that I've written which normally have >1 functions being exported (which makes me sort of lean towards named exports). P.S. I appreciate the kind words about the livestream! I actually hope to livestream this task! |
It should be named export, I just updated the examples |
I change the examples from utils to helpers, to resemble more like ruby on rails. |
In my humble opinion ./redwood/packages/cli/src/commands/generate
$ ls
README.md commands helpers.js templates (The I'm not happy with this code, but I'm OK with this code because it's colocated to the functionality that it supports. Colocation is one of the principles that I try to follow in order to simplify my code. Colocation works well because I can focus on the task at hand and don't have to worry that something else is using this code. So having said that I would hate to introduce a pattern that I don't think is a good idea, but I'm happy to be convinced otherwise, and in this case, maybe it's the name Maybe the idea is that you're abstracting some other module in Or maybe it's something completely separated from your codebase in which case adding it to |
@cannikin Would ❤️a Rails perspective here:
|
https://mixandgo.com/learn/the-beginners-guide-to-rails-helpers Rails provides a top-level <% if current_user && current_user.active? && current_user.admin? %>
<% link_to 'Admin', admin_path %>
<% end %> You could put all those checks into a helper: module AdminHelper
def admin?
current_user && current_user.active? && current_user.admin?
end
end And then your view becomes: <% if admin? %>
<% link_to 'Admin', admin_path %>
<% end %> By default helpers are available across all views in all controllers. You can limit them, but in this particular example it's probably something you would want to be available everywhere. I'm not sure what the equivalent paradigm would be in the React world. If you want something only in a single component then you can just add a function above your component definition, but if you want it shared in other places...put a file and function in This feels like an opportunity for Redwood to introduce The Redwood Way™ to handle these kinds of helping functions. Let's bring this up with @mojombo in our next weekly meeting! |
I think we would just use a component in the React world. It's components all the way down. |
Closing the loop here after the team had a chance to discuss this. Effectively, we don't think we're ready for this yet (e.g. starting to implement helpers as a part of core features). But we wanted to point the way with a foundation for how to do this in an app (not as a generator but as a kind of util). See redwoodjs/create-redwood-app/issues/45 Further thoughts or questions before we close this one for now? Lastly, don't take this as a "stop everything". Please feel free to carry this forward in the forums and in the future. Given the ability to find and share helpers, it would be very interesting to see how and if this concept is used amongst the community. |
Closing this here. Given my previous comment, we’d still welcome continuing conversation about this in the forums (or looping back in the future). Thanks! |
If I ran something like this
It creates a new subfolder called ConvertDate in helpers directory. That have the following files,
Example files
This is the file that it generate ConvertDate.js
Here is the example test file ConvertDate.test.js
The text was updated successfully, but these errors were encountered: