【问题标题】:How to resoved issue check style in rubocop?如何解决 rubocop 中的问题检查样式?
【发布时间】:2017-04-05 13:48:06
【问题描述】:

我有多个条件和多个面板连接在一起的搜索功能,如下所示

hospitals = Hospital.order(created_at: :desc).group(:id)
hospitals = hospitals.joins(medical_subjects: :sicks).where("(medical_subjects.name LIKE ? OR sicks.name LIKE ?) AND hospitals.name LIKE ?" ,
                                                            "%#{params[:freeword_medical_subject_sick_name].strip}%" ,
                                                            "%#{params[:freeword_medical_subject_sick_name].strip}%" ,
                                                            "%#{params[:freeword_hospital_name].strip}%") if params[:freeword_medical_subject_sick_name].present?
hospitals = hospitals.joins(prefecture: [:cities, :stations]).where("cities.name LIKE ? OR stations.name LIKE ?" ,
                                                                                    "%#{params[:freeword_city_station_name]}%" ,
                                                                                    "%#{params[:freeword_city_station_name].strip}%") if params[:freeword_city_station_name].present?
hospitals = hospitals.where(["hospitals.name LIKE ?" , "%#{params[:freeword_hospital_name].strip}%"]) if params[:freeword_hospital_name]
hospitals = hospitals.where(woman_doctor_existed: params[:woman_doctor_existed]) if params[:woman_doctor_existed].present?
hospitals = hospitals.where(emergency_enabled: params[:emergency_enabled]) if params[:emergency_enabled].present?
hospitals = hospitals.where(checkup_enabled: params[:checkup_enabled]) if params[:checkup_enabled].present?
hospitals = hospitals.where(dpc_enabled: params[:dpc_enabled]) if params[:dpc_enabled].present?
hospitals = hospitals.where(parking_enabled: params[:parking_enabled]) if params[:parking_enabled].present?
hospitals = hospitals.where(card_enabled: params[:card_enabled]) if params[:card_enabled].present?
hospitals = hospitals.where(newest_medicine_enabled: params[:newest_medicine_enabled]) if params[:newest_medicine_enabled].present?
hospitals = hospitals.page(params[:page])
hospitals

但是当我运行 rubocop 这个文件时遇到了一些错误

  • 搜索大小的分配分支条件太高。
  • 用于搜索的循环复杂度过高。
  • 方法行数过多。 [14/10]
  • 感知的搜索复杂度过高

有什么办法可以解决吗? 感谢阅读,对不起我的英语不好

【问题讨论】:

    标签: ruby-on-rails search model rubocop


    【解决方案1】:

    这是说你用一种方法做很多事情。您可以尝试将其分解为更小的方法。另一个想法是删除一些重复。例如,如果存在某些参数,您经常会添加 WHERE 子句。考虑这样的事情:

    %i(woman_doctor_existed emergency_enabled checkup_enabled more...).each do |key|
      hospitals = hospitals.where(key => params[key]) if params.has? key
    end
    

    【讨论】:

      猜你喜欢
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 2020-09-30
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多