Save 37% off PRO during our Black Friday Sale! »

Introduction to Hanami

Introduction to Hanami

Hanami is a great Ruby MVC framework and this is a brief intro on the M, V, and C of Hanami

2762ab764460eb605d9d5033314aa2e5?s=128

Gabriel Gizotti

February 21, 2017
Tweet

Transcript

  1. None
  2. What's Hanami?

  3. None
  4. What's Hanami in our context? • A Ruby Web MVC

    Framework • Create by Luca Guidi - @jodosha • Simple API • Minimal DSL • PORO over magic classes • Lightweight • Threadsafe
  5. Architecture • Clean Architecture • https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean- architecture.html • Enforces separation

    of concerns • Monolith First • https://martinfowler.com/bliki/MonolithFirst.html • Build as one app at first, separate the modules later
  6. Building a blog • Hanami 0.9.2 • 1.0.0.beta just released

    • Creating new project • Blog Post • Entity • Repository • Controller • Views • Templates
  7. Creating a new project • gem install hanami • hanami

    new hanami_blog –database=postgres –template=haml – test=rspec • Will build the skeleton of the app and basic config based on the options you passed. Very similar to Rails.
  8. Project Structure • Apps folder • Where the code for

    each "service" is located • Holds specific, assets, controllers, views, templates routes, and config for a 'service' • Config folder • Holds initializers and environment configuration • DB folder • Holds migration files and schema file
  9. Project Structure • Lib folder • Holds models, mailers, and

    any other piece if cold shared among apps • Spec folder • Hold test files for either Rspec or minitest
  10. Post Model • bundle exec hanami g model post •

    Creates Post entity and Post repository • Bundle exec hanami g migration create_posts • Creates a migration file to create the posts table • Manually write migration and run hanami db prepare • From Hanami 1.0 migration will be created as part of the model generator
  11. Hanami Model • Entity • The domain domain object •

    Responsible for the domain logic • The core of the application • Repository • An object that mediates between entities and persistence layer • Storage independent • Exposes just a few query methods
  12. Post Controller - Index • bundle exec hanami g action

    web posts#index • Creates Posts#index action class and spec • Creates Posts index view class • Creates posts/index.html.haml template • Inserts get '/posts', to: 'posts#index' to routes.rb • Everything is created in the context of the Web app
  13. Hanami Controller • Hanami Controller is a Ruby module •

    Each action is its own class with a call method • Sets status code and the headers for the response • Differently from Rails, the router instantiates the action itself and send the call method to it.
  14. Hanami View • The output of the view is the

    body of the action response • Is used as a context for the templates. All methods declared in the view are available to the template • It exposes helpers, like the form, and routes • Can be used to simplify templates
  15. Template • What we call a View in Rails •

    It describes the body of a response • ERB is the default engine, but a number of engines are supported
  16. Posts Controller – New • Same as creating the Index

    action • bundle exec hanami g action web posts#new
  17. Posts Controller - Create • bundle exec hanami g action

    web posts#create –method=post • Will append a post route to the web routes file • Will create the action and view classes, and their specs
  18. Validations • Validations can be added to the entity •

    Validations can also be added to controllers to validate the passed params • Useful when an entity has 2 or more different validation, depending on workflow
  19. Resources • http://hanamirb.org/guides/ • https://gitter.im/hanami/chat • https://github.com/davydovanton/awesome-hanami • https://github.com/gizotti/hanami_blog