Commit 09d26ce0 authored by hellekin's avatar hellekin

Fix resources

parent 77905bc6
......@@ -20,6 +20,17 @@ class Api::V0::ResourcesController < ApplicationController
private
def model_types
%w(taxonomies sections).freeze
%w(agent
collections
locations
sections
resource_addresses
resource_emails
resource_links
resource_phones
addresses
emails
links
phones).freeze
end
end
......@@ -23,24 +23,32 @@ class Resource < ApplicationRecord
has_many :phones, through: :resource_phones, source: :phone
with_options inverse_of: :resource do
has_one :primary_address, -> { resource_addresses.primary }, class_name: 'ResourceAddress'
has_one :primary_email, -> { resource_emails.primary }, class_name: 'ResourceEmail'
has_one :primary_link, -> { resource_links.primary }, class_name: 'ResourceLink'
has_one :primary_phone, -> { resource_phones.primary }, class_name: 'ResourcePhone'
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'
end
has_one :main_address, through: :primary_address, class_name: 'Address'
has_one :main_email, through: :primary_email, class_name: 'Email'
has_one :main_link, through: :primary_link, class_name: 'Link'
has_one :main_phone, through: :primary_phone, class_name: 'Phone'
has_many :resource_collections, dependent: :destroy
has_many :resource_sections, dependent: :destroy
has_many :sections, through: :resource_sections, source: :section
def homepage_url
primary_link&.link&.uri
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
end
end
......
# frozen_string_literal: true
class SerializableResource < SerializableIncommonResource
type { @object.type }
type { @object.type.pluralize.underscore }
id { @object.uuid }
......@@ -10,30 +10,49 @@ class SerializableResource < SerializableIncommonResource
attribute :summary
attribute :description
attribute :main_address do
@object.main_address.to_s
# Present primary properties as attributes
%w(address email link phone).each do |prop|
attribute "main_#{prop}".to_sym do
@object.send("main_#{prop}")
end
attribute :main_email do
@object.primary_email.to_s
end
attribute :homepage_url do
@object.homepage_url
end
attribute :main_phone do
@object.primary_phone.to_s
end
attribute :type
attribute :uuid
attribute :visible
attribute :created_at
attribute :updated_at
belongs_to :agent
# Descriptors
has_many :locations
# Relations
with_options if: -> { @object.type == 'Agent' && maintainer? } do
has_many :resource_addresses
has_many :resource_emails
has_many :resource_links
has_many :resource_phones
end
# Properties
has_many :addresses
has_many :emails
has_many :links
has_many :phones
# Resource collections
has_many :collections, unless: -> { @object.type == 'Agent' && restricted? }
# Resource sections (taxonomy)
has_many :sections, if: -> { @object.type == 'Agent' && leader? }
link :self do
@url_helpers.api_resource_url(@object)
end
meta do
meta_current
end
end
......@@ -20,15 +20,11 @@ RSpec.describe Resource, type: :model do
pending '#visible'
pending '#translations'
describe '#type' do
let(:newres) { Fabricate(:resource, type: nil) }
it 'must be present' do
expect(newres.type).to be_nil
expect(newres).not_to be_valid
end
it 'defaults to \'Resource\'' do
expect(newres).to receive(:save).and_return(true)
newres.save
expect(newres.type).to eql('Resource')
r = Resource.new(name: 'Sample')
expect(r).to receive(:save).and_return(true)
r.save
expect(r.type).to eql('Resource')
end
end
describe '#uuid' do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment