I'm trying to fix this error on codeclimate

Cyclomatic complexity for resource_icon is too high. [7/6]

What is the best way to refactor this function?

def resource_icon(resource)
    case resource.to_sym
    when :locations
    when :monitored_places
    when :attendances
      'fa fa-bicycle'
    when :gym_distribuitions
      'fa fa-users'
    when :ranking_activities
      'fa fa-list-ol'
    when :attendance_by_group_class_trainers
      'fa fa-graduation-cap'
  • 1
    \$\begingroup\$ Welcome to Code Review. Please explain how this code is to be used, in context. See How to Ask. \$\endgroup\$ – 200_success Oct 4 '17 at 14:58

You could try using a hash that maps "locations" to "fa-building", "monitored_places" to "fa-globe", and so on, with a default value of "fa-diamond".

| improve this answer | |
  • 3
    \$\begingroup\$ Indeed : h = {locations: 'fa-building', monitored_places: 'fa-globe', attendances: 'fa fa-bicycle', gym_distribuitions: 'fa fa-users', ranking_activities: 'fa fa-list-ol', attendance_by_group_class_trainers: 'fa fa-graduation-cap'}; h.default = 'fa-diamond'; h \$\endgroup\$ – Eric Duminil Oct 5 '17 at 12:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.