# Is there a guide to distribute slice as a gem?

**URL:** <https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038>\
**Category:** questions\
**Created:** [October 8, 2024, 1:44am UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038 "2024-10-08T01:44:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lgflorentino](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/lgflorentino/32/669_2.png) [@lgflorentino](https://discourse.hanakai.org/u/lgflorentino)\
**Post date:** [October 8, 2024, 1:44am UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038/1 "2024-10-08T01:44:12Z")

</div>

Hi,

I’m looking for any doco or guides about distributing an app as a Hanami slice via a Ruby Gem. Could anybody provide a link or some info about doing this ?

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/alassek/32/532_2.png) [@alassek](https://discourse.hanakai.org/u/alassek)\
**Post date:** [October 8, 2024, 4:55pm UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038/2 "2024-10-08T16:55:35Z")

</div>

I’m not aware of anyone who has done this, although I can’t think of any reason why this wouldn’t be possible. I’m pretty certain there’s no documentation presently.

Tim is on vacation with his family for a couple weeks, and he is the author of this system. When he returns perhaps he has some advice for doing this. I would definitely like to see this documented officially.

---

<div class="post-metadata">

**Author:** ![swilgosz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/swilgosz/32/484_2.png) [@swilgosz](https://discourse.hanakai.org/u/swilgosz)\
**Post date:** [October 8, 2024, 11:03pm UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038/3 "2024-10-08T23:03:28Z")

</div>

@lgflorentino could you share a bit more about use case? Each hanami slice is almost identical as an app itself - rack-compatible ruby piece.

Do you want to install & load a slice from a gem and use in your app?

---

<div class="post-metadata">

**Author:** ![lgflorentino](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/lgflorentino/32/669_2.png) [@lgflorentino](https://discourse.hanakai.org/u/lgflorentino)\
**Post date:** [October 9, 2024, 12:03am UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038/4 "2024-10-09T00:03:37Z")

</div>

@alassek Thanks for the reply.  
Even a NULL result is still helpful 🙂.

@swilgosz Thank you for your [articles](https://hanamimastery.com/episodes/28-configure-rom-from-scratch) on rom-rb, they are very handy to refer to.  
I was hoping for the reverse actually. I would like to distribute a gem and users of the gem could integrate it into their own app as a slice with a bit of configuration via the [Provider](https://guides.hanamirb.org/v2.2/app/providers/) subsystem. From the official docs this appears to be the most logical way to achieve what I want.  
Currently I have a minimal rack app with some api routes which accept webhook JSON data from third party services and a rom-rb persistance layer to store it. I don’t wish to do any sort of View or UI. The display of the data will be up to the user.

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/alassek/32/532_2.png) [@alassek](https://discourse.hanakai.org/u/alassek)\
**Post date:** [October 9, 2024, 12:09am UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038/5 "2024-10-09T00:09:06Z")

</div>

@swilgosz The obvious use-case here is analogous to a Rails Engine.

---

<div class="post-metadata">

**Author:** ![katafrakt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/katafrakt/32/735_2.png) [@katafrakt](https://discourse.hanakai.org/u/katafrakt)\
**Post date:** [October 13, 2024, 3:45pm UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038/6 "2024-10-13T15:45:07Z")

</div>

I tried it as a matter of fact. But without success. I always hit this error:

```auto
! Unable to load application: Hanami::AppLoadError: Hanami.app is not yet configured. You may need to `require "hanami/setup"` to load your config/app.rb file.
/home/katafrakt/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/hanami-2.2.0.beta2/lib/hanami.rb:87:in `block in app': Hanami.app is not yet configured. You may need to `require "hanami/setup"` to load your config/app.rb file. (Hanami::AppLoadError)

```

Which is weird, because it’s the same setup that is actually tested in the repo:

```auto
module HanamiApplication
  class App < Hanami::App
    register_slice(:external_slice, ExternalSlice::Slice)     
  end
end

```

So either I’m missing something simple or there is some kind of weird behaviour here.

I do think it would be _extremely_ useful to be able to distribute slices as gems for things like automatic admin panels or even fully-fledged authentication solution like Devise (not that I like the idea, but it seems to be something the community expects).

---

<div class="post-metadata">

**Author:** ![katafrakt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/katafrakt/32/735_2.png) [@katafrakt](https://discourse.hanakai.org/u/katafrakt)\
**Post date:** [October 13, 2024, 6:20pm UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038/7 "2024-10-13T18:20:05Z")

</div>

Nevermind, this was me being dumb. I actually read the error and managed to progress (so, great rubber-ducking y’all). I don’t have a tutorial or anything - maybe in the future. But meanwhile I can share the repository with example of having a slice in a gem: [katafrakt/slice-as-gem-example - Codeberg.org](https://codeberg.org/katafrakt/slice-as-gem-example)

---

<div class="post-metadata">

**Author:** ![lgflorentino](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/lgflorentino/32/669_2.png) [@lgflorentino](https://discourse.hanakai.org/u/lgflorentino)\
**Post date:** [October 14, 2024, 2:31am UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038/8 "2024-10-14T02:31:24Z")

</div>

Nice one! Thanks for sharing the repo, I’ll take a look.

Speaking of admin panels, backpack for laravel would be a much more complex PHP example of this use case.  
I’m not familiar with Rails Engines but from a quick browse, I would agree with @alassek .

edit:  
Thanks, that repo is working for me, I ran that code against the [docs](https://guides.hanamirb.org/v2.2/app/slices/) example.

```auto
bundle exec hanami console
hanami_application[development]> ExternalSlice::Slice.boot
=> ExternalSlice::Slice
hanami_application[development]> ExternalSlice::Slice.keys
=> ["routes", "assets", "actions.test.index", "version", "views.test.index", "inflector", "logger", "notifications", "rack.monitor", "settings"]

```

---

<div class="post-metadata">

**Author:** ![lgflorentino](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/lgflorentino/32/669_2.png) [@lgflorentino](https://discourse.hanakai.org/u/lgflorentino)\
**Post date:** [October 26, 2024, 2:31am UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038/9 "2024-10-26T02:31:26Z")

</div>

Is the strategy described in the specs the only way to register an external slice ?  
[hanami/spec/integration/slices/external\_slice\_spec.rb at main · hanami/hanami · GitHub](https://github.com/hanami/hanami/blob/main/spec/integration/slices/external_slice_spec.rb)

@katafrakt 's external slices repo had a go a registering the slice using providers  
[external\_slice.rb#L7](https://codeberg.org/katafrakt/slice-as-gem-example/src/commit/9067623bfe7d9003fe630592f59b2ec71cba065d/hanami_application/config/providers/external_slice.rb#L7)

I’m just wondering if there were any other ways of registering slices. Using a self contained provider file means one can automate creating the provider file from a template as opposed to inserting [lines](https://codeberg.org/katafrakt/slice-as-gem-example/src/commit/9067623bfe7d9003fe630592f59b2ec71cba065d/hanami_application/config/app.rb#L7) to `config/app.rb` and [others](https://codeberg.org/katafrakt/slice-as-gem-example/src/commit/9067623bfe7d9003fe630592f59b2ec71cba065d/hanami_application/config/routes.rb#L5) to get everything working.

---

<div class="post-metadata">

**Author:** ![katafrakt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/katafrakt/32/735_2.png) [@katafrakt](https://discourse.hanakai.org/u/katafrakt)\
**Post date:** [October 29, 2024, 11:12am UTC](https://discourse.hanakai.org/t/is-there-a-guide-to-distribute-slice-as-a-gem/1038/10 "2024-10-29T11:12:03Z")

</div>

I don’t think it’s a good expectation that external slices will automatically add something to my application router. I wouldn’t like that and I don’t think I know a framework that allows that.

As for extra `require` in `app.rb`, I’m pretty sure you gen get rid of it with smart usage of `autoload` or Zeitwerk. I can look into it when I have time.
