maybe-finance / marketing

The marketing site for Maybe
https://maybe.co
GNU Affero General Public License v3.0
17 stars 30 forks source link

Refactor mini-tools #134

Open josefarias opened 1 week ago

josefarias commented 1 week ago

WIP — 95% done

You'll notice some commented code, which I plan to refactor away. There are three tools to go:


closes https://github.com/maybe-finance/marketing/issues/133

This PR refactors the marketing site's mini-tools to follow Hotwire and Rails conventions.

Previously, the tools used a bespoke template rendering system instead of Hotwire (Turbo)'s built-in frames. Leveraging Hotwire makes the code:

Also, all calculations were previously done on the client. They're now happening on the back-end. This makes the code:

Some JS cleanup was done in passing. But I decided not to make it a priority because the scope is already large enough and the existing code is not significantly breaking convention. There's room for improvement, but it's less crucial than the other problems addressed by this PR. That means there will still be work to be done on JS after merging. Most notably, the time series charts have not yet been refactored to be reusable — they remain coupled to specific tools.

Finally, I've removed code comments — which seem to have been added recently, and intentionally. I'm happy to bring those back, but I want to make the case that code comments add a maintainability burden in keeping them up to date. Ideally, the code should be readable enough that comments are mostly not necessary.

[!IMPORTANT] This touches every single mini-tool. We should do some manual QA on every tool to ensure consistency.

Steps to add a new tool (for future reference)

  1. Add tool to db/seeds/seeds.rb
  2. Run bin/rails db:seed
  3. Create a presenter under app/presenters/tool.
    1. Class name must match the tool's slug.
    2. All tool presenters must implement the #active_record method to find their corresponding tool in the DB.
    3. See neighboring presenters for reference.
  4. Create a partial under app/views/tools/widgets
    1. Partial name must match the tool's slug.
    2. Currently, all tools are further broken down in app/views/tools/widgets/forms and app/views/tools/widgets/content. It's a nice pattern, but not mandatory.
    3. See neighboring partials for reference.
  5. Allow-list the presenter's params in ToolsController#tool_params.
  6. You're set! Use ruby to implement business logic in the presenter. Render as usual:
    1. <%= tool.some_financial_indicator %>
    2. <%= number_with_delimiter(tool.some_financial_indicator) %>
    3. <%= number_to_currency(tool.some_financial_indicator) %>
zachgoll commented 1 week ago

🚀 🚀