Skip to content

Instantly share code, notes, and snippets.

@JeremiahChurch
Last active May 4, 2020 17:11
Show Gist options
  • Save JeremiahChurch/1f27c603a90d6faf6ede75a46d8af6f0 to your computer and use it in GitHub Desktop.
Save JeremiahChurch/1f27c603a90d6faf6ede75a46d8af6f0 to your computer and use it in GitHub Desktop.
Draper Automatic association decoration
# Keeping track of associations in activerecord and then going over to draper decorators and adding new associations is
# something that I never much liked - multiple places meant a potential error.
# so here's an easy draper concern that keeps your draper decorators in sync with your underlying models.
# just `include AutoDecorateAssociations` on your decorators and you're done worrying about changing associations on your models.
# If you're working with a model that doesn't have a decorator - like papertrail then it can be easily excluded like :versions below.
module AutoDecorateAssociations
extend ActiveSupport::Concern
included do
decorates_associations *(object_class.reflect_on_all_associations.map(&:name) - [:versions])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment