2016-01-10 01:10:22 +01:00
|
|
|
# Organisations related to this agenda
|
2017-05-27 09:34:24 +02:00
|
|
|
class Orga < ApplicationRecord
|
2016-12-17 16:59:11 +01:00
|
|
|
acts_as_taggable
|
2016-01-10 01:10:22 +01:00
|
|
|
strip_attributes
|
2017-04-22 20:01:47 +02:00
|
|
|
has_paper_trail ignore: %i[last_updated secret submitter decision_time
|
|
|
|
lock_version latitude longitude]
|
2015-07-25 18:32:27 +02:00
|
|
|
|
2015-02-15 15:50:47 +01:00
|
|
|
belongs_to :region
|
2015-02-28 17:28:05 +01:00
|
|
|
belongs_to :kind
|
|
|
|
|
|
|
|
validates :kind, presence: true
|
|
|
|
validates :name, presence: true
|
|
|
|
validates :region, presence: true
|
|
|
|
validates :url, format: %r{\Ahttps?:\/\/.*\..*\z}
|
2016-01-07 22:20:09 +01:00
|
|
|
validates :diaspora, allow_blank: true, format: %r{\Ahttps?:\/\/.*\..*\z}
|
2015-02-28 17:28:05 +01:00
|
|
|
validates :feed, allow_blank: true, format: %r{\Ahttps?:\/\/.*\..*\z}
|
2015-12-26 23:32:04 +01:00
|
|
|
validates :contact, allow_blank: true, email: true
|
2015-02-28 17:28:05 +01:00
|
|
|
validates :submitter, allow_blank: true, email: true
|
|
|
|
|
2016-04-09 16:06:22 +02:00
|
|
|
geocoded_by :full_address
|
2018-01-01 17:52:33 +01:00
|
|
|
# after_validation :geocode, if: -> (obj) { obj.saved_change_to_address? }
|
2016-04-09 16:06:22 +02:00
|
|
|
after_validation :geocode
|
|
|
|
|
2015-12-26 23:32:04 +01:00
|
|
|
scope :active, -> { where active: true }
|
2015-07-25 18:32:27 +02:00
|
|
|
scope :moderated, -> { where moderated: true }
|
|
|
|
scope :unmoderated, -> { where moderated: false }
|
|
|
|
|
2016-04-16 23:08:03 +02:00
|
|
|
# Only present to simplify maps_controller, to have the same scopes as events
|
|
|
|
scope :locality, -> {}
|
|
|
|
scope :daylimit, -> {}
|
|
|
|
scope :future, -> {}
|
2016-05-21 12:02:19 +02:00
|
|
|
scope :period, ->(_year, _week) {}
|
2016-04-16 23:08:03 +02:00
|
|
|
|
2017-07-02 15:40:13 +02:00
|
|
|
scope :kind, ->(kind) { where kind: kind }
|
2017-09-17 18:25:34 +02:00
|
|
|
scope :region, (lambda do |region|
|
2017-09-17 20:41:48 +02:00
|
|
|
return if region.nil? || region == 'all' || region.to_i.zero?
|
2018-09-15 15:05:21 +02:00
|
|
|
|
2017-09-17 18:25:34 +02:00
|
|
|
temp = Region.find region
|
|
|
|
where region: [temp, temp.regions].flatten
|
|
|
|
end)
|
2016-12-17 16:59:11 +01:00
|
|
|
scope :tag, ->(tag) { tagged_with tag }
|
2016-04-16 23:08:03 +02:00
|
|
|
scope :geo, -> { where 'latitude is not null and longitude is not null' }
|
2017-07-02 15:40:13 +02:00
|
|
|
scope :active, -> { where active: true }
|
2015-12-27 16:12:27 +01:00
|
|
|
|
2015-05-25 11:42:53 +02:00
|
|
|
before_validation do
|
2016-11-08 15:40:35 +01:00
|
|
|
# Populate submitter using contact info if absent
|
|
|
|
self.submitter ||= contact
|
|
|
|
self.secret ||= SecureRandom.urlsafe_base64(32)[0...32]
|
2015-05-25 11:42:53 +02:00
|
|
|
self.submission_time ||= Time.zone.now
|
2015-02-28 17:28:05 +01:00
|
|
|
end
|
2015-07-25 18:32:27 +02:00
|
|
|
|
|
|
|
after_create do
|
2017-07-02 12:16:53 +02:00
|
|
|
send_secret if submitter.present?
|
2016-10-31 22:41:20 +01:00
|
|
|
# Send email to moderators when an new orga is received
|
|
|
|
ModerationorgaMailer.create(self).deliver_now!
|
2015-07-25 18:32:27 +02:00
|
|
|
end
|
|
|
|
|
2015-09-05 18:56:48 +02:00
|
|
|
after_update do
|
2018-01-01 17:52:33 +01:00
|
|
|
send_secret if saved_change_to_secret? || saved_change_to_submitter?
|
2016-11-08 15:40:35 +01:00
|
|
|
|
2018-01-01 17:52:33 +01:00
|
|
|
if saved_change_to_moderated?
|
2016-11-08 15:40:35 +01:00
|
|
|
OrgaMailer.accept(self).deliver_now!
|
|
|
|
# Send email to moderators when an orga is accepted
|
|
|
|
ModerationorgaMailer.accept(self).deliver_now!
|
|
|
|
else
|
|
|
|
OrgaMailer.update(self).deliver_now!
|
|
|
|
# Send email to moderators when an orga is updated
|
|
|
|
ModerationorgaMailer.update(self).deliver_now!
|
2015-07-25 18:32:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before_destroy do
|
2017-04-22 20:01:47 +02:00
|
|
|
OrgaMailer.destroy(self).deliver_now! if submitter.present?
|
2016-10-31 22:35:14 +01:00
|
|
|
# Send email to moderators when an orga is deleted
|
2017-04-22 20:01:47 +02:00
|
|
|
ModerationorgaMailer.destroy(self).deliver_now! if submitter.present?
|
2015-07-25 18:32:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def send_secret
|
|
|
|
OrgaMailer.create(self).deliver_now!
|
|
|
|
end
|
2015-10-22 22:03:18 +02:00
|
|
|
|
|
|
|
def name_as_tag
|
2017-07-01 17:25:15 +02:00
|
|
|
name.gsub(/\AL'/, '').gsub(/[\s\*'\.]/, '-').delete ':'
|
2015-10-22 22:03:18 +02:00
|
|
|
end
|
2016-01-10 01:10:22 +01:00
|
|
|
|
2016-04-16 23:08:03 +02:00
|
|
|
def as_json(_options = {})
|
|
|
|
{ type: 'Feature', properties: {
|
2016-05-21 23:15:49 +02:00
|
|
|
id: id, icon: kind.icon, name: name,
|
2016-04-16 23:08:03 +02:00
|
|
|
place_name: place_name, address: address, city: city,
|
|
|
|
tags: tags,
|
|
|
|
popupContent: "<a href=\"/orgas/#{id}\">#{self}</a>"
|
|
|
|
}, geometry: { type: 'Point', coordinates: [longitude, latitude] } }
|
|
|
|
end
|
|
|
|
|
2016-04-09 16:06:22 +02:00
|
|
|
def full_address
|
2017-10-08 14:28:18 +02:00
|
|
|
[address, city, region, region.try(:region)].compact.join ', '
|
2016-04-09 16:06:22 +02:00
|
|
|
end
|
|
|
|
|
2016-01-10 01:10:22 +01:00
|
|
|
def to_s
|
|
|
|
"[#{kind.name}] #{name}"
|
|
|
|
end
|
2013-12-28 15:45:21 +01:00
|
|
|
end
|