我正在复制我includes
的 、where
、order
和paginate
方法,因为我不知道是否有 Arel 链的块或更好的方法来使这个 DRY。where
有没有什么东西可以在不使用三元运算符的长字符串的情况下轻松测试 params 中的 special_id 或 facility_id ?
class << self
def list(options = {})
facility_id = options[:facility_id] || nil
keywords = options[:keywords] || nil
page = options[:page] || nil
specialty_id = options[:specialty_id] || nil
jobs = self.arel_table
unless keywords.nil?
keywords = keywords.downcase.tr_s('^a-z0-9 ', '').tr_s(' ', '\%')
end
if !specialty_id.blank?
approved.
includes(:facility, :specialties, :videos).
where(jobs[:name].matches("%#{keywords}%")).
where(specialties: {id: specialty_id}).
order(jobs[:name]).
paginate(page: page, per_page: 20)
elsif !facility_id.blank?
approved.
includes(:facility, :specialties, :videos).
where(jobs[:name].matches("%#{keywords}%")).
where(facilities: {id: facility_id}).
order(jobs[:name]).
paginate(page: page, per_page: 20)
else
approved.
includes(:facility, :specialties, :videos).
where(jobs[:name].matches("%#{keywords}%")).
order(jobs[:name]).
paginate(page: page, per_page: 20)
end
end
end