# Hanami 2.0 Ideas

**URL:** <https://discourse.hanakai.org/t/hanami-2-0-ideas/306>\
**Category:** proposals\
**Created:** [March 9, 2017, 4:03pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306 "2017-03-09T16:03:52Z")\
**Posts on this page:** 17\
**Page:** 3

<div class="post-metadata">

**Author:** ![davydovanton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/davydovanton/32/162_2.png) [@davydovanton](https://discourse.hanakai.org/u/davydovanton)\
**Post date:** [April 13, 2017, 1:59pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/41 "2017-04-13T13:59:23Z")

</div>

## Container for application

We can create the global container for all hanami application. It will be super useful for initializers, some service objects, etc. In my mind I’m thinking about something like this:

```ruby
# config/initializers/redis.rb

AppName::Container.register :redis do
  ConnectionPool.new(size: 10, timeout: 3) { Redis.new(host: 'localhost', port: 6379) }
end

# spec/spec_helper.rb

AppName::Container.register :redis do
  ConnectionPool.new(size: 10, timeout: 3) { MockRedis.new }
end

# in action, view, repository, etc

module Web::Controllers::Contributors
  class Show
    include Web::Action

    def call(params)
      user = container[:redis].with { |c| c.get(:key) }
    end
  end
end

```

In this example `container[:redis]` is not a part of hanami. Developer should define this container for his application by self.

### Why containers will good for developers:

1. you can set special container for each envs
2. you will have general way for working with shared code
3. hanami-gems developers will have the general way to share gem code in hanami app

## Ideas where you can use it now

1. `container['interactors.interactor_name']`
2. Any code in `/config/initialization/ folder (for example I have`Markdown` class for converting md to html)
3. some api clients without global configuration (`twitter = Twitter.new(config); twitter.tweet('text')`)
4. any services from `lib/` folder

---

<div class="post-metadata">

**Author:** ![jodosha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/jodosha/32/710_2.png) [@jodosha](https://discourse.hanakai.org/u/jodosha)\
**Post date:** [April 14, 2017, 12:57pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/42 "2017-04-14T12:57:56Z")

</div>

@davydovanton I agree and I already outlined this proposal (see [Hanami 2.0 Ideas](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/2), “IoC” section).

We already have a working implementation: `Hanami::Components`, which works very well. It’s a matter of enhance it a bit and mark as Public API. The plan is to rename it too.

---

<div class="post-metadata">

**Author:** ![davydovanton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/davydovanton/32/162_2.png) [@davydovanton](https://discourse.hanakai.org/u/davydovanton)\
**Post date:** [May 19, 2017, 12:05pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/43 "2017-05-19T12:05:24Z")

</div>

## GraphQL plugin for hanami

I’m not sure about this idea but I think we can create hanami-graphql plugin for graphql endpoint in hanami. This is a just idea and I’ll be happy to discuss it.

Some links:

> **[rmosolgo/graphql-ruby](https://github.com/rmosolgo/graphql-ruby)**
>
> Ruby implementation of GraphQL . Contribute to rmosolgo/graphql-ruby development by creating an account on GitHub.

  

> **[Relay/GraphQL On Rails](https://medium.com/react-weekly/relay-facebook-on-rails-8b4af2057152)**
>
> Summary: If you are excited about just declaring data required by your UI components while leaving the difficult parts of data fetching…

---

<div class="post-metadata">

**Author:** ![davydovanton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/davydovanton/32/162_2.png) [@davydovanton](https://discourse.hanakai.org/u/davydovanton)\
**Post date:** [July 17, 2017, 11:04pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/44 "2017-07-17T23:04:49Z")

</div>

## Register options for Hanami::Components

[Inspired by dry-containers](http://dry-rb.org/gems/dry-container/registry-and-resolver/)

---

<div class="post-metadata">

**Author:** ![jodosha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/jodosha/32/710_2.png) [@jodosha](https://discourse.hanakai.org/u/jodosha)\
**Post date:** [July 22, 2017, 9:16am UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/45 "2017-07-22T09:16:49Z")

</div>

As followup on the CLI state: we implemented [`hanami-cli`](https://github.com/hanami/cli) gem from scratch. It’s a replacement for `thor`, and it can be used to build CLIs with Ruby.

Based on that gem, we rewrote from scratch `hanami` CLI (aka all the `hanami server` commands et all). [https://github.com/hanami/hanami/pull/808](https://github.com/hanami/hanami/pull/808)

It cuts down the maintenance burden for us, and it allows **third-party developers to add their own generators and CLI commands**.

**This feature will be available as preview from August 2017 with `hanami-1.1.0.beta1`.**

---

<div class="post-metadata">

**Author:** ![jodosha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/jodosha/32/710_2.png) [@jodosha](https://discourse.hanakai.org/u/jodosha)\
**Post date:** [July 22, 2017, 9:20am UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/46 "2017-07-22T09:20:37Z")

</div>

I see GraphQL as third-party gem, not in the core of the framework.

---

<div class="post-metadata">

**Author:** ![davydovanton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/davydovanton/32/162_2.png) [@davydovanton](https://discourse.hanakai.org/u/davydovanton)\
**Post date:** [July 22, 2017, 9:32am UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/47 "2017-07-22T09:32:51Z")

</div>

yep, only third-party gem without changing core lib 👍

---

<div class="post-metadata">

**Author:** ![davydovanton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/davydovanton/32/162_2.png) [@davydovanton](https://discourse.hanakai.org/u/davydovanton)\
**Post date:** [July 31, 2017, 12:26pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/48 "2017-07-31T12:26:18Z")

</div>

After discussion with @jodosha, we decided that this library should look like another gem.  
Also, we think that hanami-events should use different adapters and be without a global state.

## Why different adapters?

I think hanami events should be a library for building [Event-Driven Architecture](https://www.youtube.com/watch?v=STKCRSUsyP0). In this case, a library should work with different hanami project instances:

 ![screenshot 2017-07-29 15 27 13](https://canada1.discourse-cdn.com/flex030/uploads/hanamirb/original/1X/0e160729b9bd2db8e563f961994d35654ef08eb2.jpeg)

In this case, we can provide different transport layers for a event storage like:

- Memory
- Postgres
- Kafka
- Kinesis
- etc

And that’s why we need to replace wispers gem as an adapter.

## From which hanami events should be?

I think that we need to split all logic to 3 different parts:

1. Broadcaster
2. Subscriber
3. Routing

All these parts should be without a global state. And here my ideas about API for all this.

### Broadcaster

```ruby
events = Hanami::Events.new(:adapter)
events.broadcast('user.created', user: user)

```

### Subscriber

```ruby
kafka_events = Hanami::Events.new(:kafka)
memory_events = Hanami::Events.new(:memory)

memory_events.subscribe('user.signup', Mailer)

# or in class
class Signup
  inclide Hanami::Events::Subscriber
  subscribe_to memory_events, 'user.signup'
  subscribe_to kafka_events, 'user.signup'

  def call(payload)
    # ...
  end
end

```

Also, I think we should have an ability for subscribing to all events:

```ruby
kafka_events = Hanami::Events.new(:kafka)

kafka_events.listen do |event|
  event.name # => 'user.signup'
  event.payload # => { ... }
end

```

### Routing

Routing should route all events and call specific code for each one. It will be helpful for manage all events in one place (look like HTTP routers):

```ruby
class WebHandler < Hanami::Events::Handler
  on('user.created') { |payload| UserRepository.new.create(payload) }
  on('user.updated') { |payload| payload }
end

class AnaliticHandler < Hanami::Events::Handler
  on('*', AnaliticSender)
  on('user.*') { |payload| payload }
  on('user.signup') { |payload| payload }
end

class NotificationHandler < Hanami::Events::Handler
  on('user.created') { |payload| payload }
  on('post.created') { |payload| payload }
end

class EventRouting < Hanami::Events::Routing
  mount UserHandler, Hanami.app?(:user_events) # load specific handlers for instance
  mount AnaliticHandler
  mount NotificationHandler
end

events = Hanami::Events.new(:adapter)
events.listen { |event| EventRouter.new.resolve(event) }

```

## Plan

We will start work on proof of concept for this idea. It’s mean that we will implement only broadcaster and simple subscriber without routing and other features.

## Links

- [https://github.com/hanami/utils/pull/222](https://github.com/hanami/utils/pull/222)
- [https://github.com/hanami/events](https://github.com/hanami/events)

---

<div class="post-metadata">

**Author:** ![titeiko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/titeiko/32/257_2.png) [@titeiko](https://discourse.hanakai.org/u/titeiko)\
**Post date:** [August 11, 2017, 9:00am UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/49 "2017-08-11T09:00:58Z")

</div>

## Remove view escaping dark magic

```ruby
class Web::Views::Foo::Index
  include Web::View

  def date
    "10/10/10"
  end
end

def test_view_method
  view = Web::Views::Foo::Index.new('apps/web/templates/foo/index.html.slim', Hash[])
  assert_equal "10/10/10", view.date
end

```

```auto
--- expected
+++ actual
@@ -1,2 +1,2 @@
-# encoding: UTF-8
-"10/10/10"
+# encoding: ASCII-8BIT
+"10&#x2F;10&#x2F;10"

```

This is because of this 🧙

> <https://github.com/hanami/view/blob/master/lib/hanami/view/escape.rb#L208>

### Template engines

- `slim`: escapes by default (`==` to unescape)
- `haml` with `:escape_html` option: escapes by default (`!=` to unescape)
- `erb`: doesn’t escape

### Proposal

Stop using `ERB` to parse `.erb` templates, instead, use [`Erubi`](https://github.com/jeremyevans/erubi), which has an `:escape` option, turning `<%=` to escaped and `<%==` to unescaped, thus rendering the 🧙 useless for all 3 main template file formats.

---

<div class="post-metadata">

**Author:** ![amerov](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/amerov/32/337_2.png) [@amerov](https://discourse.hanakai.org/u/amerov)\
**Post date:** [March 30, 2018, 7:16pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/50 "2018-03-30T19:16:47Z")

</div>

Get ride of controller callbacks and use Laravel like aproach.

Examples:  
Middleware for route groups [https://laravel.com/docs/5.6/authentication#protecting-routes](https://laravel.com/docs/5.6/authentication#protecting-routes), [https://laravel.com/docs/5.6/authorization#via-middleware](https://laravel.com/docs/5.6/authorization#via-middleware)  
Model binding [https://laravel.com/docs/5.6/routing#route-model-binding](https://laravel.com/docs/5.6/routing#route-model-binding)

---

<div class="post-metadata">

**Author:** ![hachi8833](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/hachi8833/32/338_2.png) [@hachi8833](https://discourse.hanakai.org/u/hachi8833)\
**Post date:** [April 3, 2018, 12:36pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/51 "2018-04-03T12:36:15Z")

</div>

Switching to Roda sounds nice!

---

<div class="post-metadata">

**Author:** ![amerov](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/amerov/32/337_2.png) [@amerov](https://discourse.hanakai.org/u/amerov)\
**Post date:** [April 4, 2018, 7:15pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/52 "2018-04-04T19:15:37Z")

</div>

freeze object after expose 😉 and make view object immutable.

---

<div class="post-metadata">

**Author:** ![jc00ke](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/jc00ke/32/422_2.png) [@jc00ke](https://discourse.hanakai.org/u/jc00ke)\
**Post date:** [August 15, 2018, 5:27pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/53 "2018-08-15T17:27:15Z")

</div>

I’d like to have the ability to override public methods in a repository. See my comment [here](https://github.com/hanami/model/issues/349#issuecomment-413269043) in an old issue.

I think it’d also be worth adding better support for `UUID` primary keys. A [recent change](https://github.com/hanami/hanami.github.io/pull/475) to the guides is a great start, but I realized after I proposed that change that default orderings will no longer work. Take `FooRepository#first` for example: it will sort randomly generated `UUID`'s and return the first. That doesn’t make sense with a randomly generated primary key. Maybe this could be handled in the generator, where it could add implementations of `#first` and `#last`that `order(:created_at)`?

Lastly, I’d love to see more attention brought to vendored assets. [This](https://github.com/hanami/assets/pull/88) was a great start but lacks support for vendored assets used across multiple apps. I had to create my own S3/CloudFront-based CDN to vendor these assets.

---

<div class="post-metadata">

**Author:** ![ylluminate](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/ylluminate/32/346_2.png) [@ylluminate](https://discourse.hanakai.org/u/ylluminate)\
**Post date:** [November 4, 2018, 6:05am UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/54 "2018-11-04T06:05:13Z")

</div>

Are these being implemented anywhere? I’m not seeing any branches on Github with anything here really being implemented as of yet…

---

<div class="post-metadata">

**Author:** ![jodosha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/jodosha/32/710_2.png) [@jodosha](https://discourse.hanakai.org/u/jodosha)\
**Post date:** [November 6, 2018, 2:00pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/55 "2018-11-06T14:00:58Z")

</div>

A couple of weeks ago we [released](http://hanamirb.org/blog/2018/10/24/announcing-hanami-130.html) v1.3, which marks the end of the 1.x series.

We’re jumping now on 2.x series. If you’re interested, please check `unstable` branches of all the repositories.

---

<div class="post-metadata">

**Author:** ![jodosha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/jodosha/32/710_2.png) [@jodosha](https://discourse.hanakai.org/u/jodosha)\
**Post date:** [November 6, 2018, 2:10pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/56 "2018-11-06T14:10:45Z")

</div>

I’m gonna close the conversation here, as we started planning `v2.0.0.alpha1`. [Roadmap for v2.0.0.alpha1](https://discourse.hanakai.org/t/roadmap-for-v2-0-0-alpha1/475)

---

<div class="post-metadata">

**Author:** ![jodosha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/jodosha/32/710_2.png) [@jodosha](https://discourse.hanakai.org/u/jodosha)\
**Post date:** [November 6, 2018, 2:10pm UTC](https://discourse.hanakai.org/t/hanami-2-0-ideas/306/57 "2018-11-06T14:10:59Z")

</div>



[Previous page](https://discourse.hanakai.org/t/hanami-2-0-ideas/306.md?page=2)
