Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rails engines

137 views

Published on

My talk at Ruby Meetup Hamburg, January 10, 2018 19:00
hosted by XING (www.xing.com) Dammtorstrasse 29-32, 20354 Hamburg by

Description:
When a monolithic Rails app reaches a certain complexity, it becomes hard to understand what each part of code does. In this talk, I explained how to build a large rails app modularized by engines to enforce an internal dependency structure.

Published in: Software
  • Be the first to comment

Rails engines

  1. 1. Rails Engines By Gharbi Mohammed Software Engineer at Event Inc github.com/mgharbik mgharbik@gmail.com
  2. 2. ● What problems one can face using monolithic rails app? ● How we could solve them by using: ○ Namespaces ○ Rails Engines Content
  3. 3. Monolithic Rails App
  4. 4. Solution?
  5. 5. Namespaces Modularity Dependency structure
  6. 6. Engines
  7. 7. Engines
  8. 8. Engines
  9. 9. "The secret to building large apps is never build large apps.
  10. 10. "The secret to building large apps is never build large apps. Break your applications into small pieces. Then, assemble those testable, bite-sized pieces into your big application" - Justin Meyer
  11. 11. path 'components' do gem 'content' gem 'public_ui' gem 'admin_ui' gem 'api' ... end Gemfile
  12. 12. Rails.application.routes.draw do mount PublicUi::Engine, at: '/' mount AdminUi::Engine, at: '/admin' mount Api::Engine, at: '/api' ... end Routes
  13. 13. ● each component in isolation ● dummy app ● spec_helper.rb require File.expand_path("../dummy/config/environment", __FILE__) Tests
  14. 14. #!/bin/bash exit_code=0 echo "*** Running engine_name engine specs" bundle install | grep Installing RAILS_ENV=test bundle exec rake db:create RAILS_ENV=test bundle exec rake db:migrate bundle exec rspec spec exit_code+=$? exit $exit_code test.sh
  15. 15. #!/bin/bash result=0 cd "$( dirname "${BASH_SOURCE[0]}" )" for test_script in $(find . -name test.sh); do pushd `dirname $test_script` > /dev/null ./test.sh result+=$? popd > /dev/null done if [ $result -eq 0 ]; then echo "SUCCESS" else echo "FAILURE" fi exit $result build.sh
  16. 16. Benefits ● Component based architecture is complementary to good object oriented practices ● Engines use namespaces ● It enforces an internal dependency structure ● Clean Gemfile ● Clean routes.rb ● Fast tests ● Working in large team
  17. 17. content shared_ui admin_ui public_ui auth_ui registration_ui api importer user search
  18. 18. content shared_ui admin_ui public_ui auth_ui registration_ui api importer user search
  19. 19. content shared_ui admin_ui public_ui auth_ui registration_ui api importer user search
  20. 20. ● https://github.com/plataformatec/devise ● https://github.com/solidusio/solidus Examples
  21. 21. https://github.com/mgharbik/sportsball-cbra Repo
  22. 22. Thank you
  23. 23. ● http://teotti.com/component-based-rails-architecture-primer ● http://tech.taskrabbit.com/blog/2014/02/11/rails-4-engines ● https://leanpub.com/cbra Resources
  24. 24. $ rails plugin new admin_ui --mountable Engine
  25. 25. initializer :append_migrations do |app| unless app.root.to_s.match root.to_s+File::SEPARATOR config.paths['db/migrate'].expanded.each do |expanded_path| app.config.paths['db/migrate'] << expanded_path end end end Migrations
  26. 26. $ bundle exec rake engine_name:install:migrations Migrations

×