lightening a component based rails architecture

24
component based Rails application primer Enrico Teotti -- @agenteo

Upload: enrico-teotti

Post on 21-Feb-2017

517 views

Category:

Software


0 download

TRANSCRIPT

component based Rails application primer

Enrico Teotti -- @agenteo

component based Rails application primer

Rogrido Teotti -- @agenteo

misconception that working on complex software must feel hard

like carrying 200 potatos is harder than carrying 20

This is wrong for two reasons: software development is knowledge work not labour and

because of documented patterns to tackle software complexity with incremental code design

decorators, presenters, service objects can be sufficient to handle some complexity but

when the project is over a certain size they don’t help understand what the

whole application does

Component based architecture is complementary to good object oriented practices and uses

namespaces, test driven development and Ruby gems to gradually define application boundaries and enforce an internal dependency structure.

path 'components' do

gem 'public_ui'

gem 'admin_ui'

gem 'legacy_migration'

end

intention revealing Gemfile

Component is the name given to a Ruby on Rails engine or Ruby gem when used as building block of

the Rails application

● are ruby gems● are special ruby gems that provide extra

behaviour (models, views, routes, rake tasks) to a Rails application

● they can be hosted on a gemserver or they can live inside your repository

● can be tested in isolation

what are rails engines?

config/routes.rb

Rails.application.routes.draw do

case AppRunningMode.value

when :admin

mount AdminUi::Engine => "/"

when :public

mount PublicUi::Engine => "/"

else

mount AdminUi::Engine => "/"

mount PublicUi::Engine => "/"

end

end

don’t architect an application with components from day one instead introduce them gradually as the

complexity grows or when the scope changes

RUNNING_MODE=public rails s

RUNNING_MODE=admin rails s

http://example-internal-vpn-domain.com/admin

http://example.com/content

rails s

http://localhost:3000

proceeding without automated tests

could drive you crazy

use the test dummy app

avoid high level engines coupling

admin_ui

public_ui

admin_ui public_ui

domain_logic

DomainLogic::Content::Article

admin_ui public_ui

shared_ui domain_logic

Gem::Specification.new do |s|

# ... other fields up here

s.name = "admin_ui"

s.add_dependency "rails", "~> 4.1.1"

s.add_dependency 'jquery-rails'

s.add_dependency 'mongoid'

s.add_dependency 'faraday'

s.add_dependency "domain_logic"

s.add_dependency "shared_ui"

s.add_development_dependency 'byebug'

s.add_development_dependency 'database_cleaner'

s.add_development_dependency 'rspec-rails', '2.99.0'

s.add_development_dependency 'vcr'

s.add_development_dependency 'webmock'

s.add_development_dependency 'capybara'

s.add_development_dependency 'poltergeist'

end

admin_ui/admin_ui.gemspec

admin_ui public_ui

shared_ui domain_logic

legacy data import

admin_ui public_ui

shared_ui domain_logic

legacy data importglobal search

admin_ui/app/views/cargo_preview/show.html.erb

<%# admin console stuff here %>

<%= render partial: 'shared_ui/cargos/show' %>

<%# admin spaceship here %>

public_ui/app/views/cargos/show.html.erb

<%= render partial: 'shared_ui/cargos/show' %>

http://teotti.com

Enrico Teotti - @agenteo

http://teotti.com/component-based-rails-architecture-primer/

Questions?