require "csv"
require_dependency "safe_html"

class LocalService
  include Mongoid::Document

  field :description,    type: String
  field :lgsl_code,      type: Integer
  field :providing_tier, type: Array

  validates :lgsl_code, :providing_tier, presence: true
  validates :lgsl_code, uniqueness: true
  validates :providing_tier,
            inclusion: {
              in: [%w[county unitary], %w[district unitary], %w[district unitary county]],
            }

  def self.find_by_lgsl_code(lgsl_code)
    LocalService.where(lgsl_code: lgsl_code).first
  end
end

require "csv"
require_dependency "safe_html"

class LocalService
  include Mongoid::Document

  field :description,    type: String
  field :lgsl_code,      type: Integer
  field :providing_tier, type: Array

  validates :lgsl_code, :providing_tier, presence: true
  validates :lgsl_code, uniqueness: true
  validate :eligible_providing_tier

  def self.find_by_lgsl_code(lgsl_code)
    LocalService.where(lgsl_code: lgsl_code).first
  end

  def eligible_providing_tier
    providing_tiers = [%w[county unitary], %w[district unitary], %w[district unitary county]]

    return if providing_tiers.include?(providing_tier)

    errors.add(:providing_tier, "Not in list")
  end
end

Fix validation after Rails bump
Fix validation after Rails bump

The use of `inclusion` `in:` stoppped recognising nested arrays, and
began to fail validation everytime an attempt to create a LocaleService
was made. I couldn't find an out of the box solution for this so have
added a simple custom validator.
