Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Generate and inherit from ApplicationComponent #227

Open

Description

@sfcgeorge

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.

Activity

  1. janbiedermann commented on Nov 15, 2017

    @janbiedermann
    Contributor

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

  2. explainer commented on Nov 16, 2017

    @explainer

    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?

  3. barriehadfield commented on Nov 16, 2017

    @barriehadfield
    Member

    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.

  4. sfcgeorge commented on Nov 16, 2017

    @sfcgeorge
    Author

    @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: #228

  5. sfcgeorge commented on Nov 16, 2017

    @sfcgeorge
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions