diff --git a/app/models/user/with_favorites.rb b/app/models/user/with_favorites.rb index d5a71a3b244ba08a3d3e4eaf68972f83bfcbdf0a..66a8cb4636f25663aaa98f147cce5d516c5291ca 100644 --- a/app/models/user/with_favorites.rb +++ b/app/models/user/with_favorites.rb @@ -3,6 +3,7 @@ module User::WithFavorites included do has_many :favorites, dependent: :destroy + after_save :autoset_favorites end def add_favorite(about) @@ -22,4 +23,12 @@ module User::WithFavorites def favorites_for(about) favorites.where(about_id: about.id, about_type: about.class.polymorphic_name) end + + def autoset_favorites + if saved_change_to_role? && website_manager? && favorites.none? + websites_to_manage.each do |website| + add_favorite(website) + end + end + end end