mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-24 08:52:14 -04:00
Move seed.interesting to a scope
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user