Last active
May 4, 2020 17:11
-
-
Save JeremiahChurch/1f27c603a90d6faf6ede75a46d8af6f0 to your computer and use it in GitHub Desktop.
Draper Automatic association decoration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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