Commit 95c5ab3c authored by hellekin's avatar hellekin

Rename public to visible

As `public` is a Ruby core method (Module#public), using it as a boolean field
to determine whether the Resource can be accessed publicly can pose
problems down the line.

Since we're dealing with public records anyway, it's useful to rename it
to `visible` and gain consistency with other boolean flags behavior,
e.g., for question methods.
parent 3eb8fead
...@@ -18,7 +18,7 @@ module INCOMMON ...@@ -18,7 +18,7 @@ module INCOMMON
# Create INCOMMON::IncommonAgent # Create INCOMMON::IncommonAgent
Agent.create( Agent.create(
id: -1, id: -1,
public: true, visible: true,
translations: { translations: {
en: { en: {
name: 'IN COMMON Agent', name: 'IN COMMON Agent',
......
...@@ -39,10 +39,10 @@ end ...@@ -39,10 +39,10 @@ end
# #
# id :bigint(8) not null, primary key # id :bigint(8) not null, primary key
# meta :json # meta :json
# public :boolean
# translations :json # translations :json
# type :string(16) # type :string(16) default("Resource")
# uuid :uuid # uuid :uuid
# visible :boolean
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# agent_id :bigint(8) # agent_id :bigint(8)
......
...@@ -9,10 +9,10 @@ end ...@@ -9,10 +9,10 @@ end
# #
# id :bigint(8) not null, primary key # id :bigint(8) not null, primary key
# meta :json # meta :json
# public :boolean
# translations :json # translations :json
# type :string(16) # type :string(16) default("Resource")
# uuid :uuid # uuid :uuid
# visible :boolean
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# agent_id :bigint(8) # agent_id :bigint(8)
......
...@@ -9,10 +9,10 @@ end ...@@ -9,10 +9,10 @@ end
# #
# id :bigint(8) not null, primary key # id :bigint(8) not null, primary key
# meta :json # meta :json
# public :boolean
# translations :json # translations :json
# type :string(16) # type :string(16) default("Resource")
# uuid :uuid # uuid :uuid
# visible :boolean
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# agent_id :bigint(8) # agent_id :bigint(8)
......
...@@ -23,21 +23,25 @@ class Resource < ApplicationRecord ...@@ -23,21 +23,25 @@ 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, ->(res) { res.primary }, class_name: 'ResourceAddress' has_one :primary_address, -> { resource_addresses.primary }, class_name: 'ResourceAddress'
has_one :primary_email, ->(res) { res.primary }, class_name: 'ResourceEmail' has_one :primary_email, -> { resource_emails.primary }, class_name: 'ResourceEmail'
has_one :primary_link, ->(res) { res.primary }, class_name: 'ResourceLink' has_one :primary_link, -> { resource_links.primary }, class_name: 'ResourceLink'
has_one :primary_phone, ->(res) { res.primary }, class_name: 'ResourcePhone' has_one :primary_phone, -> { resource_phones.primary }, class_name: 'ResourcePhone'
end end
has_one :main_address, through: :primary_address, class_name: 'Address' has_one :main_address, through: :primary_address, class_name: 'Address'
has_one :main_email, through: :primary_email, class_name: 'Email' has_one :main_email, through: :primary_email, class_name: 'Email'
has_one :main_link, through: :primary_link, class_name: 'Link' has_one :main_link, through: :primary_link, class_name: 'Link'
has_one :main_phone, through: :primary_phone, class_name: 'Phone' 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
primary_link&.link&.uri
end
end end
# == Schema Information # == Schema Information
...@@ -46,10 +50,10 @@ end ...@@ -46,10 +50,10 @@ end
# #
# id :bigint(8) not null, primary key # id :bigint(8) not null, primary key
# meta :json # meta :json
# public :boolean
# translations :json # translations :json
# type :string(16) # type :string(16) default("Resource")
# uuid :uuid # uuid :uuid
# visible :boolean
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# agent_id :bigint(8) # agent_id :bigint(8)
......
...@@ -8,10 +8,10 @@ class Service < Resource; end ...@@ -8,10 +8,10 @@ class Service < Resource; end
# #
# id :bigint(8) not null, primary key # id :bigint(8) not null, primary key
# meta :json # meta :json
# public :boolean
# translations :json # translations :json
# type :string(16) # type :string(16) default("Resource")
# uuid :uuid # uuid :uuid
# visible :boolean
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# agent_id :bigint(8) # agent_id :bigint(8)
......
...@@ -8,10 +8,10 @@ class Thing < Resource; end ...@@ -8,10 +8,10 @@ class Thing < Resource; end
# #
# id :bigint(8) not null, primary key # id :bigint(8) not null, primary key
# meta :json # meta :json
# public :boolean
# translations :json # translations :json
# type :string(16) # type :string(16) default("Resource")
# uuid :uuid # uuid :uuid
# visible :boolean
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# agent_id :bigint(8) # agent_id :bigint(8)
......
...@@ -6,7 +6,7 @@ class AgentPolicy < ApplicationPolicy ...@@ -6,7 +6,7 @@ class AgentPolicy < ApplicationPolicy
end end
def show? def show?
record == user.agent || record.public? record == user.agent || record_public?
end end
def create? def create?
...@@ -26,7 +26,7 @@ class AgentPolicy < ApplicationPolicy ...@@ -26,7 +26,7 @@ class AgentPolicy < ApplicationPolicy
if user.current_agent_is_incommon? || user.leader? || user.maintainer? if user.current_agent_is_incommon? || user.leader? || user.maintainer?
scope.all scope.all
else else
scope.where(public: true) scope.where(visible: true)
end end
end end
end end
......
...@@ -58,6 +58,6 @@ class ApplicationPolicy ...@@ -58,6 +58,6 @@ class ApplicationPolicy
end end
def record_public? def record_public?
record&.public? == true record&.visible? == true
end end
end end
...@@ -24,9 +24,9 @@ class ResourcePolicy < ApplicationPolicy ...@@ -24,9 +24,9 @@ class ResourcePolicy < ApplicationPolicy
class Scope < Scope class Scope < Scope
def resolve def resolve
if user.authenticated? == true if user.authenticated? == true
scope.where(agent_id: user.uuid).or(scope.where(public: true)) scope.where(agent_id: user.uuid).or(scope.where(visible: true))
else else
scope.where(public: true) scope.where(visible: true)
end end
end end
end end
......
...@@ -16,7 +16,7 @@ class SerializableAgent < SerializableIncommonResource ...@@ -16,7 +16,7 @@ class SerializableAgent < SerializableIncommonResource
attribute :summary attribute :summary
attribute :description attribute :description
attribute :public attribute :visible
# attribute :translations # attribute :translations
attribute :type attribute :type
# attribute :uuid # attribute :uuid
......
...@@ -10,11 +10,29 @@ class SerializableResource < SerializableIncommonResource ...@@ -10,11 +10,29 @@ class SerializableResource < SerializableIncommonResource
attribute :summary attribute :summary
attribute :description attribute :description
attribute :main_address do
@object.main_address.to_s
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 :created_at attribute :created_at
attribute :updated_at attribute :updated_at
belongs_to :agent belongs_to :agent
has_many :locations
link :self do link :self do
@url_helpers.api_resource_url(@object) @url_helpers.api_resource_url(@object)
end end
......
class RenamePublicColumnToVisible < ActiveRecord::Migration[5.2]
def change
change_table :resources do |t|
t.rename :public, :visible
end
end
end
...@@ -550,7 +550,7 @@ ALTER SEQUENCE public.resource_sections_id_seq OWNED BY public.resource_sections ...@@ -550,7 +550,7 @@ ALTER SEQUENCE public.resource_sections_id_seq OWNED BY public.resource_sections
CREATE TABLE public.resources ( CREATE TABLE public.resources (
id bigint NOT NULL, id bigint NOT NULL,
meta json, meta json,
public boolean, visible boolean,
uuid uuid, uuid uuid,
type character varying(16) DEFAULT 'Resource'::character varying, type character varying(16) DEFAULT 'Resource'::character varying,
created_at timestamp without time zone NOT NULL, created_at timestamp without time zone NOT NULL,
...@@ -1520,6 +1520,7 @@ INSERT INTO "schema_migrations" (version) VALUES ...@@ -1520,6 +1520,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20181106092911'), ('20181106092911'),
('20181106094615'), ('20181106094615'),
('20181106153013'), ('20181106153013'),
('20181209023655'); ('20181209023655'),
('20181209191924');
...@@ -88,18 +88,14 @@ entries.each do |ent| ...@@ -88,18 +88,14 @@ entries.each do |ent|
res = Resource.create(name: ent['fields']['name'], res = Resource.create(name: ent['fields']['name'],
description: ent['fields']['comment'], description: ent['fields']['comment'],
public: (ent['fields']['public'] == true), visible: (ent['fields']['public'] == true),
created_at: ent['fields']['created'], created_at: ent['fields']['created'],
updated_at: ent['fields']['last_modified'], updated_at: ent['fields']['last_modified'],
agent_id: agent.id, agent_id: agent.id,
type: 'Resource',
meta: xtr) meta: xtr)
if res&.valid? if res&.valid?
# ResourceAddress.create(address: adr, resource: res) if adr&.valid?
# ResourcePhone.create(phone: phone, resource: res) if phone&.valid?
# ResourceLink.create(link: link, resource: res) if link&.valid?
# ResourceEmail.create(email: email, resource: res) if email&.valid?
# Location.create(position: pos, locatable: res)
# Add resource properties # Add resource properties
res.resource_addresses.create(address: adr) if adr&.valid? res.resource_addresses.create(address: adr) if adr&.valid?
res.resource_emails.create(email: email) if email&.valid? res.resource_emails.create(email: email) if email&.valid?
......
...@@ -151,14 +151,14 @@ def add_contacts_to(res, contacts) ...@@ -151,14 +151,14 @@ def add_contacts_to(res, contacts)
name: "#{contact['prenom']} #{contact['nom']}", name: "#{contact['prenom']} #{contact['nom']}",
email: email_address email: email_address
) )
a.public = contact['type_d_identite'] != 'Personne physique' a.visible = contact['type_d_identite'] != 'Personne physique'
a.meta = { a.meta = {
source: 'RCR', source: 'RCR',
source_original_data: contact, source_original_data: contact,
} }
a.save a.save
# Pass the resource to the contact agent if it's a collective # Pass the resource to the contact agent if it's a collective
res.update_attribute(:agent_id, a.id) if a.public? res.update_attribute(:agent_id, a.id) if a.visible?
end end
end end
......
...@@ -3,7 +3,7 @@ Fabricator(:agent) do ...@@ -3,7 +3,7 @@ Fabricator(:agent) do
summary "Sexist lucky spy" summary "Sexist lucky spy"
description { Faker::Markdown.sandwich } description { Faker::Markdown.sandwich }
meta "{'arbitrary': 'json'}" meta "{'arbitrary': 'json'}"
public false visible false
uuid { SecureRandom.uuid } uuid { SecureRandom.uuid }
type "Agent" type "Agent"
end end
...@@ -14,10 +14,10 @@ end ...@@ -14,10 +14,10 @@ end
# #
# id :bigint(8) not null, primary key # id :bigint(8) not null, primary key
# meta :json # meta :json
# public :boolean
# translations :json # translations :json
# type :string(16) # type :string(16) default("Resource")
# uuid :uuid # uuid :uuid
# visible :boolean
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# agent_id :bigint(8) # agent_id :bigint(8)
......
...@@ -3,21 +3,31 @@ Fabricator(:resource) do ...@@ -3,21 +3,31 @@ Fabricator(:resource) do
summary "MyString" summary "MyString"
description "MyText" description "MyText"
meta "" meta ""
public false visible false
uuid "" uuid ""
type "" type ""
end end
Fabricator(:new_resource) do
name "A New Resource"
summary "It's missing a type"
description nil
meta nil
visible false
uuid nil
type nil
end
# == Schema Information # == Schema Information
# #
# Table name: resources # Table name: resources
# #
# id :bigint(8) not null, primary key # id :bigint(8) not null, primary key
# meta :json # meta :json
# public :boolean
# translations :json # translations :json
# type :string(16) # type :string(16) default("Resource")
# uuid :uuid # uuid :uuid
# visible :boolean
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# agent_id :bigint(8) # agent_id :bigint(8)
......
require 'rails_helper' require 'rails_helper'
RSpec.describe Resource, type: :model do RSpec.describe Resource, type: :model do
pending "add some examples to (or delete) #{__FILE__}" let(:res) { Fabricate(:resource) }
describe 'factory' do
it 'exists and is valid' do
expect(res).to be_valid
end
end
describe 'associations' do
it 'belongs to an Agent' do
expect(res).to belong_to(:agent)
end
end
describe 'validations' do
pending '#meta'
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')
end
end
describe '#uuid' do
it 'is optional' do
res.uuid = nil
expect(res).to be_valid
end
it 'is managed by UUIDParameter' do
expect(res.class.ancestors).to include(UUIDParameter)
end
end
end
end end
# == Schema Information # == Schema Information
...@@ -10,10 +49,10 @@ end ...@@ -10,10 +49,10 @@ end
# #
# id :bigint(8) not null, primary key # id :bigint(8) not null, primary key
# meta :json # meta :json
# public :boolean
# translations :json # translations :json
# type :string(16) # type :string(16) default("Resource")
# uuid :uuid # uuid :uuid
# visible :boolean
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# agent_id :bigint(8) # agent_id :bigint(8)
......
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