agenda-libre-ruby/app/models/orga.rb

63 lines
1.6 KiB
Ruby
Raw Normal View History

# Organisations related to this agenda
2015-02-15 17:10:17 +01:00
class Orga < ActiveRecord::Base
strip_attributes
has_paper_trail ignore: [:last_updated, :secret, :submitter]
belongs_to :region
belongs_to :kind
validates :kind, presence: true
validates :name, presence: true
validates :region, presence: true
validates :url, format: %r{\Ahttps?:\/\/.*\..*\z}
validates :diaspora, allow_blank: true, format: %r{\Ahttps?:\/\/.*\..*\z}
validates :feed, allow_blank: true, format: %r{\Ahttps?:\/\/.*\..*\z}
validates :contact, allow_blank: true, email: true
validates :submitter, allow_blank: true, email: true
scope :active, -> { where active: true }
scope :moderated, -> { where moderated: true }
scope :unmoderated, -> { where moderated: false }
scope :region, ->(region) { where region: region unless region == 'all' }
2015-05-25 11:42:53 +02:00
before_validation do
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
end
after_create do
send_secret unless submitter.blank?
end
after_update do
unless submitter.blank?
send_secret if secret_changed?
if moderated_changed?
OrgaMailer.accept(self).deliver_now!
else
OrgaMailer.update(self).deliver_now!
end
end
end
before_destroy do
OrgaMailer.destroy(self).deliver_now! unless submitter.blank?
end
def send_secret
OrgaMailer.create(self).deliver_now!
end
def name_as_tag
name.gsub(/\AL'/, '').gsub(/[\s\*']/, '-').delete ':'
end
def to_s
"[#{kind.name}] #{name}"
end
end