From b54a8458d5029b3494165b7430e21b3ae34ecc0c Mon Sep 17 00:00:00 2001 From: hellekin Date: Fri, 22 Jan 2021 05:32:15 +0100 Subject: Upgrade Rails and add StimulusJS support --- app/models/agency.rb | 28 ---------------------------- app/models/resource.rb | 20 ++++++++++++++++---- 2 files changed, 16 insertions(+), 32 deletions(-) (limited to 'app/models') diff --git a/app/models/agency.rb b/app/models/agency.rb index 7ffe489..842e8bd 100644 --- a/app/models/agency.rb +++ b/app/models/agency.rb @@ -3,34 +3,6 @@ # SPDX-License-Identifier: AGPL-3.0-or-later class Agency < ApplicationRecord - include Bitfields - belongs_to :agent belongs_to :user - - bitfield :roles, :observer, :editor, :maintainer, :leader - - class << self - # Grant role in agent to user - def grant(agent, user, role) - r = find_or_create_by(agent: agent, user: user) - r&.public_send("#{role}=", true) && r&.save - end - - # Revoke role in agent from user - def revoke(agent, user, role) - r = find_by(agent: agent, user: user) - r&.public_send("#{role}=", false) && r&.save - end - end - - # Grant role to current user in current agent - def grant(role) - self.class.grant(agent, user, role) - end - - # Revoke role from current user in current agent - def revoke(role) - self.class.revoke(agent, user, role) - end end diff --git a/app/models/resource.rb b/app/models/resource.rb index 8df106c..bde0c56 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -44,17 +44,17 @@ class Resource < ApplicationRecord # You can use, e.g.: res.longitude = 0.123 def longitude - feature['geometry']['coordinates'][0] + format('%3.7f', lon: feature['geometry']['coordinates'][0]).to_f end def longitude=(value) - feature['geometry']['coordinates'][0] = value + feature['geometry']['coordinates'][0] = format('%3.7f', lon: value).to_f end # You can use, e.g.: res.latitude = 0.123 def latitude - feature['geometry']['coordinates'][1] + format('%2.7f', lat: feature['geometry']['coordinates'][1]).to_f end def latitude=(value) - feature['geometry']['coordinates'][1] = value + feature['geometry']['coordinates'][1] = format('%2.7f', lat: value).to_f end # Properties @@ -68,4 +68,16 @@ class Resource < ApplicationRecord feature['properties'][prop.to_s] = v end end + + # Poor man's GeoJSON output + def to_geojson + out = feature.dup + # Convert original Dewey IDs with local Section IDs + out['properties']['categories'] = Section.where(dewey_id: out['properties']['categories']).pluck(:id) + # Add IN COMMON Resource UUID property + out['properties']['uuid'] = uuid + # Add IN COMMON Agent UUID property + out['properties']['agent_uuid'] = agent.uuid + out + end end -- cgit v1.2.3