Mistři v programování na #hovnokod

Ruby #314

def self.find_or_create params
  if params[:rule_id].present?
    rule = new_from_params params
    return nil if rule.nil?
    parent_rule = Rule.find_by_id params[:rule_id]

    if parent_rule and Rule.calc_md5(rule.to_hash) == parent_rule.crc
      parent_rule.rule_id = parent_rule.id
      parent_rule.grouped_by = rule.grouped_by
      parent_rule.sorted_by = rule.sorted_by
      parent_rule.save
      return parent_rule
    end
    rule_id = params.delete :rule_id
  end

  params = clear_and_sort params
  if (rule = Rule.find_by_crc(Rule.calc_md5(params))).present?
    rule.rule_id = rule_id
    rule.grouped_by = params[:grouped_by]
    rule.sorted_by = params[:sorted_by]
    rule.save
  else
    rule = new_from_params params.merge(:rule_id => rule_id)
    rule.save
  end
  rule
end

HovnoKod,