Commit 3b8b69f7 authored by hellekin's avatar hellekin

Add Resource properties serialization

parent fe6e032a
......@@ -16,6 +16,14 @@ class Address < ApplicationRecord
presence: true,
length: { is: 2 }
def to_s
[street, pobox, postal_code, locality, region, country_name].compact.join(' ')
end
def country_name
COUNTRIES[country]
end
private
def normalize_country
......
......@@ -27,6 +27,10 @@ class Email < ApplicationRecord
uniqueness: true,
format: { with: URI::MailTo::EMAIL_REGEXP }
def to_s
address
end
class << self
def with_address!(address)
find_by("#{hashable_column_name}": EmailHelper.hash_email(address)) ||
......
......@@ -15,6 +15,10 @@ class Link < ApplicationRecord
!secure?
end
def to_s
uri
end
has_many :resource_links, dependent: :destroy
has_many :agents, through: :resource_links, foreign_key: :resource_id, foreign_type: 'Agent'
......
......@@ -20,6 +20,10 @@ class Phone < ApplicationRecord
presence: true,
phony_plausible: { enforce_record_country: false },
format: { with: /\A\+\d+\z/ }
def to_s
number.phony_formatted(format: :international)
end
end
# == Schema Information
......
......@@ -7,7 +7,7 @@ class ResourceAddress < ApplicationRecord
belongs_to :address
has_one :main_address,
-> { where(bitfield_sql(primary: true)) },
-> { primary },
class_name: 'Address',
inverse_of: :resource_addresses
belongs_to :resource, polymorphic: true
......
......@@ -8,7 +8,7 @@ class ResourceEmail < ApplicationRecord
belongs_to :email
belongs_to :resource, polymorphic: true
with_options inverse_of: :resource_emails do
has_one :main_email, -> { where(bitfield_sql(primary: true)) }, class_name: 'Email'
has_one :main_email, -> { primary }, class_name: 'Email'
belongs_to :agent, -> { where(resource_type: 'Agent') }, class_name: 'Agent', optional: true
end
end
......
......@@ -9,17 +9,18 @@ class ResourceLink < ApplicationRecord
belongs_to :resource, polymorphic: true
with_options inverse_of: :resource_links do
belongs_to :agent,
-> { where(resource_type: 'Agent') },
-> { where(type: 'Agent') },
class_name: 'Agent',
foreign_key: :resource_id,
optional: true
has_one :main_link,
-> { where(bitfield_sql(homepage: true)) },
class_name: 'Link'
has_many :origins,
-> { where(bitfield_sql(origin: true)) },
-> { origin },
class_name: 'Link'
end
scope :icon, -> { icon }
scope :origin, -> { origin }
scope :primary, -> { homepage }
end
# == Schema Information
......
......@@ -13,14 +13,24 @@ class ResourcePhone < ApplicationRecord
class_name: 'Agent',
optional: true
with_options class_name: 'Phone' do
has_one :main_phone, -> { where(bitfield_sql(primary: true)) }
has_many :home_phones, -> { where(bitfield_sql(home: true)) }
has_many :office_phones, -> { where(bitfield_sql(office: true)) }
has_many :mobile_phones, -> { where(bitfield_sql(mobile: true)) }
has_many :fax_phones, -> { where(bitfield_sql(fax: true)) }
has_one :main_phone, -> { primary }
has_many :home_phones, -> { home }
has_many :office_phones, -> { office }
has_many :mobile_phones, -> { mobile }
has_many :fax_phones, -> { fax }
end
end
def to_s
main_phone.&number
end
scope :fax, -> { where(bitfield_sql(fax: true)) }
scope :home, -> { where(bitfield_sql(home: true)) }
scope :mobile, -> { where(bitfield_sql(mobile: true)) }
scope :office, -> { where(bitfield_sql(office: true)) }
scope :primary, -> { where(bitfield_sql(primary: true)) }
# TODO: this breaks, reclaiming a 'resource_phones.resource_phone_id' key...
# See: https://github.com/rails/rails/issues/20676
# validates :main_phone,
......
# frozen_string_literal: true
class SerializableAddress < JSONAPI::Serializable::Resource
type 'addresses'
attribute :street
attribute :pobox
attribute :postal_code
attribute :locality
attribute :region
attribute :country
attribute :created_at
attribute :updated_at
has_many :resource_addresses do
meta do
{
resources_count: @object.resources.count,
entities_count: @object.entities.count,
places_count: @object.places.count,
services_count: @object.services.count,
things_count: @object.things.count,
}
end
end
meta do
meta_current
end
end
# frozen_string_literal: true
class SerializableEmail < JSONAPI::Serializable::Resource
type 'emails'
attribute :address do
restricted? ? @object.sha256_hash : @object.address
end
# attribute :flags
attribute :bouncing do
@object.bouncing?
end
attribute :failing do
@object.failing?
end
attribute :created_at
attribute :updated_at
has_many :resource_emails do
meta do
{
resources_count: @object.resources.count,
entities_count: @object.entities.count,
places_count: @object.places.count,
services_count: @object.services.count,
things_count: @object.things.count,
}
end
end
meta do
meta_current
end
end
# frozen_string_literal: true
class SerializableLink < JSONAPI::Serializable::Resource
type 'links'
attribute :uri
# Flags
%w(secure not_found cert_error server_error).each do |flag|
attribute :"#{flag}" do
@object.send(:"#{flag}?")
end
end
attribute :created_at
attribute :updated_at
has_many :agents
has_many :resource_links
meta do
meta_current
end
end
# frozen_string_literal: true
class SerializableResourceAddress < JSONAPI::Serializable::Resource
type 'resource_addresses'
attribute :resource_type
# Flags
attribute :flags
attribute :created_at
attribute :updated_at
has_one :main_address
has_one :address
has_one :resource
has_one :agent
meta do
meta_current
end
end
# frozen_string_literal: true
class SerializableResourceEmail < JSONAPI::Serializable::Resource
type 'resource_emails'
attribute :resource_type
# Flags
attribute :primary do
@object.primary?
end
attribute :validated do
@object.validated?
end
attribute :personal do
@object.personal?
end
attribute :created_at
attribute :updated_at
belongs_to :email
belongs_to :resource
has_one :agent
has_one :main_email
meta do
meta_current
end
end
# frozen_string_literal: true
class SerializableResourceLink < JSONAPI::Serializable::Resource
type 'resource_links'
# Flags
attribute :homepage do
@object.homepage?
end
attribute :icon do
@object.icon?
end
attribute :origin do
@object.origin?
end
attribute :resource_type
attribute :created_at
attribute :updated_at
belongs_to :agent
belongs_to :link
belongs_to :resource
has_many :origins
meta do
meta_current
end
end
# frozen_string_literal: true
class SerializableResourcePhone < JSONAPI::Serializable::Resource
type 'resource_phones'
attribute :resource_type
# Flags
# attribute :flags
attribute :primary do
@object.primary?
end
attribute :home do
@object.home?
end
attribute :office do
@object.office?
end
attribute :mobile do
@object.mobile?
end
attribute :fax do
@object.fax?
end
attribute :created_at
attribute :updated_at
has_one :main_phone
has_one :phone
has_one :resource
has_one :agent
has_many :home_phones
has_many :office_phones
has_many :mobile_phones
has_many :fax_phones
meta do
meta_current
end
end
......@@ -11,6 +11,10 @@ RSpec.describe Phone, type: :model do
describe 'validations' do
describe '#number' do
it 'must be present' do
phone.number = nil
expect(phone).not_to be_valid
end
pending 'is a valid E.164 international number'
end
end
......
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