phlex icon indicating copy to clipboard operation
phlex copied to clipboard

Initial Rails helpers

Open joeldrapper opened this issue 1 year ago • 3 comments

[WIP] @enderahmetyurt and I paired on some initial helpers for Rails today, though I’m not entirely sure how we should best include them.

  1. I don't want Phlex to be dependent on Rails, all the Rails integration should be optional.
  2. I’m not sure if we should have a Railtie or Rails Engine, but we'll likely want to extend the application with some controller methods and custom helpers.

For now, we put both features in the same file.

render_page

The idea is that this method would be available in a rails controller so if you used a consistent naming convention for your page components, you could render the correct page automatically.

class ArticlesController < ApplicationController
  def index
    render_page articles: Article.all
  end
end

component

The component method is designed to be used in ERB templates so you can render a Phlex component from your existing Rails views.

joeldrapper avatar Jul 19 '22 13:07 joeldrapper

@joeldrapper, maybe we could have two different gems:

  • phlex could contain the logic for creating components. This gem would be independent of any framework.
  • phlex-rails would contain the Rails-specific modules and the Engine/Railtie.

It could be just like factory_bot and factory_bot_rails. What do you think?

alexandreruban avatar Jul 19 '22 16:07 alexandreruban

@alexandreruban that's a really good option. It's what I did with literal_enums and literal_enums-rails. The alternative is to have a rails.rb file that can be optionally required. I think you could just do this in the Gemfile.

gem "phlex", require: "phlex/rails"

This wouldn't make sense if we end up needing a dummy Rails app for testing but I don't know if that's ever going to be necessary. 🤔

joeldrapper avatar Jul 19 '22 16:07 joeldrapper

I think it would be easier to extract a phlex-rails gem later than it would be to merge a separate gem back into the main one, so my instinct is to see how far we can get with just one gem while always keeping that door open. I don't feel strongly about this though.

joeldrapper avatar Jul 19 '22 16:07 joeldrapper