Decorating associations in Rails with Tramway
May 20, 2024
We already talked about decorators in Rails here [Delegating ActiveRecord methods to decorators in Rails].
Now we provide a new feature — decorating association via Tramway Decorator.
How it works
class User < ApplicationRecord
has_many :posts
end
class UserDecorator < Tramway::BaseDecorator
association :posts
end
class PostDecorator < Tramway::BaseDecorator
# stuff you need
end
user = tramway_decorate User.first
user.posts #=> returns collection of decorated object with PostDecorator
Well, it makes it simple to make a kinda “Decorating structure”. You always know that once you decorate one object it decorates all associations you set to be decorated.
Install the last version of Tramway Gem for this.