Skip to content
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

Insert load hooks for collaborator gems #335

Merged
merged 3 commits into from
Jul 18, 2023
Merged

Insert load hooks for collaborator gems #335

merged 3 commits into from
Jul 18, 2023

Conversation

kaspth
Copy link
Contributor

@kaspth kaspth commented Jul 10, 2023

This means collaborator gems, particularly the bullet_train-billing(-*) ones, can hook in and help define extra functionality. Here's how that would look:

module BulletTrain::Billing::Teams::Base
  extend ActiveSupport::Concern

  included do
    has_many :billing_subscriptions, class_name: "Billing::Subscription", dependent: :destroy, foreign_key: :team_id
  end
end

ActiveSupport.on_load(:bullet_train_teams_base) { include BulletTrain::Billing::Teams::Base }

Collaborator gems must use concerns otherwise features won't be included into the class that includes ::Teams::Base. That's because regular Ruby modules don't support this kind of deep nesting but concerns have specifically added that.

Note: this is also how Rails works internally and expects you to hook into its libraries, like Active Record.

How to ship this

kaspth added a commit to bullet-train-pro/bullet_train-billing that referenced this pull request Jul 10, 2023
This work is dependent on bullet-train-co/bullet_train-core#335 being
shipped.

Then we're just grabbing the bullet_train-billing specific extensions and pulling them in to our
repo. Note: we can skip the surrounding `billing_enabled?` checks, since we already know its enabled
by the fact that we're loading the very library.
@kaspth
Copy link
Contributor Author

kaspth commented Jul 10, 2023

Took me a bit to figure out locally, but I got the bullet_train-billing version loaded and working with this bullet_train version in a starter repo. I confirmed that the load hook is fired and then used by billing to insert the functionality. Note: I removed the existing billing extensions in Teams::Base to verify the new behavior.

This means collaborator gems, particularly the bullet_train-billing(-*) ones, can hook in and help define extra
functionality. Here's how that would look:

```ruby
module BulletTrain::Billing::Teams::Base
  extend ActiveSupport::Concern

  included do
    has_many :billing_subscriptions, class_name: "Billing::Subscription", dependent: :destroy, foreign_key: :team_id
  end
end

ActiveSupport.on_load(:bullet_train_teams_base) { include BulletTrain::Billing::Teams::Base }
```

Collaborator gems must be concerns otherwise features won't be included into the class that includes `::Teams::Base`.
That's because regular Ruby modules don't support this kind of deep nesting but concerns have specifically added that.

Note: this is also how Rails works internally and expects you to hook into its libraries, like Active Record.
@kaspth
Copy link
Contributor Author

kaspth commented Jul 10, 2023

Rebased out eb622ba now that master doesn't have that anymore.

kaspth added a commit to bullet-train-pro/bullet_train-billing-stripe that referenced this pull request Jul 10, 2023
This work is dependent on bullet-train-co/bullet_train-core#335 being
shipped.

Then we're just grabbing the bullet_train-billing-stripe specific extensions and pulling them in to our
repo. Note: we can skip the surrounding `defined?(Billing::Stripe::Subscription)` checks, since we already know its enabled
by the fact that we're loading the very library.
@kaspth kaspth changed the title Insert load hook into Teams::Base for collaborator gems Insert load hooks for collaborator gems Jul 10, 2023
@kaspth
Copy link
Contributor Author

kaspth commented Jul 10, 2023

Found more places we're extending these concerns with other modules. So I put in extra load hooks and I'll do a double check on the collaborator gems again.

Edit: just bullet_train-billing needed updating.

kaspth added a commit to bullet-train-pro/bullet_train-billing that referenced this pull request Jul 17, 2023
This work is dependent on bullet-train-co/bullet_train-core#335 being
shipped.

Then we're just grabbing the bullet_train-billing specific extensions and pulling them in to our
repo. Note: we can skip the surrounding `billing_enabled?` checks, since we already know its enabled
by the fact that we're loading the very library.
kaspth added a commit to bullet-train-pro/bullet_train-billing-stripe that referenced this pull request Jul 18, 2023
This work is dependent on bullet-train-co/bullet_train-core#335 being
shipped.

Then we're just grabbing the bullet_train-billing-stripe specific extensions and pulling them in to our
repo. Note: we can skip the surrounding `defined?(Billing::Stripe::Subscription)` checks, since we already know its enabled
by the fact that we're loading the very library.
@kaspth kaspth merged commit 843f0a5 into main Jul 18, 2023
4 checks passed
@kaspth
Copy link
Contributor Author

kaspth commented Jul 18, 2023

Note to self: don't delete the branch yet, the starter repo still relies on it.

kaspth added a commit to bullet-train-pro/bullet_train-billing that referenced this pull request Aug 2, 2023
* Use Teams::Base load hook to insert functionality

This work is dependent on bullet-train-co/bullet_train-core#335 being
shipped.

Then we're just grabbing the bullet_train-billing specific extensions and pulling them in to our
repo. Note: we can skip the surrounding `billing_enabled?` checks, since we already know its enabled
by the fact that we're loading the very library.

* Extract remaining Billing extensions to our repo

* Add an AbilitySupport load hook for other billing libraries

* Add hook to inject provider_subscription_attributes
kaspth added a commit to bullet-train-pro/bullet_train-billing-stripe that referenced this pull request Aug 2, 2023
* Use Teams::Base load hook to insert functionality

This work is dependent on bullet-train-co/bullet_train-core#335 being
shipped.

Then we're just grabbing the bullet_train-billing-stripe specific extensions and pulling them in to our
repo. Note: we can skip the surrounding `defined?(Billing::Stripe::Subscription)` checks, since we already know its enabled
by the fact that we're loading the very library.

* Use AbilitySupport load hook to insert functionality

* Hook into BulletTrain::Billing.provider_subscription_attributes

* Hide behind compatibility hook for backwardscompatibility
kaspth added a commit that referenced this pull request Oct 2, 2023
We inserted load hooks in #335,
and then used them in bullet-train-pro/bullet_train-billing#19

We've shipped releases with that new code, so we can remove our inline backing code from here.
kaspth added a commit that referenced this pull request Oct 2, 2023
We inserted load hooks in #335,
and then used them in bullet-train-pro/bullet_train-billing#19

We've shipped releases with that new code, so we can remove our inline backing code from here.
jagthedrummer pushed a commit that referenced this pull request Oct 2, 2023
We inserted load hooks in #335,
and then used them in bullet-train-pro/bullet_train-billing#19

We've shipped releases with that new code, so we can remove our inline backing code from here.
jagthedrummer pushed a commit that referenced this pull request Oct 2, 2023
We inserted load hooks in #335,
and then used them in bullet-train-pro/bullet_train-billing#19

We've shipped releases with that new code, so we can remove our inline backing code from here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants