Checks owner exists, in default_scopes of many models

This commit is contained in:
Brenda Wallace
2017-05-26 23:00:06 +12:00
parent 9c4a45bc29
commit 0b684eb05e
5 changed files with 13 additions and 8 deletions

View File

@@ -14,7 +14,8 @@ class Garden < ActiveRecord::Base
after_validation :empty_unwanted_geocodes
after_save :mark_inactive_garden_plantings_as_finished
default_scope { order("lower(name) asc") }
default_scope { owner_exists.order("lower(name) asc") }
scope :owner_exists, -> { joins(:owner) }
scope :active, -> { where(active: true) }
scope :inactive, -> { where(active: false) }

View File

@@ -9,7 +9,8 @@ class Harvest < ActiveRecord::Base
belongs_to :plant_part
belongs_to :planting
default_scope { order(created_at: :desc) }
default_scope { owner_exists.order(created_at: :desc) }
scope :owner_exists, -> { joins(:owner) }
validates :crop, approved: true
validates :crop, presence: { message: "must be present and exist in our database" }

View File

@@ -8,15 +8,16 @@ class Planting < ActiveRecord::Base
belongs_to :crop, counter_cache: true
has_many :harvests, -> { order(harvested_at: :desc) }, dependent: :destroy
default_scope { order(created_at: :desc) }
default_scope { owner_exists.order(created_at: :desc) }
scope :owner_exists, -> { joins(:owner) }
scope :finished, -> { where(finished: true) }
scope :current, -> { where(finished: false) }
scope :interesting, -> { has_photos.one_per_owner }
scope :one_per_owner, lambda {
joins("JOIN members m ON (m.id=plantings.owner_id)
LEFT OUTER JOIN plantings p2
ON (m.id=p2.owner_id AND plantings.id < p2.id)").where("p2 IS NULL")
LEFT OUTER JOIN plantings p2
ON (m.id=p2.owner_id AND plantings.id < p2.id)").where("p2 IS NULL")
}
delegate :name,

View File

@@ -36,7 +36,8 @@ class Post < ActiveRecord::Base
end
end
default_scope { order(created_at: :desc) }
default_scope { author_exists.order(created_at: :desc) }
scope :author_exists, -> { joins(:author) }
validates :subject,
presence: true,

View File

@@ -6,7 +6,7 @@ class Seed < ActiveRecord::Base
belongs_to :crop
belongs_to :owner, class_name: 'Member', foreign_key: 'owner_id', counter_cache: true
default_scope { order(created_at: :desc) }
default_scope { owner_exists.order(created_at: :desc) }
validates :crop, approved: true
delegate :name, to: :crop
@@ -32,7 +32,8 @@ class Seed < ActiveRecord::Base
},
allow_nil: true
scope :tradable, -> { where("tradable_to != 'nowhere'") }
scope :owner_exists, -> { joins(:owner) }
scope :tradable, -> { where.not(tradable_to: 'nowhere') }
scope :interesting, -> { tradable.has_location }
scope :has_location, -> { joins(:owner).where.not("members.location": nil) }
TRADABLE_TO_VALUES = %w(nowhere locally nationally internationally).freeze