BEYOND $rails new
By Paul Oguda
Software Engineer, Andela
This framework is becoming very popular in the agile
development due to its productivity and agility features. Ruby on
Rails have built-in solutions to many common problems that
developer face during web development. Furthermore, this
framework favors convention over configuration, which
makes the web development more agile. Additionally, these
conventions make the web applications more maintainable as
well, because the developers adopt the Rails convention and
sensitive defaults" (Mejja, 2011)
But do these conventions make the web
applications more maintainable?
WHAT IS INSIDE
I. Rails New
II. Beyond Models
III. Beyond Controllers.
IV. Beyond Views
V. Beyond Rails new
VI. Beyond Rails
VII. Closing Thoughts
Ruby On Rails in 2020
❏ Ruby on Rails is a teenager in human terms (15 years
plus).
❏ Like any teenager, it is bound to get rebellious, unsettled
and there may be need to move beyond conventions.
❏ Beyond conventions we need help and different strategies.
❏ That which should result in the design of flexible,
delightful and maintainable solutions.
...a pattern is a common
solution
Drink Water while I preach Wine
❏ This is not gospel truth, your organization
may have its own patterns, go with it.
❏ Before adopting any of these its good if you
work on a prototype first.
Antipattern … the common
defective processes and
implementations within
organizations.
PART 1: RAILS NEW
The Rails Doctrine: A Recap
1. Optimize for programmer happiness
2. Convention over Configuration
3. The menu is omakase
4. No one paradigm
5. Exalt beautiful code
6. Provide sharp knives
7. Value integrated systems
8. Progress over stability
9. Push up a big tent
GETTING YOU FAR
❏ rails new gives you a head start.
❏ Upon which you can start your creation process.
❏ The scaffold created, allows you to know what, goes where.
❏ Most often, the scaffold gave you more than you actually
needed.
❏ From rails 6.1 you can do rails new your_cool_app
--minimal
PART 2: BEYOND MODELS
Model Layer
❏ The M in the MVC.
❏ It carries the business logic of the application and the
rules to manipulate the data.
❏ Models help manage the interaction with their
corresponding elements in the database.
❏ Models represent the information in the database and
do the appropriate validations.
Model antipatterns
❏ The logic of operation with an external service
is located in the model.
❏ Difficulty in maintaining and scaling the model.
❏ Breaking the Single Responsibility Principle
(SRP).
model patterns
Service Objects
app/services
When to Use
Service Objects are used:
❏ When an action is complex (some calculation).
❏ An action uses an external services
❏ An action just does not seem to belong to a model.
❏ An action makes use of a number of models.
Adapters
app/adapters
When to Use
❏ Adapters helps wrap third-party API calls.
❏ This provides an abstraction layer over our external API
calls.
❏ Centralizing all the requests that originate from our
application to third party APIs.
❏ This makes replacing such logic easy as everything is in
one place (infinum, 2020).
Query Objects
app/queries
When to use Query Objects
❏ Query Object is a design pattern that lets us extract query logic from Controllers
and Models into reusable classes.
❏ When your queries become super complex when working with ActiveRecord
relations.
❏ Scope that interacts with more than one column or model, should be a query object.
❏ Chained scope, should go to a query object.
PART 3: BEYOND
CONTROLLERS
Controllers
❏ The C in the MVC.
❏ Controllers interact with models and views.
❏ The incoming requests from the browsers are
processed by the controllers.
❏ Controllers process the data from the models and
pass it to the views for presentation.
Controller Antipatterns
❏ External API calls in a controller.
❏ Non-RESTful Controllers.
❏ A lot of Nested Resources
❏ Business Logic in the controllers.
controller patterns
Form Objects
app/forms
When to use
❏ Form Object is a design pattern that encapsulates logic
related to validating and persisting data (Railscast, 2013).
❏ Helps clean complex models that seem to always grow in
complexity.
❏ In the case of a complicated form, for instance one that
persists data into two different tables.
Value Objects
Value Objects
❏ A small simple object, like money or a date range, whose equality isn't
based on identity (Martin Fowler).
❏ Objects in Ruby are usually considered to be entity objects.
❏ Two objects may have matching attribute values but we do not consider
them equal because they are distinct objects.
❏ Value objects on the other hand, are compared by value.
❏ Two different value objects are considered equal when their attribute
values match.
PART 4: BEYOND VIEWS
Views
❏ The V in the MVC.
❏ The view is the front-end of the application,
representing the user interface.
❏ In Ruby on Rails, views are HTML files with embedded
Ruby code.
❏ Views can serve content in several formats, such as
HTML, PDF, XML, RSS and more.
View Antipatterns
❏ Calculations/Computations in the view.
❏ Fetching data from the database in the view.
❏ Law of demeter violation.
❏ Plenty of conditionals
view patterns
Presenters
app/presenters
Presenter
❏ Adds Presentation functionality to another class.
❏ Helps in extracting presentation logic from models leaving only
business logic.
❏ Instantiated at the view level.
Decorators
app/decorators
Decorators
❏ A class that adds functionality to another class.
❏ In Rails you can use draper gem.
❏ Differs from presenters in how close it lives live
to the view.
❏ Decorators lives near the business logic of your
application.
View Components
app/components
View Components
❏ Framework for building view components that are reusable, testable &
encapsulated, in Ruby on Rails.
❏ Designed to integrate as seamlessly as possible with Rails.
❏ Ruby objects that output HTML.
❏ A step up of the presenter pattern, drawing inspiration from React.
❏ Components are most effective in cases where view code is reused or
benefits from being tested directly.
StimulusReflex::Reflex
app/reflexes
Stimulus and Stimulus Reflex
❏ Build reactive applications with the Rails tooling you already know and
love.
❏ Concerns like managing state and rendering views are handled server
side.
❏ StimulusReflex keeps a 1:1 relationship between application state and
what is visible in the browser.
❏ This results in no need of managing state on the client.
❏ Resulting in massive reduction in application complexity.
Stimulus Reflex: How it works
❏ It extends the capabilities of both Rails and Stimulus by intercepting user
interactions.
❏ Then passing these to Rails over real-time websockets.
❏ These interactions are processed by Reflex actions that change application
state.
❏ The current page is quickly re-rendered and the changes are sent to the client
using CableReady.
❏ The page is then morphed to reflect the new application state.
❏ This entire round-trip allows us to update the UI in 20-30ms without flicker or
expensive page loads.
PART 5: BEYOND RAILS
NEW
Bounded Contexts
app/bounded_contexts
Why Bounded Contexts
❏ The point of Bounded Contexts is to organize the code
inside business boundaries (EquiValent, 2018).
❏ As you can imagine both bounded contexts are interacting
with same models (Customers, Employees, Managers) just
from different business perspective.
❏ A Bounded Context is a world in its own.
❏ It has its own “model” (not the ActiveRecord models)
which represents the domain.
Rails Engines
rails_app/components
Component Based Rails Applications
❏ AKA Engines.
❏ Component-Based Rails is a proven method to manage the
complexity of large applications.
❏ It ensures that we can maintain development speed, quality, and
fun throughout the life cycle of applications.
EventSourcing/CQRS
EVENT SOURCING /CQRS
❏ In an Event Sourced application, the database is an append
only.
❏ Immutable stack of events.
❏ Each event represent a delta change of the system.
❏ An event is roughly composed of an event name and a payload.
❏ There is only need of a single database table: events.
RAILS EVENT STORE (gem)
❏ Rails Event Store is a library for publishing,
consuming, storing and retrieving events.
❏ It's your best companion for going with an
Event-Driven Architecture for your Rails application.
PART 6: BEYOND RAILS
Every framework you learn is a lost opportunity to
build something that could really matter to the
world. Please choose responsibly. ⏳
(StimulusReflex, Docs)
Sinatra
❏ Sinatra is a DSL for quickly creating web applications.
require 'sinatra'
get '/frank-says' do
'Hello World from Sinatra'
end
❏ Then ruby myapp.rb and open http://localhost:4567
❏ http://sinatrarb.com/
Padrino
❏ Ruby web framework that leverages Sinatra.
❏ Adds Rails like feel to developing with Sinatra.
❏ http://padrinorb.com/
Roda
❏ A Modular, Scalable Ruby Framework. That aims for:
❏ Simplicity. Roda is designed to be simple, both
internally and externally, reducing cognitive overhead.
❏ Usability. Roda uses a routing tree. At any point during
routing, it allows you to operate on the current request.
❏ Productivity. Roda makes it easy to develop
applications quickly.
❏ https://roda.jeremyevans.net/
Rack
❏ Rack provides a minimal, modular, and adaptable interface
for developing web applications in Ruby.
❏ By wrapping HTTP requests and responses in the
simplest way possible.
❏ Allowing it to unify and distil the API for web servers, web
frameworks, and software in between (the so-called
middleware) into a single method call.
❏ https://github.com/rack/rack
Hanami
❏ Hanami is a modern web framework for Ruby.
❏ Hanami is based on two principles: Clean
Architecture and Monolith First.
❏ The main purpose of this architecture is to enforce a
separation of concerns between the core of our
product and the delivery mechanisms.
❏ http://hanamirb.org/
CLOSING THOUGHTS
Go Beyond Rails New
❏ Despite Rails fronting the concept of conventions over
configurations.
❏ It is also setup to allow you to undertake configurations
when you need to.
❏ Scalability and maintainability of a Rails application is how
you set it up in ways that works for you.
❏ Through Patterns or different abstractions that one may find
suitable to work with.
REFERENCES
1. https://adrianmejia.com/ruby-on-rails-architectural-design/
2. https://blog.eq8.eu/article/rails-bounded-contexts.html
3. https://www.sitepoint.com/7-design-patterns-to-refactor-mvc-components-in-ra
ils/.
4. https://www.bacancytechnology.com/blog/design-patterns-in-ruby-on-rails
5. https://naturaily.com/blog/ruby-on-rails-design-patterns
6. https://www.rubyguides.com/2019/09/rails-patterns-presenter-service/
7. https://www.ironin.it/blog/design-patterns-in-large-r
ails-applications-query-objects.html
References Cont...
8.https://www.ironin.it/blog/design-patterns-in-large-rails-applications-policy-objects
.html
9. https://blog.eq8.eu/article/rails-bounded-contexts.html.
10. https://blog.arkency.com/physical-separation-in-rails-apps/
11. https://cbra.info/book/index.html
12. https://blog.arkency.com/2015/09/cqrs-example-in-the-rails-app/
13. http://railscasts.com/episodes/416-form-objects
References Cont...
14. https://github.com/github/view_component

Beyond rails new

  • 1.
    BEYOND $rails new ByPaul Oguda Software Engineer, Andela
  • 3.
    This framework isbecoming very popular in the agile development due to its productivity and agility features. Ruby on Rails have built-in solutions to many common problems that developer face during web development. Furthermore, this framework favors convention over configuration, which makes the web development more agile. Additionally, these conventions make the web applications more maintainable as well, because the developers adopt the Rails convention and sensitive defaults" (Mejja, 2011)
  • 4.
    But do theseconventions make the web applications more maintainable?
  • 5.
    WHAT IS INSIDE I.Rails New II. Beyond Models III. Beyond Controllers. IV. Beyond Views V. Beyond Rails new VI. Beyond Rails VII. Closing Thoughts
  • 6.
    Ruby On Railsin 2020 ❏ Ruby on Rails is a teenager in human terms (15 years plus). ❏ Like any teenager, it is bound to get rebellious, unsettled and there may be need to move beyond conventions. ❏ Beyond conventions we need help and different strategies. ❏ That which should result in the design of flexible, delightful and maintainable solutions.
  • 7.
    ...a pattern isa common solution
  • 8.
    Drink Water whileI preach Wine ❏ This is not gospel truth, your organization may have its own patterns, go with it. ❏ Before adopting any of these its good if you work on a prototype first.
  • 9.
    Antipattern … thecommon defective processes and implementations within organizations.
  • 10.
  • 11.
    The Rails Doctrine:A Recap 1. Optimize for programmer happiness 2. Convention over Configuration 3. The menu is omakase 4. No one paradigm 5. Exalt beautiful code 6. Provide sharp knives 7. Value integrated systems 8. Progress over stability 9. Push up a big tent
  • 12.
    GETTING YOU FAR ❏rails new gives you a head start. ❏ Upon which you can start your creation process. ❏ The scaffold created, allows you to know what, goes where. ❏ Most often, the scaffold gave you more than you actually needed. ❏ From rails 6.1 you can do rails new your_cool_app --minimal
  • 13.
  • 14.
    Model Layer ❏ TheM in the MVC. ❏ It carries the business logic of the application and the rules to manipulate the data. ❏ Models help manage the interaction with their corresponding elements in the database. ❏ Models represent the information in the database and do the appropriate validations.
  • 15.
    Model antipatterns ❏ Thelogic of operation with an external service is located in the model. ❏ Difficulty in maintaining and scaling the model. ❏ Breaking the Single Responsibility Principle (SRP).
  • 16.
  • 17.
  • 18.
    When to Use ServiceObjects are used: ❏ When an action is complex (some calculation). ❏ An action uses an external services ❏ An action just does not seem to belong to a model. ❏ An action makes use of a number of models.
  • 19.
  • 20.
    When to Use ❏Adapters helps wrap third-party API calls. ❏ This provides an abstraction layer over our external API calls. ❏ Centralizing all the requests that originate from our application to third party APIs. ❏ This makes replacing such logic easy as everything is in one place (infinum, 2020).
  • 21.
  • 22.
    When to useQuery Objects ❏ Query Object is a design pattern that lets us extract query logic from Controllers and Models into reusable classes. ❏ When your queries become super complex when working with ActiveRecord relations. ❏ Scope that interacts with more than one column or model, should be a query object. ❏ Chained scope, should go to a query object.
  • 23.
  • 24.
    Controllers ❏ The Cin the MVC. ❏ Controllers interact with models and views. ❏ The incoming requests from the browsers are processed by the controllers. ❏ Controllers process the data from the models and pass it to the views for presentation.
  • 25.
    Controller Antipatterns ❏ ExternalAPI calls in a controller. ❏ Non-RESTful Controllers. ❏ A lot of Nested Resources ❏ Business Logic in the controllers.
  • 26.
  • 27.
  • 28.
    When to use ❏Form Object is a design pattern that encapsulates logic related to validating and persisting data (Railscast, 2013). ❏ Helps clean complex models that seem to always grow in complexity. ❏ In the case of a complicated form, for instance one that persists data into two different tables.
  • 29.
  • 30.
    Value Objects ❏ Asmall simple object, like money or a date range, whose equality isn't based on identity (Martin Fowler). ❏ Objects in Ruby are usually considered to be entity objects. ❏ Two objects may have matching attribute values but we do not consider them equal because they are distinct objects. ❏ Value objects on the other hand, are compared by value. ❏ Two different value objects are considered equal when their attribute values match.
  • 31.
  • 32.
    Views ❏ The Vin the MVC. ❏ The view is the front-end of the application, representing the user interface. ❏ In Ruby on Rails, views are HTML files with embedded Ruby code. ❏ Views can serve content in several formats, such as HTML, PDF, XML, RSS and more.
  • 33.
    View Antipatterns ❏ Calculations/Computationsin the view. ❏ Fetching data from the database in the view. ❏ Law of demeter violation. ❏ Plenty of conditionals
  • 34.
  • 35.
  • 36.
    Presenter ❏ Adds Presentationfunctionality to another class. ❏ Helps in extracting presentation logic from models leaving only business logic. ❏ Instantiated at the view level.
  • 37.
  • 38.
    Decorators ❏ A classthat adds functionality to another class. ❏ In Rails you can use draper gem. ❏ Differs from presenters in how close it lives live to the view. ❏ Decorators lives near the business logic of your application.
  • 39.
  • 40.
    View Components ❏ Frameworkfor building view components that are reusable, testable & encapsulated, in Ruby on Rails. ❏ Designed to integrate as seamlessly as possible with Rails. ❏ Ruby objects that output HTML. ❏ A step up of the presenter pattern, drawing inspiration from React. ❏ Components are most effective in cases where view code is reused or benefits from being tested directly.
  • 41.
  • 42.
    Stimulus and StimulusReflex ❏ Build reactive applications with the Rails tooling you already know and love. ❏ Concerns like managing state and rendering views are handled server side. ❏ StimulusReflex keeps a 1:1 relationship between application state and what is visible in the browser. ❏ This results in no need of managing state on the client. ❏ Resulting in massive reduction in application complexity.
  • 43.
    Stimulus Reflex: Howit works ❏ It extends the capabilities of both Rails and Stimulus by intercepting user interactions. ❏ Then passing these to Rails over real-time websockets. ❏ These interactions are processed by Reflex actions that change application state. ❏ The current page is quickly re-rendered and the changes are sent to the client using CableReady. ❏ The page is then morphed to reflect the new application state. ❏ This entire round-trip allows us to update the UI in 20-30ms without flicker or expensive page loads.
  • 44.
    PART 5: BEYONDRAILS NEW
  • 45.
  • 46.
    Why Bounded Contexts ❏The point of Bounded Contexts is to organize the code inside business boundaries (EquiValent, 2018). ❏ As you can imagine both bounded contexts are interacting with same models (Customers, Employees, Managers) just from different business perspective. ❏ A Bounded Context is a world in its own. ❏ It has its own “model” (not the ActiveRecord models) which represents the domain.
  • 47.
  • 48.
    Component Based RailsApplications ❏ AKA Engines. ❏ Component-Based Rails is a proven method to manage the complexity of large applications. ❏ It ensures that we can maintain development speed, quality, and fun throughout the life cycle of applications.
  • 49.
  • 50.
    EVENT SOURCING /CQRS ❏In an Event Sourced application, the database is an append only. ❏ Immutable stack of events. ❏ Each event represent a delta change of the system. ❏ An event is roughly composed of an event name and a payload. ❏ There is only need of a single database table: events.
  • 51.
    RAILS EVENT STORE(gem) ❏ Rails Event Store is a library for publishing, consuming, storing and retrieving events. ❏ It's your best companion for going with an Event-Driven Architecture for your Rails application.
  • 52.
  • 53.
    Every framework youlearn is a lost opportunity to build something that could really matter to the world. Please choose responsibly. ⏳ (StimulusReflex, Docs)
  • 54.
    Sinatra ❏ Sinatra isa DSL for quickly creating web applications. require 'sinatra' get '/frank-says' do 'Hello World from Sinatra' end ❏ Then ruby myapp.rb and open http://localhost:4567 ❏ http://sinatrarb.com/
  • 55.
    Padrino ❏ Ruby webframework that leverages Sinatra. ❏ Adds Rails like feel to developing with Sinatra. ❏ http://padrinorb.com/
  • 56.
    Roda ❏ A Modular,Scalable Ruby Framework. That aims for: ❏ Simplicity. Roda is designed to be simple, both internally and externally, reducing cognitive overhead. ❏ Usability. Roda uses a routing tree. At any point during routing, it allows you to operate on the current request. ❏ Productivity. Roda makes it easy to develop applications quickly. ❏ https://roda.jeremyevans.net/
  • 57.
    Rack ❏ Rack providesa minimal, modular, and adaptable interface for developing web applications in Ruby. ❏ By wrapping HTTP requests and responses in the simplest way possible. ❏ Allowing it to unify and distil the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call. ❏ https://github.com/rack/rack
  • 58.
    Hanami ❏ Hanami isa modern web framework for Ruby. ❏ Hanami is based on two principles: Clean Architecture and Monolith First. ❏ The main purpose of this architecture is to enforce a separation of concerns between the core of our product and the delivery mechanisms. ❏ http://hanamirb.org/
  • 59.
  • 60.
    Go Beyond RailsNew ❏ Despite Rails fronting the concept of conventions over configurations. ❏ It is also setup to allow you to undertake configurations when you need to. ❏ Scalability and maintainability of a Rails application is how you set it up in ways that works for you. ❏ Through Patterns or different abstractions that one may find suitable to work with.
  • 61.
    REFERENCES 1. https://adrianmejia.com/ruby-on-rails-architectural-design/ 2. https://blog.eq8.eu/article/rails-bounded-contexts.html 3.https://www.sitepoint.com/7-design-patterns-to-refactor-mvc-components-in-ra ils/. 4. https://www.bacancytechnology.com/blog/design-patterns-in-ruby-on-rails 5. https://naturaily.com/blog/ruby-on-rails-design-patterns 6. https://www.rubyguides.com/2019/09/rails-patterns-presenter-service/ 7. https://www.ironin.it/blog/design-patterns-in-large-r ails-applications-query-objects.html
  • 62.
    References Cont... 8.https://www.ironin.it/blog/design-patterns-in-large-rails-applications-policy-objects .html 9. https://blog.eq8.eu/article/rails-bounded-contexts.html. 10.https://blog.arkency.com/physical-separation-in-rails-apps/ 11. https://cbra.info/book/index.html 12. https://blog.arkency.com/2015/09/cqrs-example-in-the-rails-app/ 13. http://railscasts.com/episodes/416-form-objects
  • 63.