HanamiMastery

Leverage ROM Repositories

Episode #29
PRO

by Sebastian Wilgosz

Picture of the author

In the last episode, I've shown you how to configure the persistence layer from scratch in Hanami applications. That was fun, however, we ended up using only relations.

Relations are the heart of ROM, but this is only a single piece of the whole ORM. Using relations alone in a bigger project would not be too convenient so it makes sense to improve on it.

We are Ruby developers, aren't we? And Ruby was designed for developers' happiness!

In this episode, we'll go one step further, by extending our current application by adding repositories to the system.

Convenience.

I start with the code from the previous example, where I have just a single action defined to list my articles.

module Sandbox
  module Actions
    module Articles
      class Index < Action
        include Deps['persistence.rom']

        def handle(req, res)
          res.status = 200
          relation = rom.relations[:articles].combine(:author)
          res.body = serialize(relation)
        end

        private

        def serialize(collection)
          collection.to_a.to_json
        end
      end
    end
  end
end

You may notice, that I've added the serialize method to clean the code just a little bit.

Still, however, there are some problems with this approach.

When I want to list articles with authors, I need to explicitly tell my relation, loaded from the ROM container, to join the authors table. I can see two problems here.

  • First, I depend on ROM to give me the proper relation
  • Secondly, I have the data fetching logic leaked into the action.

I bet that You've never heard good words about fat controllers in Rails. This is also true in Hanami - we want to keep our actions slim and skinny, and hide the persistence logic if possible.

This is what repositories are for.

Subscribe to Hanami Mastery PRO

This is only a preview of the episode. Subscribe to Hanami Mastery PRO to see the full video, and access several other premium resources. Thanks for watching, and see you in the next one!

Thanks

In the next PRO episode, we'll talk more about mapping the structs to different entity classes, for better data presentation management.

That's all for today!

Thank you for being a part of this community and watching this content, without your support this whole initiative would never be possible.

See you in the next episode!

Do you know great Ruby gems?

Add your suggestion to our discussion panel!

I'll gladly cover them in the future episodes! Thank you!

Suggest topicTweet #suggestion

May also interest you...

#32 Pagination in Hanami apps with ROM
hanamirom-rbpagination

There are plenty of popular ways to handle pagination in Ruby apps. But did you know, that ROM supports Pagination out of the box? And so Hanami does? In this episode, I'll show you how to quickly implement pagination from scratch for your Hanami projects.

#30 ROM - Mapping is everythingPRO

Understand data mapping in ROM, on all levels! In this episode we go through examples of simple to complex data mapping with ROM, with real usecases for each one.

Hanami 2.0 comes without the persistence layer nor views preconfigured. It is useful then to know how to set up the best ORM available in the Ruby ecosystem.

dry-system and dry-container are extremely powerful tools and if you understand how to work with them, you'll never look back. It's amazing that Hanami uses them by default! Check out some useful debugging tips!

Coffee buy button
Trusted & Supported by
DNSimple

1 / 2
Open Hanami Jobs

We use cookies to improve your experience on our site. To find out more, read our privacy policy.