hyper-react icon indicating copy to clipboard operation
hyper-react copied to clipboard

Generate and inherit from ApplicationComponent

Open sfcgeorge opened this issue 7 years ago • 5 comments

Similar to Rails by default generating an ApplicationModel < ActiveRecord::Base class which all AR generated classes inherit from User < ApplicationModel; we should generate an ApplicationComponent class and the component generator should generate components inheriting from that. E.g.

class ApplicationComponent < Hyperloop::Component
end

And then a component:

class Greeter < ApplicationComponent
  render do
    "Hello, World!"
  end
end

This way you can add useful methods to your app's ApplicationComponent without monkeypatching Hyperloop or messing around with mixins.

I've just set up this structure in our app and it works great.

sfcgeorge avatar Oct 03 '17 13:10 sfcgeorge

Thats one of the first things i did, create an ApplicationComponent. It seems very natural.

janbiedermann avatar Nov 15 '17 19:11 janbiedermann

I just checked through my 12 different component classes I have in my hyperloop app, and I don't see any common code that could be promoted to a base class like ApplicationComponent. Could you give some examples of how you use this feature, behavior common to many component classes?

explainer avatar Nov 16 '17 05:11 explainer

My view is that it is a good pattern that we should document but not be too prescriptive about. It is a very solid idea though and certainly a good one.

barriehadfield avatar Nov 16 '17 07:11 barriehadfield

@barriehadfield I'd usually agree with keep it simple out of the box, but as Rails generates ApplicationRecord and ApplicationController is seems better to be consistent.

@explainer A method I added within days to help with accessing routes: https://github.com/ruby-hyperloop/hyper-react/issues/228

sfcgeorge avatar Nov 16 '17 09:11 sfcgeorge

Another use case is with metaprogramming stuff. I'll give an example with controllers.

I wanted to autogenerate tests that every "admin_" prefixed controller included an admin authorization concern. You can loop through descendants of ApplicationController. I had an admin controller extending the RailsAdmin engine that's completely separate and I didn't want to mess with, but since it inherits from RailsAdmin::ApplicationController it's ignored anyway.

Basically having a common namespace you own that's different from what engines use helps with metaprogramming, otherwise everyone inherits from the same Hyperloop::Component and it's harder to differentiate.

It's easy enough to do add ApplicationComponent manually so this isn't an essential feature unless we deem it a beneficial default.

sfcgeorge avatar Nov 16 '17 09:11 sfcgeorge