Last active
November 30, 2016 02:17
-
-
Save CloCkWeRX/0112a6527fc75db784b3daf9e6b76086 to your computer and use it in GitHub Desktop.
Managing sets of relationships with an Operation
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
PhotosController | |
def destroy | |
@photo = Photo.find(params[:id]) | |
@photo.destroy | |
end | |
Planting | |
before_destroy do |planting| | |
photolist = planting.photos.to_a # save a temp copy of the photo list | |
planting.photos.clear # clear relationship b/w planting and photo | |
photolist.each do |photo| | |
photo.destroy_if_unused | |
end | |
end | |
Garden | |
before_destroy do |garden| | |
photolist = garden.photos.to_a # save a temp copy of the photo list | |
garden.photos.clear # clear relationship b/w garden and photo | |
photolist.each do |photo| | |
photo.destroy_if_unused | |
end | |
end | |
Harvest | |
before_destroy do |harvest| | |
photolist = harvest.photos.to_a # save a temp copy of the photo list | |
harvest.photos.clear # clear relationship b/w harvest and photo | |
photolist.each do |photo| | |
photo.destroy_if_unused | |
end | |
end | |
GardensController | |
def destroy | |
@garden = Garden.find(params[:id]) | |
@garden.destroy | |
expire_fragment("homepage_stats") | |
HarvestsController | |
def destroy | |
@harvest = Harvest.find(params[:id]) | |
@harvest.destroy | |
PlantingController | |
def destroy | |
@planting = Planting.find(params[:id]) | |
@garden = @planting.garden | |
@planting.destroy |
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
class SafePhotoRemovalOperation | |
def destroy_if_unused(photo) # more or less the same as current implementation | |
def destroy_all_relationships!(photo) # Currently in before_destroy do |photo| | |
photo.plantings.clear | |
photo.harvests.clear | |
photo.gardens.clear | |
## Remove photos safely from an object we are destroying, ie: garden | |
def remove_photo_from_object(object) | |
photolist = planting.photos.to_a # save a temp copy of the photo list | |
object.photos.clear # clear relationship b/w planting and photo | |
photolist.each do |photo| | |
destroy_if_unused(photo) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment