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
|
|
|
|
has_paper_trail ignore: [:last_updated, :secret, :submitter]
|
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
|
|
|
|
|
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 }
|
|
|
|
|
2015-12-27 16:12:27 +01:00
|
|
|
scope :region, ->(region) { where region: region unless region == 'all' }
|
|
|
|
|
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
|
|
|
|
|
|
|
def to_s
|
|
|
|
"[#{kind.name}] #{name}"
|
|
|
|
end
|
2013-12-28 15:45:21 +01:00
|
|
|
end
|