Duplication of enum across two models

In my Rails app, I have two classes each with the same enum for status. Code climate is complaining about duplication.

class Enrolment < ActiveRecord::Base
  has_many :enrolment_presentations
  has_many :presentations, through: :enrolment_presentations

  enum status: {
    'Pending' => 1,
    'Suspended' => 2,
    'Cancelled' => 3,
    'Pass' => 4,
    'Fail' => 5,
    'DPR' => 6,
    'Unknown' => 7,
    'Enrolled' => 8
  }
end


class EnrolmentPresentations < ActiveRecord::Base
  belongs_to :enrolment
  belongs_to :presentation

  enum status: {
    'Pending' => 1,
    'Suspended' => 2,
    'Cancelled' => 3,
    'Pass' => 4,
    'Fail' => 5,
    'DPR' => 6,
    'Unknown' => 7,
    'Enrolled' => 8
  }
end

How would you go about removing this duplication?

You could extract the hash of statuses to a common module, e.g.:

module EnrolmentStatus
  def self.all
    {
      'Pending' => 1,
      'Suspended' => 2,
    ...
    }
  end
end

This would be referenced using:

enum status: EnrolmentStatus.all

1 Like