このコードをリファクタリングするにはどうすればよいですか? where 句、includes、および order 関数を分割する方法はありますか?
def self.product_search(query, console, genre, sort, order)
if query
#search(query)
if !console.nil? && console != "all" && !genre.nil? && genre != "all"
where("name_en ilike :q AND console_id = :c AND genre_id = :g OR ean ilike :q AND console_id = :c AND genre_id = :g", q: "%#{query}%", c: console, g: genre).includes(:genre, :console, :brand, :images).order("#{sort} #{order}")
elsif !console.nil? && console != "all"
where("name_en ilike :q AND console_id = :c OR ean ilike :q AND console_id = :c", q: "%#{query}%", c: console).includes(:genre, :console, :brand, :images).order("#{sort} #{order}")
elsif !genre.nil? && genre != "all"
where("name_en ilike :q AND genre_id = :g OR ean ilike :q AND genre_id = :g", q: "%#{query}%", g: genre).includes(:genre, :console, :brand, :images).order("#{sort} #{order}")
else
where("name_en ilike :q OR ean ilike :q", q: "%#{query}%").includes(:genre, :console, :brand, :images).order("#{sort} #{order}")
end
end
end