From 77b7969fc98d39b0f930f29489e00c0897dbcadc Mon Sep 17 00:00:00 2001 From: Mackenzie Morgan Date: Wed, 30 Nov 2016 00:46:58 -0500 Subject: [PATCH] DRY up photo & model interaction * move stuff in models into a concern * create combined collection for all models that have photos --- app/models/concerns/photo_capable.rb | 18 ++++++++++++++++++ app/models/garden.rb | 14 ++------------ app/models/harvest.rb | 13 ++----------- app/models/photo.rb | 24 ++++++++++++++---------- app/models/planting.rb | 12 +----------- spec/models/photo_spec.rb | 2 ++ 6 files changed, 39 insertions(+), 44 deletions(-) create mode 100644 app/models/concerns/photo_capable.rb diff --git a/app/models/concerns/photo_capable.rb b/app/models/concerns/photo_capable.rb new file mode 100644 index 000000000..f2f53aa42 --- /dev/null +++ b/app/models/concerns/photo_capable.rb @@ -0,0 +1,18 @@ +module PhotoCapable + extend ActiveSupport::Concern + + included do + has_and_belongs_to_many :photos + + before_destroy :remove_from_list + end + + def remove_from_list + photolist = self.photos.to_a # save a temp copy of the photo list + self.photos.clear # clear relationship b/w object and photo + + photolist.each do |photo| + photo.destroy_if_unused + end + end +end diff --git a/app/models/garden.rb b/app/models/garden.rb index ec4aea94f..9f58ac0ab 100644 --- a/app/models/garden.rb +++ b/app/models/garden.rb @@ -1,23 +1,13 @@ class Garden < ActiveRecord::Base - include Geocodable extend FriendlyId + include Geocodable + include PhotoCapable friendly_id :garden_slug, use: [:slugged, :finders] belongs_to :owner, class_name: 'Member', foreign_key: 'owner_id' has_many :plantings, -> { order(created_at: :desc) }, dependent: :destroy has_many :crops, through: :plantings - has_and_belongs_to_many :photos - - 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 - # set up geocoding geocoded_by :location after_validation :geocode diff --git a/app/models/harvest.rb b/app/models/harvest.rb index 04f4608f2..1afdf473d 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -1,22 +1,13 @@ class Harvest < ActiveRecord::Base - include ActionView::Helpers::NumberHelper extend FriendlyId + include ActionView::Helpers::NumberHelper + include PhotoCapable friendly_id :harvest_slug, use: [:slugged, :finders] belongs_to :crop belongs_to :owner, class_name: 'Member' belongs_to :plant_part - has_and_belongs_to_many :photos - - 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 default_scope { order('created_at DESC') } diff --git a/app/models/photo.rb b/app/models/photo.rb index 6d7fdbcba..bd41b5c39 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -1,22 +1,26 @@ class Photo < ActiveRecord::Base + ON_MODELS = %w(plantings harvests gardens) belongs_to :owner, class_name: 'Member' - has_and_belongs_to_many :plantings - has_and_belongs_to_many :harvests - has_and_belongs_to_many :gardens - before_destroy do |photo| - photo.plantings.clear - photo.harvests.clear - photo.gardens.clear + ON_MODELS.each do |relation| + has_and_belongs_to_many relation.to_sym end + before_destroy { relationships.clear } + default_scope { order("created_at desc") } + def relationships + associations = [] + ON_MODELS.each do |association_name, _reflection| + associations << self.send("#{association_name}").to_a + end + associations.flatten! + end + # remove photos that aren't used by anything def destroy_if_unused - unless plantings.size > 0 or harvests.size > 0 or gardens.size > 0 - self.destroy - end + self.destroy unless relationships.size > 0 end # This is split into a side-effect free method and a side-effecting method diff --git a/app/models/planting.rb b/app/models/planting.rb index 8dc339687..6e869be33 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -1,22 +1,12 @@ class Planting < ActiveRecord::Base extend FriendlyId + include PhotoCapable friendly_id :planting_slug, use: [:slugged, :finders] belongs_to :garden belongs_to :owner, class_name: 'Member', counter_cache: true belongs_to :crop, counter_cache: true - has_and_belongs_to_many :photos - - 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 - default_scope { order("created_at desc") } scope :finished, -> { where(finished: true) } scope :current, -> { where(finished: false) } diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index 5ec32bf73..b1f4f2759 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -85,11 +85,13 @@ describe Photo do planting.destroy # photo is still used by harvest and garden photo.reload + expect(photo.plantings.size).to eq 0 expect(photo.harvests.size).to eq 1 harvest.destroy garden.destroy # photo is now no longer used by anything + photo.reload expect(photo.plantings.size).to eq 0 expect(photo.harvests.size).to eq 0