I am a Sr. Software Developer at Oracle Cloud. The opinions expressed here are my own and not necessarily those of my employer.
Rails application object pattern
Ruby on Rails has patterns for ApplicationRecord, ApplicationMailer, ApplicationJob, etc. Other gems follow this approach. Pundit has ApplicationPolicy and ActiveModelSerializers has AppliciationSerializer.
In post about Rails concerns I shared examples on how to extract common logic into modules. This post is about using base classes to DRY our code. We can extend Application* pattern to other gems and our own classes. Draper does not have ApplicationDecorator but we can create one.
# app/decorators/application_decorator.rb
class ApplicationDecorator < Draper::Decorator
delegate_all
...
end
# app/decorators/user_decorator.rb
class UserDecorator < ApplicationDecorator
...
endAdditonally our applications can have other POROs for forms, service objects, validators, etc. Where needed we can create those base Application* classes and inherit from them.
# app/forms/application_form.rb
class ApplicationForm
include ActiveModel::Model
...
end
class UserRegisterForm < ApplicationForm
...
endAt the end of the day this is just basic object oriented inheritance, use it where it makes sense. But this pattern makes things easy to follow.