Move seed.interesting to a scope

This commit is contained in:
Brenda Wallace
2017-05-20 16:58:49 +12:00
parent 4841c52bcd
commit 5f9b0890fb

View File

@@ -31,7 +31,8 @@ class Seed < ActiveRecord::Base
allow_nil: true
scope :tradable, -> { where("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
validates :tradable_to, inclusion: { in: TRADABLE_TO_VALUES,
message: "You may only trade seed nowhere, "\
@@ -77,27 +78,6 @@ class Seed < ActiveRecord::Base
end
end
def interesting?
# assuming we're passed something that's already known to be tradable
# eg. from Seed.tradable scope
return false if owner.location.blank? # don't want unspecified locations
true
end
# Seed.interesting
# returns a list of interesting seeds, for use on the homepage etc
def self.interesting
howmany = 12 # max number to find
interesting_seeds = []
Seed.tradable.each do |s|
break if interesting_seeds.size == howmany
interesting_seeds.push(s) if s.interesting?
end
interesting_seeds
end
def seed_slug
"#{owner.login_name}-#{crop}".downcase.tr(' ', '-')
end