DRY up photo & model interaction

* move stuff in models into a concern
 * create combined collection for all models that have photos
This commit is contained in:
Mackenzie Morgan committed 2016-11-30 00:46:58 -05:00
1 parent 7b0fdcf007
commit 77b7969fc9
6 files changed
+39 -44

No files matched your search

+18
View File
@@ -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
+2 -12
View File
@@ -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
+2 -11
View File
@@ -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') }
+14 -10
View File
@@ -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
+1 -11
View File
@@ -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) }
+2
View File
@@ -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