2016-01-10 01:10:22 +01:00
|
|
|
# Organisations related to this agenda
|
2015-02-15 17:10:17 +01:00
|
|
|
class Orga < ActiveRecord::Base
|
2016-01-10 01:10:22 +01:00
|
|
|
strip_attributes
|
2016-04-16 23:08:03 +02:00
|
|
|
has_paper_trail ignore: [: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, -> {}
|
|
|
|
scope :period, ->(year, week) {}
|
|
|
|
|
2015-12-27 16:12:27 +01:00
|
|
|
scope :region, ->(region) { where region: region unless region == 'all' }
|
2016-04-16 23:08:03 +02:00
|
|
|
scope :tag, ->(tag) { where 'tags like ?', "%#{tag}%" }
|
|
|
|
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
|
2015-12-27 00:39:29 +01:00
|
|
|
unless submitter.blank?
|
|
|
|
self.secret ||= SecureRandom.urlsafe_base64(32)[0...32]
|
|
|
|
end
|
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
|
2015-12-27 00:39:29 +01:00
|
|
|
send_secret unless submitter.blank?
|
2015-07-25 18:32:27 +02:00
|
|
|
end
|
|
|
|
|
2015-09-05 18:56:48 +02:00
|
|
|
after_update do
|
2015-12-27 00:39:29 +01:00
|
|
|
unless submitter.blank?
|
2015-12-27 00:35:31 +01:00
|
|
|
send_secret if secret_changed?
|
|
|
|
|
|
|
|
if moderated_changed?
|
|
|
|
OrgaMailer.accept(self).deliver_now!
|
|
|
|
else
|
|
|
|
OrgaMailer.update(self).deliver_now!
|
|
|
|
end
|
2015-07-25 18:32:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before_destroy do
|
2015-12-27 00:39:29 +01:00
|
|
|
OrgaMailer.destroy(self).deliver_now! unless submitter.blank?
|
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: {
|
|
|
|
id: id, name: name,
|
|
|
|
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
|
|
|
|
[address, city, region.try(:name)].compact.join ', '
|
|
|
|
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
|