- 方法分割,该方法是否干了过多工作
- 将参数封装为对象/Hash
- 使用块
比如查询方法,传入很多查询条件,转化为传入块,块内调用方法处理参数
def query(name,age,gender,enter_time,leave_time,level,poisition)
cond = {}
cond.merge! name: name if name.present?
cond.merge! gendar: gender if gender.present?
...
Employee.where(cond)
end
# 修改为
def cond(key,value)
@cond || = {}
@cond.merge key: value
end
def query
yield(self) if block_given?
end
# 调用
query do |q|
q.cond name: name if name.present?
q.cond age: age if age.present?
..
end
网友评论