resource.rb 1.94 KB
Newer Older
hellekin's avatar
hellekin committed
1 2 3
# frozen_string_literal: true

class Resource < ApplicationRecord
4 5
  # I18n :translations
  extend Mobility
hellekin's avatar
hellekin committed
6
  translates :name, :summary, :description
7
  # Universally Unique Identifier :uuid
hellekin's avatar
hellekin committed
8 9
  include UUIDParameter

hellekin's avatar
hellekin committed
10 11
  belongs_to :agent, optional: true, class_name: 'Agent', inverse_of: :resources

hellekin's avatar
hellekin committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25
  with_options dependent: :destroy do
    has_many :locations, as: :locatable
    has_many :resource_addresses
    has_many :resource_emails
    has_many :resource_links
    has_many :resource_phones
  end

  has_many :addresses, through: :resource_addresses, source: :address
  has_many :emails,    through: :resource_emails,    source: :email
  has_many :links,     through: :resource_links,     source: :link
  has_many :phones,    through: :resource_phones,    source: :phone

  with_options inverse_of: :resource do
hellekin's avatar
hellekin committed
26 27 28 29
    has_one  :primary_address, -> { primary }, class_name: 'ResourceAddress'
    has_one  :primary_email,   -> { primary }, class_name: 'ResourceEmail'
    has_one  :primary_link,    -> { primary }, class_name: 'ResourceLink'
    has_one  :primary_phone,   -> { primary }, class_name: 'ResourcePhone'
hellekin's avatar
hellekin committed
30 31
  end

hellekin's avatar
hellekin committed
32 33
  has_many :resource_collections, dependent: :destroy

34 35
  has_many :resource_sections, dependent: :destroy
  has_many :sections, through: :resource_sections, source: :section
hellekin's avatar
hellekin committed
36

hellekin's avatar
hellekin committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  def main_address
    primary_address&.address&.to_s
  end

  def main_email
    primary_email&.email&.to_s
  end

  def main_link
    primary_link&.link&.to_s
  end
  alias homepage_url main_link

  def main_phone
    primary_phone&.phone&.to_s
hellekin's avatar
hellekin committed
52
  end
hellekin's avatar
hellekin committed
53 54 55 56 57 58
end

# == Schema Information
#
# Table name: resources
#
59 60 61
#  id           :bigint(8)        not null, primary key
#  meta         :json
#  translations :json
hellekin's avatar
hellekin committed
62
#  type         :string(16)       default("Resource")
63
#  uuid         :uuid
hellekin's avatar
hellekin committed
64
#  visible      :boolean
65 66
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
hellekin's avatar
hellekin committed
67 68 69 70 71
#  agent_id     :bigint(8)
#
# Indexes
#
#  index_resources_on_agent_id  (agent_id)
hellekin's avatar
hellekin committed
72
#