-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathdojo.rb
78 lines (65 loc) · 2.12 KB
/
dojo.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
67
68
69
70
71
72
73
74
75
76
77
78
class Dojo < ApplicationRecord
NUM_OF_COUNTRIES = "100"
NUM_OF_WORLD_DOJOS = "2,000"
NUM_OF_TOTAL_EVENTS = "1,100"
NUM_OF_TOTAL_NINJAS = "7,000"
NUM_OF_PARTNERSHIPS = "25"
DOJO_INFO_YAML_PATH = Rails.root.join('db', 'dojos.yaml')
belongs_to :prefecture
has_many :dojo_event_services, dependent: :destroy
has_many :event_histories, dependent: :destroy
serialize :tags
before_save { self.email = self.email.downcase }
scope :default_order, -> { order(prefecture_id: :asc, order: :asc) }
scope :active, -> { where(is_active: true ) }
scope :inactive, -> { where(is_active: false) }
validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: false
validates :order, presence: false
validates :description, presence: true, length: { maximum: 50 }
validates :logo, presence: false
validates :tags, presence: true
validate :number_of_tags
validates :url, presence: true
class << self
def load_attributes_from_yaml
YAML.unsafe_load_file(DOJO_INFO_YAML_PATH)
end
def dump_attributes_to_yaml(attributes)
YAML.dump(attributes, File.open(DOJO_INFO_YAML_PATH, 'w'))
end
def active_dojos_count
active.sum(:counter)
end
def group_by_region
eager_load(:prefecture).default_order.group_by { |dojo| dojo.prefecture.region }
end
def group_by_region_on_active
active.group_by_region
end
def aggregatable_annual_count(period)
Hash[
joins(:dojo_event_services)
.where(created_at: period)
.group('year')
.order('year ASC')
.pluck(Arel.sql("to_char(dojos.created_at, 'yyyy') AS year, COUNT(DISTINCT dojos.id)"))
]
end
def annual_count(period)
Hash[
where(created_at: period)
.group('year')
.order('year ASC')
.pluck(Arel.sql("to_char(created_at, 'yyyy') AS year, SUM(counter)"))
]
end
end
private
def number_of_tags
num_of_tags = self.tags.length
if num_of_tags > 5
errors.add(:number_of_tags, 'should be 1 to 5')
end
end
end