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.
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.
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.
@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?
@alassek Thanks for the reply.
Even a NULL result is still helpful .
@swilgosz Thank you for your articles 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 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.
@swilgosz The obvious use-case here is analogous to a Rails Engine.
I tried it as a matter of fact. But without success. I always hit this error:
! 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:
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).
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
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 example.
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"]
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
@katafrakt 's external slices repo had a go a registering the slice using 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 to config/app.rb
and others to get everything working.
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.