Sha256: b00ef2d434b013b1823c91fa93e8764b733baf19a34429ebb83e60e390388de4

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

require "csv"
require "local_interaction"
require "safe_html"

class LocalAuthority
  include Mongoid::Document

  embeds_many :local_interactions

  field :name,               type: String
  field :snac,               type: String
  field :local_directgov_id, type: Integer
  field :tier,               type: String
  field :contact_address,    type: Array
  field :contact_url,        type: String
  field :contact_phone,      type: String
  field :contact_email,      type: String
  field :homepage_url,       type: String

  validates_uniqueness_of :snac
  validates_presence_of   :snac, :local_directgov_id, :name, :tier

  scope :for_snacs, ->(snacs) { any_in(snac: snacs) }

  def self.find_by_snac(snac)
    for_snacs([snac]).first
  end

  def provides_service?(lgsl_code, lgil_code = nil)
    interactions_for(lgsl_code, lgil_code).any?
  end

  def interactions_for(lgsl_code, lgil_code = nil)
    interactions = local_interactions.where(lgsl_code: lgsl_code)
    if lgil_code
      interactions.where(lgil_code: lgil_code)
    else
      interactions
    end
  end

  def preferred_interaction_for(lgsl_code, lgil_code = nil)
    interactions = local_interactions.where(lgsl_code: lgsl_code)
    if lgil_code
      interactions.where(lgil_code: lgil_code).first
    else
      interactions.excludes(
        lgil_code: LocalInteraction::LGIL_CODE_PROVIDING_INFORMATION
      ).order_by(lgil_code: :asc).first ||
      interactions.where(
        lgil_code: LocalInteraction::LGIL_CODE_PROVIDING_INFORMATION
      ).first
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
govuk_content_models-35.0.1 app/models/local_authority.rb
govuk_content_models-35.0.0 app/models/local_authority.rb