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
|
|
|
|
# after_validation :geocode, if: -> (obj) { obj.address_changed? }
|
|
|
|
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
|
|
|
|
2015-12-27 16:12:27 +01:00
|
|
|
scope :region, ->(region) { where region: region unless region == 'all' }
|
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' }
|
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
|
2016-11-08 15:40:35 +01:00
|
|
|
send_secret
|
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
|
2016-11-08 15:40:35 +01:00
|
|
|
send_secret if secret_changed? || submitter_changed?
|
|
|
|
|
|
|
|
if moderated_changed?
|
|
|
|
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
|
2016-01-02 17:32:22 +01: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
|
2016-04-20 16:25:53 +02:00
|
|
|
# Region seems to disturb openstreetmap :(
|
|
|
|
# [address, city, region.try(:name)].compact.join ', '
|
|
|
|
[address, city].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
|