Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hanami for Rails Developers

Hanami for Rails Developers

Slides from Rzeszow Ruby User Group talk

Paweł Cyło

June 27, 2017
Tweet

More Decks by Paweł Cyło

Other Decks in Programming

Transcript

  1. HANAMI FOR RAILS DEVELOPERS AGENDA ▸ What is Hanami? ▸

    Architecture and key features ▸ Container architecture ▸ Models ▸ Views and templates ▸ Controllers and actions ▸ Practical basics ▸ What’s hot and what’s not
  2. HANAMI FOR RAILS DEVELOPERS WHAT IS HANAMI? ▸ “Hanami is

    a modern web framework for Ruby” created by Luca Guidi ▸ Promoting incremental design and separation of concerns beyond traditional MVC ▸ Multi application approach - container architecture
  3. HANAMI FOR RAILS DEVELOPERS CONTAINER ARCHITECTURE ▸ Hanami architecture can

    host several applications in the same Ruby process ▸ Each of them can be a component, such as the user facing web interface, the admin pane, metrics, HTTP API etc. ▸ These apps are a delivery mechanism to the business logic that lives under lib/ (entities, repositories, mailers) ▸ Generators:
 hanami new demoapp --application_name=api
 hanami generate app admin
  4. HANAMI FOR RAILS DEVELOPERS MODELS ▸ Model = Entity +

    Repository ▸ Entities hold the models attributes, but it doesn't deal with the persistence layer ▸ Repositories are responsible for dealing with the persistence of entities (storage independent) ▸ Shared between the Hanami applications ▸ Querying methods (like where, limit, order) are private to the repository context
  5. HANAMI FOR RAILS DEVELOPERS VIEWS AND TEMPLATES ▸ Rails views

    == Hanami templates ▸ A view is an object that's responsible for rendering a template ▸ Partials, similar to Rails ▸ Presenters included in framework ▸ Many supported rendering engines, including ERb, Haml, Slim, CoffeeScript, SASS etc.
  6. HANAMI FOR RAILS DEVELOPERS CONTROLLERS AND ACTIONS ▸ Controllers are

    simply modules that group many actions together ▸ Controller actions are classes (single responsibility!) ▸ Using expose method to expose variable of the action with the view ▸ Validations are performed in Controller Actions using dry- validations ▸ Callbacks support, easy error handling etc.
  7. HANAMI FOR RAILS DEVELOPERS class Admin::UsersController < ActionController::Base def index

    @users = User.all end def show @user = User.find_by(id: params[:id]) end end RUBY ON RAILS app/controllers/admin/users_controller.rb
  8. HANAMI FOR RAILS DEVELOPERS module Admin::Controllers::Users class Index include Admin::Action

    expose :users def call(params) @users = UserRepository.new.all end end end module Admin::Controllers::Users class Show include Admin::Action expose :user def call(params) @user = UserRepository.new.find(id: params[:id]) end end end HANAMI apps/admin/controllers/users/index.rb apps/admin/controllers/users/show.rb
  9. HANAMI FOR RAILS DEVELOPERS % gem install hanami % hanami

    new bookshelf --database=postgresql % cd bookshelf && echo "ruby '2.3.0'" >> Gemfile && bundle % git add . && git commit -m "Initial commit" % heroku apps:create % heroku config:add SERVE_STATIC_ASSETS=true % git push heroku master % heroku run bundle exec hanami db migrate % heroku open PRACTICAL BASICS
  10. HANAMI FOR RAILS DEVELOPERS WHAT’S HOT… ▸ Default architecture and

    conventions that make the app scalable and easy to test ▸ Working and maintained alternative for Rails ▸ Strong CLI
  11. HANAMI FOR RAILS DEVELOPERS …AND WHAT’S NOT ▸ Immature in

    terms of ecosystem - the community is still young, most popular gems are targeting Rails ▸ Be prepared to "roll up your sleeves" to get to the bottom of issues, figure how to do things that aren't covered by the guides
  12. HANAMI FOR RAILS DEVELOPERS USEFUL LINKS ▸ http://hanamirb.org/guides/getting-started/ ▸ https://github.com/davydovanton/awesome-hanami

    ▸ https://gitter.im/hanami/chat ▸ http://sequel.jeremyevans.net/rdoc/files/doc/ association_basics_rdoc.html