Commit 09d26ce0 authored by hellekin's avatar hellekin

Fix resources

parent 77905bc6
...@@ -20,6 +20,17 @@ class Api::V0::ResourcesController < ApplicationController ...@@ -20,6 +20,17 @@ class Api::V0::ResourcesController < ApplicationController
private private
def model_types 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
end end
...@@ -23,24 +23,32 @@ class Resource < ApplicationRecord ...@@ -23,24 +23,32 @@ class Resource < ApplicationRecord
has_many :phones, through: :resource_phones, source: :phone has_many :phones, through: :resource_phones, source: :phone
with_options inverse_of: :resource do with_options inverse_of: :resource do
has_one :primary_address, -> { resource_addresses.primary }, class_name: 'ResourceAddress' has_one :primary_address, -> { primary }, class_name: 'ResourceAddress'
has_one :primary_email, -> { resource_emails.primary }, class_name: 'ResourceEmail' has_one :primary_email, -> { primary }, class_name: 'ResourceEmail'
has_one :primary_link, -> { resource_links.primary }, class_name: 'ResourceLink' has_one :primary_link, -> { primary }, class_name: 'ResourceLink'
has_one :primary_phone, -> { resource_phones.primary }, class_name: 'ResourcePhone' has_one :primary_phone, -> { primary }, class_name: 'ResourcePhone'
end 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_collections, dependent: :destroy
has_many :resource_sections, dependent: :destroy has_many :resource_sections, dependent: :destroy
has_many :sections, through: :resource_sections, source: :section has_many :sections, through: :resource_sections, source: :section
def homepage_url def main_address
primary_link&.link&.uri 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
end end
......
# frozen_string_literal: true # frozen_string_literal: true
class SerializableResource < SerializableIncommonResource class SerializableResource < SerializableIncommonResource
type { @object.type } type { @object.type.pluralize.underscore }
id { @object.uuid } id { @object.uuid }
...@@ -10,30 +10,49 @@ class SerializableResource < SerializableIncommonResource ...@@ -10,30 +10,49 @@ class SerializableResource < SerializableIncommonResource
attribute :summary attribute :summary
attribute :description attribute :description
attribute :main_address do # Present primary properties as attributes
@object.main_address.to_s %w(address email link phone).each do |prop|
attribute "main_#{prop}".to_sym do
@object.send("main_#{prop}")
end
end end
attribute :main_email do attribute :type
@object.primary_email.to_s attribute :uuid
end attribute :visible
attribute :homepage_url do
@object.homepage_url
end
attribute :main_phone do
@object.primary_phone.to_s
end
attribute :created_at attribute :created_at
attribute :updated_at attribute :updated_at
belongs_to :agent belongs_to :agent
# Descriptors
has_many :locations 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 link :self do
@url_helpers.api_resource_url(@object) @url_helpers.api_resource_url(@object)
end end
meta do
meta_current
end
end end
...@@ -20,15 +20,11 @@ RSpec.describe Resource, type: :model do ...@@ -20,15 +20,11 @@ RSpec.describe Resource, type: :model do
pending '#visible' pending '#visible'
pending '#translations' pending '#translations'
describe '#type' do 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 it 'defaults to \'Resource\'' do
expect(newres).to receive(:save).and_return(true) r = Resource.new(name: 'Sample')
newres.save expect(r).to receive(:save).and_return(true)
expect(newres.type).to eql('Resource') r.save
expect(r.type).to eql('Resource')
end end
end end
describe '#uuid' do 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