Sha256: 98bc2cd60675b1da94e64770a9b3b187eea6613bf35ec0799e0a756e1b86404b

Contents?: true

Size: 1.26 KB

Versions: 99

Compression:

Stored size: 1.26 KB

Contents

require "csv"
require "local_authority"
require "safe_html"

class LocalService
  include Mongoid::Document

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

  GOVSPEAK_FIELDS = []

  validates_presence_of :lgsl_code, :providing_tier
  validates_uniqueness_of :lgsl_code
  validates :providing_tier, inclusion: {
    in: [%w{county unitary}, %w{district unitary}, %w{district unitary county}]
  }
  validates_with SafeHtml

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

  def preferred_interaction(snac_or_snac_list, lgil_override = nil)
    provider = preferred_provider(snac_or_snac_list)
    provider && provider.preferred_interaction_for(lgsl_code, lgil_override)
  end

  def preferred_provider(snac_or_snac_list)
    snac_list = [*snac_or_snac_list]
    providers = LocalAuthority.for_snacs(snac_list)
    select_tier(providers)
  end

  def provided_by
    LocalAuthority.where("local_interactions.lgsl_code" => lgsl_code)
                  .any_in(tier: providing_tier)
  end

private

  def select_tier(authorities)
    by_tier = Hash[authorities.map {|a| [a.tier, a]}]
    tier = providing_tier.find { |t| by_tier.has_key?(t) }
    tier && by_tier[tier]
  end

end

Version data entries

99 entries across 99 versions & 1 rubygems

Version Path
govuk_content_models-10.4.0 app/models/local_service.rb
govuk_content_models-10.3.0 app/models/local_service.rb
govuk_content_models-10.2.2 app/models/local_service.rb
govuk_content_models-10.2.1 app/models/local_service.rb
govuk_content_models-10.2.0 app/models/local_service.rb
govuk_content_models-10.1.2 app/models/local_service.rb
govuk_content_models-10.1.1 app/models/local_service.rb
govuk_content_models-10.1.0 app/models/local_service.rb
govuk_content_models-10.0.0 app/models/local_service.rb
govuk_content_models-9.0.1 app/models/local_service.rb
govuk_content_models-9.0.0 app/models/local_service.rb
govuk_content_models-8.10.0 app/models/local_service.rb
govuk_content_models-8.9.0 app/models/local_service.rb
govuk_content_models-8.8.0 app/models/local_service.rb
govuk_content_models-8.7.0 app/models/local_service.rb
govuk_content_models-8.6.0 app/models/local_service.rb
govuk_content_models-8.5.0 app/models/local_service.rb
govuk_content_models-8.4.1 app/models/local_service.rb
govuk_content_models-8.4.0 app/models/local_service.rb
govuk_content_models-8.3.1 app/models/local_service.rb