I am a fairly new (RubyonRails) developer. I desire to improve my coding skills so I used climate to do some review on my code. It gave me a flag for this method that its complex. Is it characterised as complex because of having several "actions/tasks" in a single method?
Will it be better if I extract some code segments to a different method?
Is there something else I am not seeing?
def search
filter_mapping = {"cost" => "rcost", "quality" => "rquality", "time" => "rtime", "experience" => "rexperience", "communication" => "rcommunication"}
@filters = params[:filter].split(",") rescue []
@sort = params[:sort]
@user_type = params[:s_user_type]
@skills = params[:s_skills]
@location = params[:location]
@max_rate = params[:max_rate]
@availability = params[:availability]
@users = User.scoped.joins(:user_skills)
@users = @users.where('user_type = ?', @user_type) if @user_type.present?
@users = @users.where('user_skills.skill_id in (?)', @skills.map(&:to_i)) if @skills.present? && @skills.size > 0
@users = @users.where('availability = ?', @availability) if @availability.present?
@users = @users.where('location_country = ?', @location) if @location.present?
@users = @users.where('rate < ?', @max_rate.to_i) if @max_rate.present?
@users = @users.page(params[:page]).per(PER_PAGE)
@filters.each do |f|
if filter_mapping.keys.include?(f)
@users = @users.order("#{filter_mapping[f]} desc")
end
end
@users = @users.order('id asc') if @filters.empty?
@advanced_link = @location.blank? && @max_rate.blank? && @availability.blank?
render :index
end
Update
I figured out that I can extract the scopes into a method like that:
def get_users_where_scopes
@users = User.scoped.joins(:user_skills)
@users = @users.where('user_type = ?', @user_type) if @user_type.present?
@users = @users.where('user_skills.skill_id in (?)', @skills.map(&:to_i)) if @skills.present? && @skills.size > 0
@users = @users.where('availability = ?', @availability) if @availability.present?
@users = @users.where('location_country = ?', @location) if @location.present?
@users = @users.where('rate < ?', @max_rate.to_i) if @max_rate.present?
@users = @users.page(params[:page]).per(PER_PAGE)
end
and then call it with @users = get_users_where_scopes()
. But now the complexity of this method seems wrong to me.