forked from rughh/on_ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.rb
66 lines (53 loc) · 1.87 KB
/
event.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true
class Event < ApplicationRecord
include Slug
extend ApiHandling
expose_api :id, :name, :description, :date, :location_id, :user_id, :participants, :topics, :materials, :location
belongs_to :location, optional: true
belongs_to :user
has_many :participants
has_many :users, through: :participants
has_many :topics
has_many :materials
validates :user, :name, :description, :date, presence: true
validates :name, uniqueness: { scope: :label }
accepts_nested_attributes_for :materials
accepts_nested_attributes_for :topics
default_scope -> { where(label: Whitelabel[:label_id]) }
scope :with_topics, -> { joins(:topics).distinct }
scope :current, -> { where(date: Date.today.to_time..).limit(1).order('date ASC') }
scope :latest, -> { where('date < ?', Date.today.to_time).order('date DESC') }
scope :unpublished, -> { where('published IS NULL') }
scope :ordered, -> { order('date DESC') }
def end_date
date + 2.hours
end
def closed?
limit.present? && participants.count >= limit
end
def particpate(user)
return false if users.include? user
!!participants.create!(user:)
end
class << self
def duplicate!
latest = Event.last
date = Whitelabel[:next_event_date]
Event.new.tap do |it|
it.name = "#{I18n.tw('name')} - #{I18n.l date, locale: :de, format: :month}"
it.date = date
it.user = latest.user
it.location = latest.location
it.description = latest.description
it.save!
end
end
def stats(size: 10)
stats = Event.limit(size).map { |event| [event.participants.count, event.topics.count] }
{
participants: stats.empty? ? 0 : stats.map(&:first).sum / stats.size,
topics: stats.empty? ? 0 : stats.map(&:last).sum / stats.size,
}
end
end
end